Testing

Visual Studio Code provides tools to discover, run, and debug tests, inspect results, and measure code coverage. This article covers testing extensions, test execution, coverage, and integration with tasks.

AI assistance is optional. If you use GitHub Copilot, you can also add tests with an agent, get help configuring a test framework, and investigate failures.

About testing in VS Code

Testing support in VS Code is language-specific and depends on the testing extensions you have installed. Language extensions or standalone extensions can implement the testing features for a particular language or testing framework.

VS Code has the following features for testing your code:

  • Support multiple testing frameworks: language extensions and standalone testing extensions provide support for various languages and test runners, such as Jest, Mocha, Pytest, JUnit, and more.

  • Centralized test management: the Test Explorer provides a centralized place to manage and run the tests in your project. The Test Explorer might automatically discover the tests in your project, depending on the testing extension.

  • Integrated test results: view the test status inline in the editor alongside the test code, or view all tests in the Test Explorer. Detailed test results are available in the Test Results panel.

  • Debugging: debug your tests to diagnose test failures. Take advantage of the rich debugging support in VS Code, such as breakpoints, watch variables, and more.

  • Test coverage: run tests with coverage to see how much of your code is covered by your tests.

  • AI-assisted testing: optionally use GitHub Copilot to add and review tests, inspect your testing setup, and investigate failures.

  • Task integration: make your testing work easier by creating tasks to run your tests. You can also run your tests in the background when you change your code.

Get started with testing in VS Code

To get started with testing in VS Code, follow these steps:

  1. Open the Extensions view and install a testing extension for your project. Filter the extensions by the Testing category (@category:"testing").

  2. Select the beaker icon () in the Activity Bar to open the Test Explorer and discover the tests in your project.

  3. Run and debug your tests from the Test Explorer or directly from the editor.

Extensions for testing

You can find extensions that support testing by looking in the Visual Studio Marketplace. You can also go to the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)), and filter by the Testing category.

Tip

For AI help, ask an agent to inspect your project's testing setup and recommend a framework only if one isn't already configured. Review the proposal before installing dependencies. You can also use /setupTests (Experimental) when it is offered in your session's slash-command list.

Automatic test discovery in Testing view

The Testing view provides a centralized place to manage and run your tests. You can get to the Testing view by selecting the beaker icon () in the Activity Bar. You can also use the Testing: Focus on Test Explorer View command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

If you have a project with tests, the Test Explorer view discovers and lists the tests in your workspace. By default, the discovered tests are displayed in a tree view in the Test Explorer. The tree view matches the hierarchical structure of your tests, making it easy to navigate and run your tests.

Screenshot that shows VS Code with the Test Explorer showing the tests, matching the test code that is shown in the editor.

You can run tests from the Test Explorer by selecting the play button. Learn more about running and debugging tests in the Run and debug tests section.

Tip

Navigate to the test code by double-clicking on the test in the Test Explorer view. If you select a test that failed, the editor opens the test file, highlights the failed test, and shows the error message.

If you have many tests, you can use the filtering options to quickly find the tests you're interested in. You can use the Filter button to filter tests by status or only show tests for the current file. You can also use the search box to search for specific tests by name or use the @ symbol to search by status.

Test Explorer view with filtering options

In the More Actions menu, you can access more functionality, such as sort and display options.

If you add new tests or change your tests, use the Refresh Tests button to refresh the list of tests in the Test Explorer. You can also use the Test Explorer: Reload tests command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

Note

Depending on the testing extension, you might first have to configure the test framework or test runner to discover the tests in your project. For more information, consult the documentation of the testing extension. For AI-assisted setup, identify the project's framework and test commands before making changes.

Write tests with AI

An agent can help add tests, run the project's test command, and investigate failures. You still need to review the assertions and verify the results. Start with one function or module rather than asking for tests for the entire project.

The following workflow uses the Copilot harness in the Chat view, alongside your code. Both the Chat view and the Agents window run agents; see Run agents to choose an interface for your work.

  1. Set up GitHub Copilot, open your project, and install the project's dependencies.

  2. Open the Chat view (⌃⌘I (Windows, Linux Ctrl+Alt+I)), start a new chat, and select Copilot as the Session Target and Agent as the agent.

  3. Attach the source file, a representative test if available, and relevant requirements. Ask the agent to inspect the existing framework and propose cases before editing:

    Propose tests for the attached code using this project's existing
    framework and test helpers. Use the supplied requirements to define
    expected results, including boundary and error cases.
    Identify the targeted test command and any setup requirements.
    Do not edit files or install dependencies yet.
    
  4. Review the proposed cases. Once you agree on the expected behavior, ask the agent to add and run the tests without changing implementation code. Request the exact command, pass and fail counts, and any skipped tests.

  5. Review the diff and run the tests yourself. Check that assertions test the intended behavior and that unrelated files remain unchanged. Treat tests that couldn't run as unverified.

If a test fails, identify whether the cause is test setup, an incorrect expectation, or an implementation bug before accepting a fix. Don't weaken assertions, skip tests, or change expected values solely to obtain a passing result.

For a complete walkthrough, follow Test existing code with AI. It covers baseline tests, failure diagnosis, assertion quality, and coverage. An agent can run test commands in the terminal without a testing extension; Test Explorer discovery still requires an appropriate extension.

