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.

pip install pytest
pip install pytest-django

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.

[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings

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.

 

2 thoughts on “Django Unit Testing in VSCode”

Leave a Reply

Your email address will not be published. Required fields are marked *