A Simple PyTest Setup in VS Code
Unit testing is essential to development, whether it be applications or data pipelines. Visual Studio Code (VS Code) like most IDE’s…
Unit testing is essential to development, whether it be applications or data pipelines. Visual Studio Code (VS Code) like most IDE’s provides a nice and easy way of performing these tests within the IDE rather than relying on the terminal. Therefore, this is a simple guide on setting up VS Code to perform Python unit tests aimed at people who are new to VS Code or even testing in general, it should help you get started with a simple test before you move on to digging deeper into the tests that you require.
An example VS Code Setup
Below is an simple example directory structure to setup Pytest in VS Code.
vscode-pytest
|__.vscode
|____ settings.json
|__ src
|____ main
|______ hello_world.py
|____ test
|______ test_hello_world.py
|__ .env
In this directory structure vscode-pytest is the root of the project with two subdirectories, src and .vscode and an environment file (.env) that will be used by VS Code to define the Python path for the environment you will be working in. Populating this .env file with the PYTHONPATH variable is critical if you want to import your modules contained in the main directory or any other directory into your tests, the contents of the .env file is shown below.
PYTHONPATH=.
If this is not done you will receive an error similar to that shown below.
ModuleNotFoundError: No module named 'src'
In the .vscode directory there is a settings.json file that sets up VS Code, in this case for testing so it enables pytest and specifies the location to start looking for tests, the contents of this file is shown below.
{
"python.testing.pytestArgs": [
"src"
],
"python.testing.pytestEnabled": true
}
Let’s now take a look at the contents of the .py file starting with hello_world.py in the vscode-pytest/src/main directory, this has a simple function shown below that simply returns the string ‘hello world’.
def return_hello_world():
return 'hello world'
The next .py file test_hello_world.py in the vscode-pytest/src/test directory contains a unit test for the function defined in the hello_world module, the code is illustrated below.
from src.main.hello_world import return_hello_world
def test_return_hello_world():
assert return_hello_world() == 'hello world'
The first line in the code above shows that only the function to be tested is being imported from the module and the code then defines a function that tests the imported function by simply asserting that the two strings are the same.
Running the example test
Running these tests can be done in a few different ways a couple of them are:
The play icon in the test file, shown below.
2. Or by navigating to the testing section represented by a test tube. This will allow you to see all the tests and select whether you wish to run a single test or all of them, an illustration is shown below.
As can be seen by figure 2 the single test in test_hello_world.py ran successfully.
Hopefully this simple guide, has helped you setup your VS Code environment to run unit tests with PyTest in VS Code.