Documentation
Topics Overview Overview Linux macOS Windows VS Code for the Web Raspberry Pi Network Additional Components Uninstall VS Code Tutorial Copilot Quickstart User Interface Personalize VS Code Install Extensions Tips and Tricks Intro Videos Overview Setup Quickstart Overview Language Models Context Tools Agents Customization Trust & Safety Overview Agents Tutorial Agents Window Planning Memory Tools Subagents Local Agents Copilot CLI Cloud Agents Third-Party Agents Overview Chat Sessions Add Context Inline Chat Review Edits Checkpoints Artifacts Panel Debug Chat Interactions Prompt Examples Overview Instructions Prompt Files Custom Agents Agent Skills Language Models MCP Hooks Plugins Context Engineering Customize AI Test-Driven Development Edit Notebooks with AI Test with AI Test Web Apps with Browser Tools Debug with AI MCP Dev Guide OpenTelemetry Monitoring Inline Suggestions Smart Actions Best Practices Security Troubleshooting FAQ Cheat Sheet Settings Reference MCP Configuration Workspace Context Display Language Layout Keyboard Shortcuts Settings Settings Sync Extension Marketplace Extension Runtime Security Themes Profiles Overview Voice Interactions Command Line Interface Telemetry Basic Editing IntelliSense Code Navigation Refactoring Snippets Overview Multi-Root Workspaces Workspace Trust Tasks Debugging Debug Configuration Testing Port Forwarding Integrated Browser Overview Quickstart Staging & Committing Branches & Worktrees Repositories & Remotes Merge Conflicts Collaborate on GitHub Troubleshooting FAQ Getting Started Tutorial Terminal Basics Terminal Profiles Shell Integration Appearance Advanced Overview Enterprise Policies AI Settings Extensions Telemetry Updates Overview JavaScript JSON HTML Emmet CSS, SCSS and Less TypeScript Markdown PowerShell C++ Java PHP Python Julia R Ruby Rust Go T-SQL C# .NET Swift Working with JavaScript Node.js Tutorial Node.js Debugging Deploy Node.js Apps Browser Debugging Angular Tutorial React Tutorial Vue Tutorial Debugging Recipes Performance Profiling Extensions Tutorial Transpiling Editing Refactoring Debugging Quick Start Tutorial Run Python Code Editing Linting Formatting Debugging Environments Testing Python Interactive Django Tutorial FastAPI Tutorial Flask Tutorial Create Containers Deploy Python Apps Python in the Web Settings Reference Getting Started Navigate and Edit Refactoring Formatting and Linting Project Management Build Tools Run and Debug Testing Spring Boot Modernizing Java Apps Application Servers Deploy Java Apps GUI Applications Extensions FAQ Intro Videos GCC on Linux GCC on Windows GCC on Windows Subsystem for Linux Clang on macOS Microsoft C++ on Windows Build with CMake CMake Tools on Linux CMake Quick Start C++ Dev Tools for Copilot Editing and Navigating Debugging Configure Debugging Refactoring Settings Reference Configure IntelliSense Configure IntelliSense for Cross-Compiling FAQ Intro Videos Get Started Navigate and Edit IntelliCode Refactoring Formatting and Linting Project Management Build Tools Package Management Run and Debug Testing FAQ Overview Node.js Python ASP.NET Core Debug Docker Compose Registries Deploy to Azure Choose a Dev Environment Customize Develop with Kubernetes Tips and Tricks Overview Jupyter Notebooks Data Science Tutorial Python Interactive Data Wrangler Quick Start Data Wrangler PyTorch Support Azure Machine Learning Manage Jupyter Kernels Jupyter Notebooks on the Web Data Science in Microsoft Fabric Foundry Toolkit Overview Foundry Toolkit Copilot Tools Create Agents Models Playground Agent Builder Agent Inspector Evaluation Tool Catalog Fine-Tuning (Automated Setup) Fine-Tuning (Project Template) Model Conversion Tracing Profiling (Windows ML) FAQ File Structure Manual Model Conversion Manual Model Conversion on GPU Setup Environment Without Foundry Toolkit Template Project Migrating from Visualizer to Agent Inspector Overview Getting Started Resources View Deployment VS Code for the Web - Azure Containers Azure Kubernetes Service Kubernetes MongoDB Remote Debugging for Node.js Overview SSH Dev Containers Windows Subsystem for Linux GitHub Codespaces VS Code Server Tunnels SSH Tutorial WSL Tutorial Tips and Tricks FAQ Overview Tutorial Attach to Container Create Dev Container Advanced Containers devcontainer.json Dev Container CLI Tips and Tricks FAQ Default Keyboard Shortcuts Default Settings Substitution Variables Tasks SchemaOn this page there are 10 sections
Python testing in Visual Studio Code
The Python extension builds on the built-in testing features in VS Code and provides test discovery, test coverage, and running and debugging tests for Python's built-in unittest framework and pytest.
Configure tests
When the Python extension is installed and a Python file is open within the editor, a test beaker icon displays on the VS Code Activity Bar representing the Test Explorer view. Opening the Test Explorer shows a Configure Python Tests button if a test framework is not enabled. Selecting Configure Python Tests prompts you to select a test framework and a folder containing the tests. If you use unittest, you also select the file glob pattern used to identify your test files.
A file glob pattern is a defined string pattern that matches file or folder names based on wildcards to include or not include files.
Tests can be configured anytime by using the Python: Configure Tests command from the Command Palette or by setting either python.testing.unittestEnabled or python.testing.pytestEnabled in the Settings editor or settings.json file as described in the VS Code Settings documentation. Each framework also has specific configuration settings as described under Test configuration settings for their folders and patterns.
If you enable pytest and it is not currently installed in the activated environment, the Python extension attempts to install it in the background. Furthermore, if both frameworks are enabled, the Python extension only runs pytest.
The minimum supported version of pytest for the python extension is 7.0.0.
Test discovery
By default, the Python extension attempts to discover tests once you enable a framework. You can trigger test discovery at any time using the Test: Refresh Tests command from the Command Palette.
python.testing.autoTestDiscoverOnSaveEnabled is set to true by default, meaning that test discovery occurs automatically whenever you add, delete, or update any Python file in the workspace. To disable this feature, set the value to false, which can be done either in the Settings editor or in the settings.json file as described in the VS Code Settings documentation. You need to reload the window for this setting to take effect. For more control over files included in auto test discovery, adjust the python.testing.autoTestDiscoverOnSavePattern setting, which defaults to **/*.py.
Test discovery applies the discovery patterns for the current framework (which can be customized using the Test configuration settings). The default behavior is as follows:
-
python.testing.unittestArgs: Looks for any Python (.py) file with "test" in the name in the top-level project folder. All test files must be importable modules or packages. You can customize the file matching pattern with the -p configuration setting, and customize the folder with the -t setting.
-
python.testing.pytestArgs: Looks for any Python (.py) file whose name begins with "test_" or ends with "_test", located anywhere within the current folder and all subfolders.
Sometimes tests placed in subfolders aren't discovered because such test files cannot be imported. To make them importable, create an empty file named __init__.py in that folder.
If the test discovery succeeds, you'll see tests listed in the Test Explorer:
When triggering test discovery directly from the Test Explorer, you can also cancel an ongoing test discovery call. Use the Cancel button, which replaces the Refresh button during discovery.
If discovery fails (for example, the test framework isn't installed or you have a syntax error in your test file), you'll see an error message displayed in the Test Explorer including a link to the logs. You can check the Python output panel to see the entire error message (use the View > Output menu command to show the Output panel, then select Python from the dropdown on the right side).
Run tests
You can run tests using any of the following actions:
-
With a test file open, select the green run icon that is displayed in the gutter next to the test definition line, as shown in the previous section. This command runs only that one method.
-
From the Command Palette, by running any of the following commands:
- Test: Run All Tests - Runs all tests that have been discovered.
- Test: Run Tests in Current File - Runs all tests in a file that is open in the editor.
- Test: Run Test at Cursor - Runs only the test method under your cursor in the editor.
-
From the Test Explorer:
-
To run all discovered tests, select the play button at the top of Test Explorer:
-
To run a specific group of tests, or a single test, select the file, class, or test, then select the play button to the right of that item:
-
You can also run a selection of tests through the Test Explorer. To do that, Ctrl+Click (or Cmd+Click on macOS) on the tests you wish to run, right-click on one of them and then select Run Test.
-
After a test run, VS Code displays results directly in the editor as gutter decorations. Failed tests will also be highlighted in the editor, with a Peek View that displays the test run error message and a history of all of the tests' runs. You can press Escape to dismiss the view, and you can disable it by opening the User settings (Preferences: Open Settings (UI) command in the Command Palette) and changing the value of the Testing: Automatically Open Peek View setting to never.
In the Test Explorer, results are shown for individual tests and any classes and files containing those tests. Folders will display a failure icon if any of the tests within that folder did not pass.
VS Code also shows test results in the Test Results panel.
Run tests with coverage
To run tests with coverage enabled, select the coverage run icon in the Test Explorer or the Run with coverage option from any menu you normally trigger test runs from. The Python extension runs coverage using the pytest-cov plugin if you are using pytest, or with coverage.py for unittest.
Before running tests with coverage, make sure to install the correct testing coverage package for your project. Branch coverage is only supported on coverage versions >= 7.7.
Once the coverage run completes, lines are highlighted in the editor for line-level coverage. Test coverage results appear as a "Test Coverage" sub-tab in the Test Explorer, which you can also navigate to with Testing: Focus on Test Coverage View in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). On this panel, you can view line coverage metrics for each file and folder in your workspace, as well as branch coverage, if relevant.
For finer grain control of your coverage run when using pytest, you can edit the python.testing.pytestArgs setting to include your specifications. When the pytest argument --cov exists in python.testing.pytestArgs, the Python extension will make no additional edits to coverage args, to allow your customizations to take effect. If there is no --cov argument found, the extension will add --cov=. to the pytest args prior to run to enable coverage at the workspace root.
For more information on test coverage, visit VS Code's Test Coverage documentation.
Debug tests
To debug tests, right-click on the gutter decoration next to the function definition and select Debug Test, or select the Debug Test icon next to that test in the Test Explorer.
Running or debugging a test does not automatically save the test file. Always be sure to save changes to a test before running it, otherwise you'll likely be confused by the results because they still reflect the previous version of the file!
You can use the following commands from the Command Palette to debug tests:
- Test: Debug All Tests - Launches the debugger for all tests in your workspace.
- Test: Debug Tests in Current File - Launches the debugger for the tests you have defined in the file you have open in the editor.
- Test: Debug Test at Cursor - Launches the debugger only for the method where you have your cursor focused on the editor. You can also use the Debug Test icons in Test Explorer to launch the debugger for all tests in a selected scope and all discovered tests.
You can also change the default behavior of clicking on the gutter decoration to debug tests instead of run, by changing the testing.defaultGutterClickAction setting value to debug in your settings.json file.
The debugger works the same for tests as for other Python code, including breakpoints, variable inspection, and so on. To customize settings for debugging tests, you can specify test debug configs in either the launch.json or settings.json files in the .vscode folder from your workspace by adding the "purpose": ["debug-test"] to your config. This configuration will be used when you run Test: Debug All Tests, Test: Debug Tests in Current File and Test: Debug Test at Cursor commands.
For example, the configuration below in the launch.json file disables the justMyCode setting for debugging tests: