How to debug unit tests in VSCode
Create or modify launch.json in VSCode by opening the Run and Debug view, selecting your environment (e.g., Python, Node.js), and configuring it for your test framework (e.g., pytest, Jest). 2. Set breakpoints in your test file, select the debug configuration, and start debugging with F5 to pause at breakpoints, inspect variables, and step through code. 3. Debug specific tests by targeting individual test functions in args for Python (e.g., "tests/test_models.py::test_create_user") or using test.only in Jest to run only the focused test. 4. Use the VSCode Testing sidebar to view and debug tests directly if your framework is detected and proper extensions (e.g., Python extension, Jest Runner) are installed, avoiding manual launch.json setup in many cases. Common tips include setting "justMyCode": false, ensuring correct test file naming (e.g., test_.py, .test.js), using --runInBand for Jest, and inserting debugger; statements in JavaScript for manual pause points, ensuring breakpoints are hit and tests are discovered properly.
Debugging unit tests in VSCode is straightforward once you set up the environment correctly. Here's how to do it effectively, especially for common languages like Python, JavaScript/TypeScript, or C#.

✅ 1. Set up a debug configuration for your tests
VSCode uses launch.json
to define debugging configurations. You need to create or modify this file to run and debug your unit tests.
Steps:
- Open the Run and Debug view (Ctrl Shift D or Cmd Shift D).
- Click "create a launch.json file" if you don’t have one.
- Choose your environment (e.g., Python, Node.js, .NET).
Example: Python with unittest
or pytest
{ "version": "0.2.0", "configurations": [ { "name": "Debug Python Unit Test", "type": "python", "request": "launch", "program": "${workspaceFolder}/-m", "args": [ "pytest", "tests/test_example.py::test_something", "-v" ], "console": "integratedTerminal", "justMyCode": false } ] }
Tip: Replace
pytest
withunittest
if using the built-in module, and adjust the path and test name accordingly.
Example: Node.js with Jest
{ "name": "Debug Jest Test", "type": "node", "request": "launch", "runtimeExecutable": "npm", "runtimeArgs": ["run", "test:debug"], "port": 9229, "console": "integratedTerminal", "skipFiles": ["<node_internals>/**"] }
You’d also need to add a script in package.json
:
"scripts": { "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand" }
✅ 2. Add breakpoints and start debugging
- Open the test file you want to debug.
- Click the left margin (next to line numbers) to set a breakpoint.
- Go to the Run and Debug panel.
- Select your test configuration from the dropdown.
- Click the green "Run" button (or press F5).
The test will run under the debugger and pause at your breakpoint.

