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)

Configuring VSCode to synchronize code with GitHub can improve development efficiency and team collaboration. First, install the "GitHubPullRequestsandIssues" and "GitLens" plugins; second, configure the GitHub account; then clone or create a repository; finally, submit and push the code to GitHub.

Best practices for writing JavaScript code in VSCode include: 1) Install Prettier, ESLint, and JavaScript (ES6) codesnippets extensions, 2) Configure launch.json files for debugging, and 3) Use modern JavaScript features and optimization loops to improve performance. With these settings and tricks, you can develop JavaScript code more efficiently in VSCode.

VSCode solves the problems of multilingual project coding and garbled code including: 1. Ensure that the file is saved with correct encoding and use the "redetection encoding" function; 2. Set the file encoding to UTF-8 and automatically detect the encoding; 3. Control whether to add BOM; 4. Use the "EncodingConverter" plug-in to convert encoding; 5. Use the multiple workspace functions to set encoding for different sub-projects; 6. Optimize performance and ignore unnecessary file monitoring. Through these steps, the coding problem of multilingual projects can be effectively dealt with.

I have a lot of experience in participating in VSCode offline technology exchange activities, and my main gains include sharing of plug-in development, practical demonstrations and communication with other developers. 1. Sharing of plug-in development: I learned how to use VSCode's plug-in API to improve development efficiency, such as automatic formatting and static analysis plug-ins. 2. Practical demonstration: I learned how to use VSCode for remote development and realized its flexibility and scalability. 3. Communicate with developers: I have obtained skills to optimize VSCode startup speed, such as reducing the number of plug-ins loaded at startup and managing the plug-in loading order. In short, this event has benefited me a lot and I highly recommend those who are interested in VSCode to participate.

Create and manage multiple project workspaces in VSCode through the following steps: 1. Click the "Manage" button in the lower left corner, select "New Workspace", and decide the save location. 2. Give the workspace a meaningful name, such as "WebDev" or "Backend". 3. Switch the project in Explorer. 4. Use the .code-workspace file to configure multiple projects and settings. 5. Pay attention to version control and dependency management to ensure that each project has .gitignore and package.json files. 6. Clean useless files regularly and consider using remote development skills

The method of setting beautiful and easy-to-read code fonts and font sizes in VSCode is as follows: 1. Open VSCode and enter the settings interface. 2. Enter {"editor.fontFamily":"FiraCode","editor.fontSize":14,"editor.lineHeight":24} in the settings. I recommend using FiraCode fonts, setting the font size to 14 and the line height to 24 to improve the programming experience.

Methods to improve the efficiency of VSCode code navigation in large code bases include: 1) using symbol navigation (Ctrl P and Ctrl T) to quickly find files and symbols; 2) using code jump (F12 or Ctrl Click) to jump directly to function definitions or variable declarations; 3) using global search (Ctrl Shift F) to accurately find code snippets; 4) install extension tools such as GitLens and Bookmarks to enhance navigation functions; 5) optimize project indexing and search performance, regularly clean useless files and use filtering conditions. These methods can significantly improve navigation efficiency in large code bases.

The reason why the editor crashes after the VSCode plugin is updated is that there is compatibility issues with the plugin with existing versions of VSCode or other plugins. Solutions include: 1. Disable the plug-in to troubleshoot problems one by one; 2. Downgrade the problem plug-in to the previous version; 3. Find alternative plug-ins; 4. Keep VSCode and plug-in updated and conduct sufficient testing; 5. Set up automatic backup function to prevent data loss.
