Skip to main content This browser is no longer supported. Show Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Set up unit testing for Python code
In this articleApplies to: Visual Studio Visual Studio for Mac Visual Studio CodeUnit 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. Select the test framework for a Python projectVisual Studio supports two testing frameworks for Python, unittest and pytest (available in Visual Studio 2019 starting with version 16.3). By default, no framework is selected when you create a Python project. To specify a framework, right-click on the project name in Solution Explorer and select the Properties option. This opens the project designer, which allows you to configure tests through the Test tab. From this tab, you can select the test framework that you want to use for your project.
Once you've saved your framework selection and settings, test discovery is initiated in the Test Explorer. If the Test Explorer window is not already open, navigate to the toolbar and select Test > Test Explorer. Configure testing for Python without a projectVisual Studio allows you to run and test existing Python code without a project, by opening a folder with Python code. Under these circumstances, you'll need to use a PythonSettings.json file to configure testing.
Discover and view testsBy default, Visual Studio identifies unittest and pytest tests as methods whose names start with
For more information on the Run testsIn Test Explorer you can run tests in a variety of ways:
Tests run in the background and Test Explorer updates each test's status as it completes:
Debug testsBecause 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. Note By default, test debugging uses the ptvsd 4 debugger for Visual Studio 2017 (versions 15.8 and later) and debugpy for Visual Studio 2019 (versions 16.5 and later). If you would like to instead use ptvsd 3, you can select the Use Legacy Debugger option on Tools > Options > Python > Debugging. 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. For more information, see Use code coverage to determine how much code is tested. FeedbackSubmit and view feedback for |