You can then:
- Inspect variables in the VARIABLES pane
- Step over/into/through code (F10, F11, Shift F11)
- Use the Debug Console to evaluate expressions
✅ 3. Debug specific tests (avoid running all tests)
To save time, only debug the test you're working on.
Python (pytest):
Use the full path to the test function:
"args": [ "tests/test_models.py::test_create_user", "-v" ]
JavaScript (Jest):
Use .only
in your test:
test.only('should return true', () => { // only this test runs });
Then start the Jest debug session.
✅ 4. Use the Testing sidebar (VSCode's built-in test explorer)
VSCode has a Testing sidebar (left panel, triangle icon) that shows all discovered tests.
- Make sure your test framework is properly detected (e.g., pytest, Jest).
- Hover over a test and click the bug icon to debug it directly.
- This skips needing to manually configure
launch.json
in many cases (especially for Jest or Python with proper extensions).
Ensure you have the right extensions installed:
- Python extension (for Python)
- Jest Runner or Jest Integration (for Jest)
- C# Dev Kit or similar (for .NET)
Common Issues & Tips
-
Breakpoints not hit?
- Make sure the test file is actually being executed.
- Disable
justMyCode
inlaunch.json
:"justMyCode": false
- For Jest, use
--runInBand
to prevent parallelization.
-
Test not discovered?
- Check test file naming (e.g.,
test_*.py
or*.test.js
) - Run test discovery manually (look for a "Discover" button in Testing panel)
- Check test file naming (e.g.,
Use
console.log
debugger
In JS, insertdebugger;
statements — they’ll pause execution when debugging is active.
Basically, it comes down to: set up launch.json
, pick the right runtime, target your test, and use breakpoints. Once configured, debugging unit tests in VSCode is fast and intuitive.
The above is the detailed content of How to debug unit tests in VSCode. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are three ways to change the default terminal in VSCode: setting through a graphical interface, editing settings.json file, and temporary switching. First, open the settings interface and search for "terminalintegratedshell" and select the terminal path of the corresponding system; secondly, advanced users can edit settings.json to add "terminal.integrated.shell.windows" or "terminal.integrated.shell.osx" fields and escape the path correctly; finally, you can enter "Terminal:SelectD through the command panel

When the "Timedoutwaitingforthedebuggertoattach" issue occurs, it is usually because the connection is not established correctly in the debugging process. 1. Check whether the launch.json configuration is correct, ensure that the request type is launch or attach and there is no spelling error; 2. Confirm whether the debugger is waiting for the debugger to connect, and add debugpy.wait_for_attach() and other mechanisms; 3. Check whether the port is occupied or firewall restricted, and replace the port or close the occupied process if necessary; 4. Confirm that the port mapping and access permissions are configured correctly in a remote or container environment; 5. Update VSCode, plug-in and debug library versions to solve potential

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

To set debug environment variables in VSCode, you need to use the "environment" array configuration in the launch.json file. The specific steps are as follows: 1. Add "environment" array to the debugging configuration of launch.json, and define variables in key-value pairs, such as API_ENDPOINT and DEBUG_MODE; 2. You can load variables through .env files to improve management efficiency, and use envFile to specify file paths in launch.json; 3. If you need to overwrite the system or terminal variables, you can directly redefine them in launch.json; 4. Note that

Unit testing is crucial in Java projects, and mastering the key steps of the JUnit framework can help you get started quickly. 1. Introduce JUnit dependencies, use Maven or Gradle to add JUnitJupiter's API and Engine dependencies; 2. Write test classes, use @Test annotation to mark the test methods, and simplify assertion calls through static import; 3. Use @BeforeEach, @AfterEach, @BeforeAll and @AfterAll to manage the test life cycle; 4. Use assertEquals, assertTrue, assertNull and assertThrows to verify normal and exception logic.

The key to debugging code with VSCode in Docker containers is to configure the development environment and connection methods. 1. Prepare a mirror with development tools, install necessary dependencies such as debugpy or node, and use the official devcontainers image to simplify configuration; 2. Mount the source code and enable the Remote-Containers plug-in, create .devcontainer folders and configuration files, and realize in-container development; 3. Configure the debugger, add debug settings for the corresponding language in launch.json, and enable the listening port in the code; 4. Solve common problems, such as exposing the debug port, ensuring the host is 0.0.0.0, and use postCreateC

UseMockeryforcustomdependenciesbysettingexpectationswithshouldReceive().2.UseLaravel’sfake()methodforfacadeslikeMail,Queue,andHttptopreventrealinteractions.3.Replacecontainer-boundserviceswith$this->mock()forcleanersyntax.4.UseHttp::fake()withURLp

Breakpoints do not take effect usually result in debug configuration or environment settings errors. First, you need to confirm whether the debugging mode is started through the F5 or Debug buttons, rather than normal operation; second, check whether the type and request in launch.json are correctly configured, such as Node.js should be "type":"node"; then make sure the source code matches the execution file, enable sourcemap and correctly configure runtimeArgs; you also need to troubleshoot factors such as extension conflicts, path mapping problems, whether the breakpoint position is reasonable, whether the code is compressed and optimized, browser restrictions, etc.; finally, you can use the insertion of debugger statement to help determine the problem.
