【问题标题】:How to run all tests in Visual Studio Code如何在 Visual Studio Code 中运行所有测试
【发布时间】:2017-06-16 22:40:09
【问题描述】:

最新版本的 VS Code 已经提供了一种运行单个测试的简单方法,正如 Tyler Long's answer 对问题 Debugging xunit tests in .NET Core and Visual Studio Code 所指出的那样。

但是,我正在寻找如何运行 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"
}

【问题讨论】:

    标签: .net visual-studio-code .net-core xunit.net


    【解决方案1】:

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

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

    【讨论】:

    • #2 与帮助中的评论相同,如何在 UI 中执行任一选项?
    • settings.json 在哪里?我在 .vscode 中添加了这个文件,但它不起作用。
    • @KokHowTeh 在 .vscode 目录中添加文件对我有用。
    • 否则ctrl + , 应该打开它(至少 Ubuntu 18.04)。
    • 设置文件路径:.vscode > settings.json --- { "dotnet-test-explorer.testProjectPath": "**/*Tests" }
    【解决方案2】:

    您可以通过在终端上执行dotnet test 来运行项目中的所有测试。如果您已经打开了终端,这很方便,但您也可以将其添加到 Visual Studio 代码中。

    如果你按Cmd-Shift-P打开命令面板并输入“test”,你可以运行运行测试任务 命令。默认情况下,这不会做任何事情,但您可以编辑 tasks.json 告诉它如何为您运行 dotnet test

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

    这两个任务定义将 Visual Studio Code 中的 Run Build TaskRun Test Task 命令分别链接到 dotnet builddotnet test

    【讨论】:

    • 这似乎在 VS Code v 1.14 中有所改变。
    【解决方案3】:

    以 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 测试(我们使用 nunit),它为每个不是测试项目的项目输出了错误 - 但在测试项目中运行测试也很好。这很糟糕,对吧?
    【解决方案4】:

    要在 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
                }
            }
        ]
    }
    

    保存后,在 Mac 上运行以下命令 Cmd-Shift-PCtrl -Shift-P 在 Linux 和 Windows 上的 VS Code 界面中,然后键入 Run Test Task,按 Enter 并选择 test

    上述配置应适用于 2020 年 4 月(1.41 版)最新的 VS Code Insider 和 Stable 版本。

    【讨论】:

      【解决方案5】:

      类似于@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"
              }
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-23
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-06
        相关资源
        最近更新 更多