Author's Picture
Author: Joel Gray Published: 18 November 2022 Read Time: ~5 minutes

How to set up Testing Explorer with Python Pytest in VSCode

VSCode contains a testing explorer feature that seems to be under utilised from my experience but it’s extremely useful and making running specific tests a breeze. The one issue is it can be tricky to set up and the VSCode documentation leaves much to be desired. So we’re going to run through how to set up the VSCode Test Explorer to discover Python tests and how to execute them with Pytest.

Open VSCode and open your project in it’s root directory.

If you cannot see the Testing tab icon on the left handside toolbar that looks like this:

You can activate it by pressing: Ctrl+Shift+P and searching for “test”, it will give you the following options:

Select: “Python: Configure Tests”.
This will bring up the Testing Tab on the lefthand side toolbar,

You may get an error saying there is a missing command this is because the testing area isn’t set up just yet, but just hit okay and VSCode will fix this in the background.

Just run it again, it should work this time.

Next you will be prompted to select which Python test framework you wish to run your tests with. In this case we want to select Pytest from the drop down.

Next we want to select the directory that contains our unit tests, I find that VSCode struggles to find Python tests if you search from the root directory of your project. It’s much better at finding tests if you select a sub folder that contains your tests.

For example in my project, I have a ‘common’ folder that contains files I need to share with other services, and then a ‘modules’ folder which contains all my Python logic, along with my project unit tests. So I will select ‘modules’ as my test directory.

If you now click back onto the Testing sidebar, you’ll be able to see whether or not VSCode has been able to find your tests. If it has – brilliant! Although I’ve never seen it find the tests first time around sadly.

It’s much more likely you’ll find an error such as this:

You can view the output logs if you like but it’s just going to say that while parsing the tests that it had errors importing the modules from your tests. The reason for this is that VSCode has no Python environment path set, and doesn’t know where it’s importing these modules from.

So we need o tell Python where to look for it’s imports. Simply create a file at the root of your directory called ‘.env’ and give it the local full path to your selected test directory, for me it’s the full local path to my ‘modules’ folder. Here’s what mine looks like:

If you want an easy way to find this path, open the terminal in the VSCode window where you have your project open. You can do this by pressing: Ctrl + Shift + ` on Linux and Ctrl + Shift + ‘ on Windows.

Then cd into the test directory you selected earlier and issues the pwd command:

There should now exist a ‘.vscode’ folder in the root of your project. This is an auto generated configuration folder for VSCode settings which can contain files such as ‘settings.json’ and ‘tasks.json’.

We’re going to make some changes to the ‘settings.json’ folder to allow VSCode to find our ‘.env’ file. See my example settings file below:

Note: In the ‘python.testing.pytestArgs’ list, you can add any normal pytest arguments you may use such as: ‘-vv’, ‘–ignore=path/to/unwanted/test’ for example.

The last issue you may run into is that if you normally execute your program in a virtual environment, you will need to pip install all your project requirements on your local instance of Python, so that VSCode can find the packages to import when running the tests. This is because VSCode will run the tests locally.

I realise this is not ideal but this is the quickest and easiest way to get Pytest to integrate with VSCode using Test Explorer. There are other extensions that allow you to run these tests in a virtual environment but I’ve found them much more complex to set up. I would advise before any release to run all tests from your virtual environment using pytest command line, but this is the quickest and easiest way to run them on the fly while developing.

To install the requirements locally, you must cd into the folder containing your requirements.txt file and issue the command: ‘pip install -r requirements.txt’. If pip is not on your PATH you may need to run ‘python -m pip install -r requirements.txt’.

Now if you go back to your VSCode Python Test Explorer tab on the left hand side and refresh the test explorer using the circular arrow icon on the top side of the Testing tab, you should no longer see any errors but a list of all your tests. Similar to the image below:

We can execute these Python tests with Pytest in groups or individually and the results will show with a green or red icon in the Test Explorer to indicate if the last run of the test passed or failed.

If your tests are failing, you can run them with debug by pressing the play button with the little bug icon on is as show above, this will display the output of the test in the Debug Console for you to investigate more (shown below). I would advise running the tests with ‘-vv’ if they’re failing, to gather as much information as possible to find the issue.

If you have any questions or comments please leave them below or send me an email.

Written by Joel Gray

18/11/2022

Checkout some of our other popular blog posts

How to install .deb Packages on Arch Linux: DevKinsta as an Example

Are you looking to dive into the world of Python programming but unsure how...

1 2 3 10