Menu Home

Python Unit Testing with VS Code

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

Configuration

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.

Acknowledgements

The VS Code page on Python testing.

The excellent Pluralsight course Unit Testing with Python by Emily Bache

Categories: Python VS Code

oraclefrontovik

Developer

1 reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.