How to run all tests in Visual Studio Code

怎甘沉沦 提交于 2019-12-02 18:45:57

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

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.

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.)

GraehamF

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"
        }
    ]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!