Unit test Python code with Test Explorer - Visual Studio (Windows) (original) (raw)

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 be confident that at least the low-level program 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.

Prerequisites

Visual Studio for Mac isn't supported. For more information, see What's happening to Visual Studio for Mac? Visual Studio Code on Windows, Mac, and Linux works well with Python through available extensions.

Select test framework for a Python project

Visual Studio supports two testing frameworks for Python, unittest and pytest (available in Visual Studio 2019 version 16.3 and later). By default, no framework is selected when you create a Python project.

Follow these steps to select the test framework for your Python project:

  1. In Solution Explorer, right-click the project name and select Properties.
  2. In the project Properties pane, select the Test tab and choose your Test framework type:
    • For the unittest framework, Visual Studio assigns the project's Root directory for test discovery. The default value is ., but you can specify a different location when you configure the project settings. You can also specify one or more strings for the test filename Pattern, such as test*.py, test_*.py.
    • For the pytest framework, testing options like test location and filename patterns are specified by using the standard pytest .ini configuration file. By default, the workspace/project folder is used for the location. The default filename pattern includes test_*py and *_test.py. For more information, see the pytest reference documentation.
      Note
      When you define the filename pattern, keep in mind that special characters like the underscore (_) aren't matched by the wildcard (*). If you want to use special characters in the filename, specify these characters in the pattern definition, such as test_*.py.
  3. To save your framework selection and settings, you can use the Ctrl+S keyboard shortcut.

After you configure the framework, Visual Studio initiates test discovery and opens Test Explorer.

Configure testing for Python without a project

Visual Studio allows you to run and test existing Python code without a project, by opening a folder with Python code. In this scenario, you need to use a PythonSettings.json file to configure testing.

  1. Open your existing Python code by using the Open a Local Folder option:
    Screenshot that shows how to select the Open a local folder option when Visual Studio 2022 starts.
    Screenshot that shows how to select the Open a local folder option when Visual Studio starts.
  2. When you open a Python folder, Visual Studio creates several hidden folders to manage settings related to the program. To see these folders (and any other hidden files and folders, such as the .git folder) in Solution Explorer, select the Show All Files option:
    Screenshot that shows how to view hidden folders and files in Solution Explorer in Visual Studio 2022.
    Screenshot that shows how to view hidden folders and files in Solution Explorer in Visual Studio.
  3. In Solution Explorer, expand the Local Settings folder, and double-click the PythonSettings.json file to open the file in the editor.
    Note
    Most configurations show two settings files: PythonSettings.json and ProjectSettings.json. For this exercise, you need to modify the PythonSettings.json file.
    If you don't see the PythonSettings.json file in the Local Settings folder, you can create it manually:
    1. Right-click the Local Settings folder and select Add > New File.
    2. Name the file PythonSettings.json, and select Enter to save your changes.
      Visual Studio automatically opens the new file in the editor.
  4. In the PythonSettings.json file, add the following code to define the TestFramework. Set the framework value to pytest or unittest depending on your desired testing framework:
{  
  "TestFramework": "unittest",  
  "UnitTestRootDirectory": "testing",  
  "UnitTestPattern": "test_*.py"  
}  
  1. If your Python program contains a src folder separate from the folder that contains your tests, specify the path to the src folder with the SearchPaths setting in the PythonSettings.json file:
  "SearchPaths": [".\\src"]  
  1. Save your changes to the PythonSettings.json file.

After you configure the framework, Visual Studio initiates test discovery for the specified framework. You can access the test in Test Explorer.

Add and discover tests

By default, Visual Studio identifies unittest and pytest tests as methods whose names start with test.

To see how Visual Studio initiates test discovery, follow these steps:

  1. Open a Python project in Visual Studio.
  2. Set the test framework Properties for the project, as described in Select test framework for Python project.
  3. In Solution Explorer, right-click the project and select Add > New Item.
    1. In the Add New Item dialog, select the Python Unit Test file type.
    2. Enter a filename that satisfies the Pattern definition you specified for the project Properties.
    3. Select Add.
  4. Visual Studio creates the test file with default code:
import unittest  
class Test_test1(unittest.TestCase):  
    def test_A(self):  
        self.fail("Not implemented")  
if __name__ == '__main__':  
    unittest.main()  

This code imports the standard unittest module and derives a test class from the unittest.TestCase method. When you run the script directly, this code also invokes the unittest.main() function.

When you add new test files, Visual Studio makes them available in Test Explorer.

View tests with Test Explorer

After you configure your test framework and test files, Visual Studio searches for tests and displays them in Test Explorer.

Here are some of the ways you can work with Test Explorer:

For more information on the unittest module and writing tests, see the Python documentation.

Run tests with Test Explorer

In Test Explorer, you can run tests in several ways:

Visual Studio runs your tests in the background. Test Explorer updates the status for each test as it completes:

Check tests with the Debugger

Unit tests are pieces of code that are subject to bugs like any other code and occasionally need to be run in a debugger. In the Visual Studio Debugger, you can set breakpoints, examine variables, and step through code. Visual Studio also provides diagnostic tools for unit tests.

Review these points about checking your tests with the Visual Studio Debugger: