65

The latest version of VS Code already provides an easy way of running a single test as pointed on Tyler Long's answer to the question Debugging xunit tests in .NET Core and Visual Studio Code.

However, I am looking how can I run all tests contained in a test suite class in VS Code (without debug)?

The only way I found was adding to launch.json a specific configuration as the following one, but which I can only run in debug (I would like to run it without debug):

{
  "name": ".NET Core Xunit tests",
  "type": "coreclr",
  "request": "launch",
  "preLaunchTask": "build",
  "program": "/usr/local/share/dotnet/dotnet",
  "args": ["test"],
  "cwd": "${workspaceRoot}/test/MyProject.Tests",
  "externalConsole": false,
  "stopAtEntry": false,
  "internalConsoleOptions": "openOnSessionStart"
}

5 Answers 5

92

There is a much easier way to run all tests:

  1. Install the .NET Core Test Explorer extension
  2. Open a .NET Core test project in VS Code, or set dotnet-test-explorer.testProjectPath to the folder path of .NET Core test project in settings.json
  3. In .NET Test Explorer of Explorer view, all the tests will be automatically detected, and you are able to run all tests or a certain test

.NET Test Explorer

8
  • #2 is the same comment from the help, how does one do either option in the UI?
    – CCondron
    Nov 3, 2018 at 23:54
  • 2
    Where is settings.json located? I added this file in .vscode but it doesn't work. May 30, 2019 at 3:31
  • @KokHowTeh Adding the file in the .vscode directory worked for me.
    – Anthony
    Jun 27, 2019 at 15:17
  • 2
    setting file path: .vscode > settings.json --- { "dotnet-test-explorer.testProjectPath": "**/*Tests" }
    – 3ehrang
    Nov 8, 2020 at 22:58
  • 4
    You should disclose that you're the author of this extension in the post. May 21, 2021 at 23:03
24

You can run all tests in a project by executing dotnet test on the terminal. This is handy if you already have the terminal open, but you can add it to Visual Studio code as well.

If you press Cmd-Shift-P to open the Command Palette and type "test", you can run the Run Test Task command. By default, this doesn't do anything, but you can edit tasks.json to tell it how to run dotnet test for you:

tasks.json

{
  "version": "0.1.0",
  "command": "dotnet",
  "isShellCommand": true,
  "args": [],
  "tasks": [
    {
      "taskName": "build",
      "args": [ ],
      "isBuildCommand": true,
      "showOutput": "silent",
      "problemMatcher": "$msCompile"
    },
    {
      "taskName": "test",
      "args": [ ],
      "isTestCommand": true,
      "showOutput": "always",
      "problemMatcher": "$msCompile"
    }
  ]
}

These two task definitions will link the Run Build Task and Run Test Task commands in Visual Studio Code to dotnet build and dotnet test, respectively.

1
  • 2
    This seems to have changed in VS Code v 1.14. Jun 17, 2018 at 14:31
15

To build on GraehamF's answer, the configuration required in tasks.json for dotnet 2.0 is different.

{
"version": "2.0.0",
"tasks": [
    {
        ...
    },
    {
        "label": "test",
        "command": "dotnet",
        "type": "shell",
        "group": "test",
        "args": [
            "test",
            "${workspaceFolder}/testprojectfolder/testprojectname.csproj"
        ],
        "presentation": {
            "reveal": "silent"
        },
        "problemMatcher": "$msCompile"
    }
]

I found that when Visual Studio and VS Code are both installed, putting the csproj reference in the command property (as in GraehamF's answer) resulted in Visual Studio being opened rather than the tests being run within VS Code.

(I would have put this in a comment, but I do not have enough reputation points.)

1
  • Would this work with multiple test projects? I ran dotnet test (we use nunit) and it outputted errors for each project that wasn't a test project - but ran the tests in the test projects fine as well. That sucks, right? Aug 23, 2018 at 16:14
7

To run tests in Visual Studio (VS) Code, you need to add a tasks.json file to the .vscode directory (if you don't already have it). Then configure your test task as follows:

{
    "version": "2.0.0",
    "tasks": [
        {
            ... // Other tasks
        },
        {
            "label": "test",
            "command": "dotnet",
            "type": "shell",
            "args": [
                "test",
                "${workspaceFolder}/TestProjectName/TestProjectName.csproj"
            ],
            "group": "test",
            "problemMatcher": "$msCompile",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            }
        }
    ]
}

When you have that saved, run the following command Cmd-Shift-P on Mac or Ctrl-Shift-P on Linux and Windows within the VS Code interface then type Run Test Task, press Enter and select test.

The above configuration should work for the latest Insider and Stable releases of VS Code as at April 2020 (version 1.41).

1

Similar to @Nate Barbettini's answer, but for .Net Core Standard 2.0 (netcoreapp2.0).

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "command": "dotnet test path/to/test-project.csproj",
            "type": "shell",
            "group": "test",
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.