This post explains how to set up VS Code to run Python unit tests
Install the Python extension
If you have not done so already install the Microsoft Python extension for VS Code
Python unit testing is turned off by default. To turn it on, bring up the VS Code Command pallet, on Windows, the keyboard shortcut for this is: ctrl + shift + p
Next enter the command Python:ConfigureTests and press enter.
This command first requests the unit testing framework to be used. The choice of framework determines which configuration options follow.
The configuration required when using the unittest framework are shown below. The steps for the popular pytest framework are similar except the option to specify the pattern to identify test files is not shown.
Select the testing framework:
Then select the location where the unit tests can be found:
The last step is the pattern used to identify test files:
The configuration is now complete and you will see a new Test icon on the VS Code Activity Bar as shown below with the red border:
Running the unit tests
Select the Test icon on the VS Code Activity Bar and then select the Discover Tests button from the toolbar, shown below with the red border.
After a short time the unit tests will appear. To run all the tests select the Run All Tests button, shown below with the red border.
Once run, each test is shown with either a passing green tick or a failed red cross.
Need further help?
Let me know via the comments.
The VS Code page on Python testing.
The excellent Pluralsight course Unit Testing with Python by Emily Bache
One thought on “Python Unit Testing with VS Code”