Setting up unit testing for Python code
Unit tests are pieces of code that test other code units in an application, typically isolated functions, classes, and so on. When an application passes all its unit tests, you can at least trust that its low-level functionality is correct.
Python uses unit tests extensively to validate scenarios while designing a program. Python support in Visual Studio includes discovering, executing, and debugging unit tests within the context of your development process, without needing to run tests separately.
This article provides a brief outline of unit testing capabilities in Visual Studio with Python. For more on unit testing in general, see Unit test your code.
|Watch a video (Microsoft Virtual Academy) on unit testing in Python (2m 31s).|
Discovering and viewing tests
By convention, Visual Studio identifies tests are as methods whose names start with
test. To see this behavior, do the following:
Open a Python project loaded in Visual Studio, right-click your project, select Add > New Item..., then select Python Unit Test followed by Add.
This action creates a
test1.pyfile with code that imports the standard
unittestmodule, derives a test class from
unittest.TestCase, and invokes
unittest.main()if you run the script directly:
import unittest class Test_test1(unittest.TestCase): def test_A(self): self.fail("Not implemented") if __name__ == '__main__': unittest.main()
Save the file if necessary, then open Test Explorer with the Test > Windows > Test Explorer menu command.
Test Explorer searches your project for tests and displays them as shown below. Double-clicking a test opens its source file.
As you add more tests to your project, you can organize the view in Test Explorer using the group by menu on the toolbar:
You can also enter text in the search field to filter tests by name.
In Test Explorer you can run tests in a variety of ways:
- Run All clearly runs all shown tests (subject to filters).
- The Run... menu gives you commands to run failed, passed, or not run tests as a group.
- You can select one or more tests, right-click, and select Run Selected Tests.
Tests run in the background and Test Explorer updates each test's status as it completes:
Passing tests show a green rick and the time taken to run the test:
Failed tests show a red cross with an Output link that shows console output and
unittestoutput from the test run:
Because unit tests are pieces of code, they are subject to bugs just like any other code and occasionally need to be run in a debugger. In the debugger you can set breakpoints, examine variables, and step through code. Visual Studio also provides diagnostic tools for unit tests.
To start debugging, set an initial breakpoint in your code, then right-click the test (or a selection) in Test Explorer and select Debug Selected Tests. Visual Studio starts the Python debugger as it would for application code.
You can also use the Analyze Code Coverage for Selected Tests and Profile Test commands, depending on your version of Visual Studio (see the Features matrix).
- When starting debugging, Visual Studio appears to start and stop debugging, and then start again. This behavior is expected.
- When debugging multiple tests, each one is run independently, which interrupts the debugging session.
- Visual Studio intermittently fails to start a test when debugging. Normally, attempting to debug the test again succeeds.
- When debugging, it is possible to step out of a test into the
unittestimplementation. Normally, the next step runs to the end of the program and stop debugging.