How to Run Pytest in VSCode for Testing

Summarize this article with:
Running tests manually gets old fast.
Learning how to run pytest in VSCode transforms your testing workflow from tedious command-line repetition into a streamlined, visual experience with click-to-run functionality and integrated debugging.
The pytest framework paired with Visual Studio Code’s Testing Explorer creates a powerful combination for Python developers.
You get instant feedback on test results, easy navigation to failing code, and breakpoint debugging without leaving your editor.
This guide walks through complete pytest configuration in VSCode: installing the Python extension, setting up your test environment, running tests from both the GUI and terminal, and debugging failures.
Takes about 15 minutes. Works on Windows, macOS, and Linux.
How to Run Pytest in VSCode

How to run pytest in VSCode is the process of configuring Visual Studio Code to discover, execute, and debug Python tests using the pytest framework.
Developers need this when building applications that require automated testing, running test suites during development, or practicing test-driven development.
This guide covers 8 steps requiring 10-15 minutes and basic Python knowledge.
Prerequisites
Before running pytest in VSCode, verify these requirements:
- Python 3.8+ installed on your system
- VSCode version 1.70 or later
- Basic familiarity with Python syntax
- A project folder with Python files
- Terminal access (command line)
Time estimate: 10-15 minutes for complete setup.
Skill level: Beginner to intermediate.
Step 1: How Do You Install the Python Extension in VSCode?
Install the Python extension by opening the Extensions marketplace in VSCode, searching for “Python” by Microsoft, and clicking Install. The extension enables Python interpreter selection, IntelliSense, and test discovery features required for pytest integration.
Action
Open VSCode and press Ctrl+Shift+X to access Extensions.
Type “Python” in the search bar.
Select the extension published by Microsoft (verified publisher badge).
Click Install and wait for completion.
The extension icon appears in the Activity Bar after installation.
Purpose
The Python extension provides the testing panel, debugger integration, and interpreter management. Without it, VSCode cannot recognize pytest or execute test files.
If you’re new to this editor, learn what VSCode is and its core features first.
Step 2: How Do You Install Pytest in Your Project?
Install pytest using pip in your project’s virtual environment by running pip install pytest in the integrated terminal. The package manager downloads pytest and its dependencies, making the test framework available for your Python project.
Action
Open the terminal with ` Ctrl+ ` or learn how to open terminal in VSCode.
Activate your virtual environment first. Need help? See how to activate venv in VSCode.
Run this command:
pip install pytest
Expected output: “Successfully installed pytest-8.x.x”
Verify installation with pytest –version.
Purpose
Pytest must exist in your environment before VSCode can detect it. Installing in a venv keeps dependencies isolated from system Python.
Step 3: How Do You Configure VSCode to Recognize Pytest?
Configure VSCode for pytest by opening Settings, navigating to Python > Testing, and selecting pytest as your test framework. VSCode stores this preference in settings.json and enables automatic test discovery across your workspace.
Action
Press Ctrl+, to open Settings.
Search for “python testing”.
Find “Python > Testing: Pytest Enabled” and check the box.
Set “Python > Testing: Pytest Args” if you need custom flags.
Alternatively, edit settings.json directly. Learn how to open settings.json in VSCode for manual configuration.
Add these lines:
` { "python.testing.pytestEnabled": true, "python.testing.unittestEnabled": false } `
Purpose
VSCode supports multiple types of software testing frameworks. Explicit configuration tells the editor which runner to use for test execution and discovery.
Step 4: How Do You Select the Correct Python Interpreter?
Select your project’s Python interpreter by pressing Ctrl+Shift+P, typing "Python: Select Interpreter", and choosing the environment where pytest is installed. The interpreter determines which Python version and packages VSCode uses for test execution.
Action
Press Ctrl+Shift+P to open Command Palette.
Type “Python: Select Interpreter”.
Choose the interpreter from your virtual environment (shows ./venv/bin/python or similar).
The selected interpreter appears in the status bar.
For detailed steps, read how to change Python interpreter in VSCode.
Purpose
Wrong interpreter selection causes “pytest not found” errors. The interpreter must match the environment where you installed the pytest package.
Step 5: How Do You Create a Test File for Pytest?
Create a test file by making a new Python file with the test prefix (like testcalculator.py), then write test functions starting with test. Pytest uses these naming conventions for test collection and automatic discovery.
Action
Create a new file: testexample.py
Add a basic test function:
` def testaddition(): assert 1 + 1 == 2
def teststringlength(): assert len(“hello”) == 5 `
Save the file in your project root or a tests/ directory.
File naming: must start with test or end with test.py.
Function naming: must start with test.
Purpose
Pytest scans for specific patterns during test discovery. Files or functions without proper naming conventions get skipped by the test runner.
Following these conventions aligns with software development best practices for maintainable test suites.
Step 6: How Do You Run Pytest Using the Testing Panel?
Run pytest through the Testing panel by clicking the flask icon in the Activity Bar, then selecting “Configure Python Tests” and choosing pytest. VSCode performs test discovery and displays all detected tests in a tree structure for execution.
Action
Click the flask icon (beaker) in the left Activity Bar.
Select “Configure Python Tests” if prompted.
Choose “pytest” as the framework.
Select your project root as the test directory.
Click the play button to run all tests, or expand the tree and run individual test files.
Purpose
The Testing Explorer provides visual feedback with green checkmarks for passed tests and red X marks for failures. Faster than command line for iterative development.
Step 7: How Do You Run Pytest From the Terminal in VSCode?
Execute pytest from the integrated terminal by typing pytest and pressing Enter. The command line runner discovers all test files, executes them sequentially, and outputs results with pass/fail status and execution time.
Action
Open terminal: Ctrl+ `
Run basic test execution:
pytest
Common pytest flags:
- pytest -v
for verbose output
- pytest -s
to show print statements
- pytest –tb=short
for shorter tracebacks
- pytest -x
to stop on first failure
Purpose
Terminal execution offers more control over pytest commands and flags. Better for CI/CD pipelines and continuous integration workflows.
Step 8: How Do You Run a Single Test Function?
Run a single test by clicking the green play icon next to the test function in the editor, or use the command pytest testfile.py::testfunctionname in terminal. Both methods execute only the specified test function without running the entire suite.
Action
Method 1 (GUI): Click the green triangle next to any def test line in your editor.
Method 2 (Terminal):
pytest testexample.py::testaddition
Method 3 (Testing Panel): Expand the test tree and click play on a specific test.
Purpose
Running individual tests speeds up debugging cycles. No need to wait for the full test suite when fixing a single failing test.
Step 9: How Do You Debug Pytest Tests in VSCode?
Debug pytest tests by setting breakpoints in your test code, then right-clicking the test and selecting “Debug Test”. VSCode launches the debugger, pauses at breakpoints, and provides access to the Debug Console for inspecting variables and stepping through code.
Action
Click in the gutter (left of line numbers) to set a breakpoint.
Right-click on the test function name.
Select “Debug Test” from the context menu.
Use debug controls: Step Over (F10), Step Into (F11), Continue (F5).
Inspect variables in the Variables panel or Debug Console.
For broader debugging techniques, see how to debug Python in VSCode.
Purpose
Debugging reveals why tests fail by letting you examine program state at runtime. Part of effective defect tracking during development.
Verification
Confirm pytest runs correctly in VSCode by checking these indicators:
- Testing panel shows discovered tests in tree format
- Green checkmarks appear for passing tests
- Terminal output displays “X passed” message
- Red indicators show for intentionally failing tests
- Test results appear in the Output panel under “Python Test Log”
Run a quick verification test:
` def testverification(): assert True `
This should show one passed test in both the Testing panel and terminal output.
Troubleshooting
Issue: Pytest Not Discovered by VSCode
Solution: Verify pytest is installed in the selected interpreter environment. Open terminal, run pip show pytest. If not found, install it. Check that "python.testing.pytestEnabled" is true in settings.json.
Issue: Import Errors When Running Tests
Solution: Add an empty init.py file to your test directory. Create a conftest.py at project root with path configuration. Check your PYTHONPATH includes the project directory.
Issue: Tests Not Appearing in Testing Panel
Solution: Verify file names start with test or end with test.py. Confirm function names begin with test. Click "Refresh Tests" in the Testing panel. Check Output > Python Test Log for discovery errors.
Issue: Wrong Python Interpreter Selected
Solution: Press Ctrl+Shift+P, type "Python: Select Interpreter", choose the venv where pytest is installed. Restart VSCode if the issue persists.
Issue: Tests Pass in Terminal but Fail in VSCode
Solution: Check workspace settings override user settings. Verify both use identical Python interpreters. Review pytest.ini or pyproject.toml for conflicting configurations.
Related Processes
After mastering pytest in VSCode, explore these connected workflows:
- Unit testing fundamentals and best practices
- Code coverage measurement with pytest-cov
- Integration testing for larger system components
- Mocking in unit tests for isolating dependencies
- Regression testing to catch bugs after code changes
- Behavior-driven development with pytest-bdd
Understanding the full software testing lifecycle helps position pytest within your overall software quality assurance process.
For Python development workflows, also check how to run Python in VSCode and how to format code in VSCode using linters and formatters.
FAQ on How To Run Pytest In VSCode
Why is pytest not showing in VSCode?
Pytest won’t appear if it’s not installed in your selected Python interpreter or if testing is disabled in settings. Verify installation with pip show pytest in terminal, then enable "Python > Testing: Pytest Enabled" in VSCode settings.
How do I enable pytest in VSCode settings?
Open Settings with Ctrl+,, search "python testing", and check "Pytest Enabled". Alternatively, add “python.testing.pytestEnabled”: true to your settings.json file. Disable unittest if both are enabled to avoid conflicts.
Can I run pytest with verbose output in VSCode?
Yes. Add “-v” to "Python > Testing: Pytest Args" in settings, or run pytest -v directly in the integrated terminal. Verbose output displays individual test names and their pass/fail status during execution.
How do I run only failed tests in VSCode?
Click the “Run Failed Tests” button in the Testing panel toolbar after initial execution. From terminal, use pytest –lf (last failed) to rerun only tests that failed in the previous session.
Why does pytest say no tests found?
Test discovery requires specific naming conventions. Files must start with test or end with test.py. Functions need the test prefix. Check that your test directory is included in pytest's search path.
How do I debug a single pytest test in VSCode?
Set a breakpoint by clicking the editor gutter, then right-click on the test function and select “Debug Test”. VSCode launches the debugger, pausing execution at your breakpoint for variable inspection.
Can I use pytest fixtures in VSCode?
Yes. Define fixtures in conftest.py or within test files using the @pytest.fixture decorator. VSCode's test discovery recognizes fixtures automatically, and they work identically whether running from the Testing panel or terminal.
How do I configure pytest.ini for VSCode?
Create a pytest.ini file in your project root with configuration options like testpaths, markers, or command-line defaults. VSCode reads this file automatically during test collection and applies your specified settings.
Why are my pytest tests running slowly in VSCode?
Slow execution often results from missing init.py files causing repeated imports, or test discovery scanning unnecessary directories. Add testpaths in pytest.ini to limit discovery scope and speed up your test runner.
How do I see pytest print statements in VSCode?
Add “-s” to pytest args in settings, or run pytest -s from terminal. By default, pytest captures stdout. The -s flag disables capture, displaying print statements in the test output panel.
Conclusion
You now know how to run pytest in VSCode from installation through debugging.
The combination of pytest’s powerful test framework and VSCode’s integrated testing panel creates an efficient workflow for automated testing.
Start simple. Write a few test functions, run them from the GUI, get comfortable with the interface.
Then expand into test fixtures, pytest markers, and conftest.py configurations as your test suite grows.
Add pytest-cov for code coverage reports. Explore parametrization for data-driven tests.
The testing workflow you build today scales with your project tomorrow.
Keep your test configuration clean, name your files correctly, and let VSCode handle the discovery. That’s really all there is to it.
- React UI Component Libraries Worth Exploring - February 10, 2026
- The Communication Gap That Kills Outsourcing Efficiency - February 10, 2026
- React Testing Libraries Every Dev Should Know - February 9, 2026







