Visual Studio Code and the Python extension provide a great interface for testing within the Test Explorer view. You can use Visual Studio Code to work with unit tests written in unittest, pytest, and nose. This chapter walks through unit testing for unittest and pytest with a simple example from the Python Koans repository.
The exercises within this chapter are completed within the Triangles
folder. The Triangles
folder contains two subfolders: unittest
and pytest
. Each subfolder contains code and a test file for the respective framework. Before proceeding with the exercises, open the subfolder for the preferred framework in Visual Studio Code and activate a virtual or conda environment for the workspace.
Unit testing in the Python extension is disabled by default. You must enable a test framework to run unit tests, and only a single test framework can be enabled at a time. (To switch frameworks, disable the current framework and enable the new one.)
To enable a test framework, complete the following:
Upon opening a project, if Visual Studio Code discovers potential tests, the editor prompts by default to configure a test framework. This is useful for a collaborator that has cloned the project and opens for the first time in Visual Studio Code. You could change this behavior in settings by setting python.testing.promptToConfigure
to false
.
Given that unittest is built into the standard Python framework, there is no further installation that needs to occur before using the framework. However, should you select to use pytest, Visual Studio Code prompts you to install the framework if the framework is not already installed. The prompt displays after the framework is enabled. If pytest is located outside the current environment, within the settings set python.testing.pytestPath
to the path to pytest. The default value is pytest
.
The structure of the project has a significant impact on how tests are discovered. For example, if your project structure organizes all tests into a singular folder and the code in a separate folder, specifying an optional working directory for tests would be optimal. The python.testing.cwd
setting enables you to specify such an optional working directory for tests. The default for the python.testing.cwd
setting is null
. To specify a directory, set python.testing.cwd
in the settings to the folder that contains the code files (e.g., if all code files are within a src
folder, cwd
would be set to src
).
Discovery patterns are dependent on the selected framework. The default behavior for unittest and pytest follow:
test
in the name in the top-level project folder. All test files must be importable modules or packages. To specify a discovery pattern for a specific naming convention (e.g., appending _test
to every test filename), change the pattern within settings for python.testing.unitTestArgs
. The default argument is -p *test*.py
..py
) file whose name begins with test_
or ends with _test
, located anywhere within the current folder and all subfolders.When a test is enabled, the python.testing.unittestEnabled
or python.testing.pyttestEnabled
setting is set to true
. The advantage of enabling a framework using the command Python: Configure Tests is that when a framework is enabled, the command automatically disables any other framework.
Once a framework is enabled, Visual Studio Code begins test discovery. Test discovery could also be triggered manually with the command Python: Discover Tests. If tests are found, the Status Bar shows “Run Tests” (see Figure 6.1). If discovery fails, the Status Bar shows “Test discovery failed.”
Try It Out: Enable either unittest or pytest and execute a test discovery for either test_unittest.py
or test_pytest
.
unittest:
test_*.py
pattern to identify test files (see Figure 6.4).
pytest:
test_*.py
pattern to identify test files (see Figure 6.7).
The Test Explorer view (see Figure 6.8) provides a convenient way for you to visualize, navigate, and run tests. The view becomes activated only after a test is discovered as a result of test discovery. Once activated, a Test Explorer icon is added to the Activity Bar.
The Test Explorer contains the following tasks at the top of the view:
The Run Test and Debug tasks also appear inline with each file, class, and test.
You can run a test in Visual Studio Code in various ways:
COMMAND | DESCRIPTION |
Run All Tests | Runs all tests in the workspace and its subfolders. |
Run Current Test File | Runs the test in the file that's currently active in the editor. |
Run Failed Tests | Reruns any tests that failed in a previous test run. Runs all tests if no tests have yet been run. |
Run Test File | Prompts for a specific test filename; then runs the test in that file. |
Run Test Method | Prompts for the name of a test to run, providing auto-completion for test names. |
Try It Out: Run all tests for the framework you enabled in the prior exercise. An intentional fail is included in the example to demonstrate a failed test.
Visual Studio Code displays test results in the following locations:
Try It Out: View the test results from the previous exercise. Although you might spot the error right away, you'll debug the assertion in the next section.
unittest:
Notice that a test failed within the def test_isosceles_triangle (self)
case for self.assertEqual(
'isosceles
', triangle(4, 5, 3))
.
pytest:
Notice that a test failed within the def test_isosceles_triangle
case for assert triangle(4, 5, 3) ==
'isosceles
'.
Debugging tests include the same functionality and commands used to debug Python files for test files. Debugging is often necessary if you suspect that your test has a bug. A breakpoint can be placed in the test file wherever desired prior to running the debugger. In addition to the Command Palette and Status Bar, you can start the debugger either within the editor or within the Test Explorer view.
Try It Out: Set a breakpoint in the test file for the test that is failing and run the debugger for the test. Though the bug may be obvious, the purpose is to use the debugger to illustrate the debugging process.
unittest:
self.assertEqual(
'isosceles', triangle(4, 5, 3))
.AssertionError
(see Figure 6.18).
test_unittest.py
, change self.assertEqual(
'isosceles
', triangle(4, 5, 3)) to self.assertEqual(
'isosceles
', triangle(4, 4, 3)
and save the file.test_unittest.py
should pass.pytest:
assert triangle(4, 5, 3) ==
'isosceles
'.test_unittest.py
, modify assert triangle(4, 5, 3) ==
'isosceles
' to assert triangle(4, 4, 3) ==
'isosceles
'test_unittest.py
should pass.In this chapter, you learned about the following:
settings.json
before running a test.At this stage, you are able to conduct unit testing for Python in Visual Studio Code.
3.16.130.201