Django Unit Testing in VSCode

While PyCharm Pro might be the most popular IDE for Python development, the community version supports neither Django nor JavaScript . VSCode is a free opensource alternative which has pretty good Python support.

Python is supported in VSCode using an extension.  A quick start guide on the extension can be found here.  It has builtin support for unittest,  pytest,  and Nose.

You can find information about debugging Django using VSCode here.  The extension does not have builtin support for Django UnitTests,  but we can use pytest to run Django UnitTests.  Please make sure that you have installed pytest and pytest-django. They can be installed using pip.

Go to Default Settings and search for ‘python.unitTest’. Change the setting

"python.unitTest.pyTestEnabled": false

to

"python.unitTest.pyTestEnabled": true

Create a file called ‘pytest.ini’ in your work space root and add the following content to that file. Remember to change the myproject.settings to your actual settings module.


Well, That’s all the settings required to run django unit tests.  Click the ‘Run Tests’ button on the status bar and select ‘Run All Unit Tests’ from the context menu.

Tests will run and the result summary will be displayed on the output tab. If the test output is not visible, select ‘Run Tests’ and select ‘View Unit Test Output’. Summary of the tests can be viewed from the status bar as well.

 

You may also like...

1 Response

  1. November 20, 2017

    […] I was always impressed with the simplicity and usefulness of this extension and already wrote a blog post on that.  Don definitely deserve this recognition and I am using this opportunity to congratulate […]

Leave a Reply

Your email address will not be published.