For a quick starting point without writing a prompt, use the Generate Tests smart action. Review and run the generated tests before keeping them. To reuse project testing conventions, add custom instructions.

Run and debug tests

After the discovery of the tests in your project, you can run and debug your tests, and view test results directly from within VS Code.

In the Test Explorer, use the controls in the section heading to run or debug all tests. You can also run or debug specific tests in the tree view by selecting the run/debug icon on a specific node.

Run and debug tests in Test Explorer

As you're viewing the test code in the editor, use the play control in the editor gutter to run the test at that position.

Run and debug tests in editor gutter

Right-click on the gutter control to view other actions, such as debugging the test.

Tip

Configure the default testing action for the gutter control by using the testing.defaultGutterClickAction Open in VS Code Open in VS Code Insiders setting.

After you run one or more tests, the editor gutter and tree view in the Test Explorer display the corresponding test status (passed/failed). When a test fails, notice that the test error message is shown as an overlay in the editor.

Screenshot that shows a test error message overlay in the editor.

Use the Show Output button in the Test Explorer to view the test output in the Test Results panel.

Test Results panel

Tip

For help with a current failure, hover over the failed test in the Test Explorer and select Fix Test Failure (sparkle icon). You can also attach failure output to chat. Review the suggested change against the expected behavior, then rerun the test and related tests. See Investigate test failures for how to distinguish a test problem from an implementation bug.

Screenshot showing the Test Explorer, highlighting the "Fix Test Failure" button, and the editor that shows a suggestion from Copilot on how to fix the failing test.

Test coverage

Test coverage is a measure of how much of your code is covered by your tests. It helps you identify areas of your code that are not being tested. VS Code supports running tests with coverage and viewing the coverage results if the corresponding testing extension support test coverage.

You can run tests with coverage, just like you can run and debug tests. Use corresponding the actions in the Test Explorer view, editor gutter, or commands in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

Run tests with coverage

After you run tests with coverage, you can view the coverage results in different locations:

  • In the Test Coverage view

    A tree view shows the tests with their coverage percentage. A color indicator also gives a visual cue about the coverage percentage. Hover over each line to see more details about the coverage results.

    Test Coverage view

  • As an overlay in the editor

    For code files that have test coverage, the editor shows a color overlay in the gutter to indicate which lines are covered by tests or not. When you hover over the gutter, notice that for covered lines, there is also an indicator for the number of times the line was executed. You can also select the Show Inline Coverage button in the editor title bar or use the Test: Show Inline Coverage command (⌘; ⇧⌘I (Windows, Linux Ctrl+; Ctrl+Shift+I)) to toggle the coverage overlay.

    View coverage in editor

  • In the Explorer view, which shows the coverage percentage of your code files

    The Explorer view shows the coverage percentage of your code files. Hover over a file or node in the Explorer to see more details about the coverage results.

    View coverage in Explorer view

  • In the diff editor

    If you have a diff editor open, the coverage results are also shown in the diff editor, similar to how they're shown in the editor.

  • In the Test Coverage toolbar

    The Test Coverage toolbar in the editor shows the test coverage results, lets you navigate between uncovered code blocks, and toggle inline coverage. Enable the Test Coverage toolbar with the testing.coverageToolbarEnabled Open in VS Code Open in VS Code Insiders setting.

    Test Coverage toolbar

Task integration

Tasks in VS Code can be configured to run scripts and start processes within VS Code, without having to enter a command line or write new code. In VS Code, you can define a default test task that runs your tests, and optionally create keyboard shortcuts to run the tests.

Use the command Tasks: Configure Default Test Task to configure the default test task. VS Code tries to automatically detect the test task, for example based on your package.json file. After you select the default test task, the tasks.json file is opened for you to customize the task.

The following code snippet shows a tasks.json file that specifies the node --test command as the default test task.

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "npm",
      "script": "test",
      "group": {
        "kind": "test",
        "isDefault": true
      },
      "problemMatcher": [],
      "label": "npm: test",
      "detail": "node --test src/tests/**.js"
    }
  ]
}

To run the test task, use the command Tasks: Run Test Task or create a keyboard shortcut for the command.

Learn more about using and configuring Tasks.

Note

Currently, tasks don't have special integration into VS Code's testing functionality, so running tests in a task won't update the user interface. Individual testing extensions can provide specific test automation functionality that integrates in the UI.

Test configuration settings

There are multiple settings that you can configure to customize the testing experience in VS Code. Each language extension might provide additional settings specific to testing in that language. Here are some common settings that you can configure:

Setting ID Details
testing.countBadge Open in VS Code Open in VS Code Insiders Controls the count badge on the Testing icon on the Activity Bar
testing.gutterEnabled Open in VS Code Open in VS Code Insiders Configure whether to show the test control in the editor gutter
testing.defaultGutterClickAction Open in VS Code Open in VS Code Insiders Configure the default action when selecting the gutter test control
testing.coverageBarThresholds Open in VS Code Open in VS Code Insiders Configure the colors for the coverage bar thresholds for the Test Coverage view
testing.displayedCoveragePercent Open in VS Code Open in VS Code Insiders Configure what percentage value is displayed in the Test Coverage view (total, statement, or minimum)
testing.showCoverageInExplorer Open in VS Code Open in VS Code Insiders Configure whether to show the coverage percentage in the Explorer view

You can find all testing-related settings in the Settings editor (⌘, (Windows, Linux Ctrl+,)).

Next steps