如何在Visual Studio Code中运行所有测试

68
最新版本的VS Code已经提供了一种简单的方法来运行单个测试,如Tyler Long的回答中指出的那样,他回答了一个关于在.NET Core和Visual Studio Code中调试xunit测试的问题。

然而,我正在寻找如何在VS Code中运行包含在测试套件类中的所有测试(不使用调试)?

我唯一找到的方法是向launch.json添加以下特定配置,但这只能在调试模式下运行(我想在没有调试的情况下运行它):

{
  "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个回答

93

有一种更简单的方法来运行所有测试:

  1. 安装 .NET Core Test Explorer 扩展。
  2. 在 VS Code 中打开一个 .NET Core 测试项目,或者在 settings.json 中设置 dotnet-test-explorer.testProjectPath 为 .NET Core 测试项目的文件夹路径。
  3. 在“浏览器”视图中,.NET 测试资源管理器会自动检测到所有测试,并且您可以运行所有测试或某个特定测试。

.NET Test Explorer


#2 是来自帮助的相同评论,如何在用户界面中执行任一选项? - CCondron
2
settings.json 文件在哪里?我把这个文件添加到了 .vscode 但它不起作用。 - Kok How Teh
这可以按项目完成吗?我必须打开两个窗口,必须不断地在它们之间跳跃并更改全局设置。无法使全局设置生效。 - Craig.C
2
设置文件路径:.vscode > settings.json "dotnet-test-explorer.testProjectPath": "**/*Tests" }``` - 3ehrang
4
你应该在帖子中披露你是这个扩展的作者。 - Chaim Eliyah
显示剩余4条评论

26
你可以通过在终端上执行dotnet test来运行项目中所有的测试。如果你已经打开了终端,这很方便,但你也可以将其添加到Visual Studio Code中。
如果你按下 Cmd-Shift-P 打开命令面板并键入“test”,你可以运行Run Test Task命令。默认情况下,它不会做任何事情,但你可以编辑tasks.json告诉它如何为你运行dotnet testtasks.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"
    }
  ]
}

这两个任务定义将把Visual Studio Code中的运行构建任务运行测试任务命令分别链接到dotnet builddotnet test


2
这似乎在 VS Code v 1.14 中已经改变了。 - Patrick Szalapski

15

在GraehamF的回答基础上,tasks.json中所需的dotnet 2.0配置是不同的。

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

我发现当同时安装Visual Studio和VS Code时,在命令属性中放置csproj引用(如GraehamF的答案中所述)会导致打开Visual Studio而不是在VS Code中运行测试。

(我本来想把这个写在评论里的,但我没有足够的声望点数。)


这个能用于多个测试项目吗?我运行了dotnet test(我们使用nunit),它为每个不是测试项目的项目输出错误 - 但是也成功地运行了测试项目中的测试。这很糟糕,对吧? - Ian Grainger

8

在Visual Studio (VS) Code中运行测试,您需要将一个tasks.json文件添加到.vscode目录中(如果您还没有它)。然后按照以下方式配置您的测试任务:

{
    "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
            }
        }
    ]
}

当你保存了这个配置后,在 VS Code 界面中,在 Mac 上运行以下命令:Cmd-Shift-P,在 Linux 和 Windows 上运行以下命令:Ctrl-Shift-P,然后输入Run Test Task,按下 Enter 并选择 test。此配置适用于 2020 年 4 月最新的 VS Code 预览版和稳定版(版本号为 1.41)。

1

与@Nate Barbettini的答案类似,但适用于.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"
        }
    ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接