2017-01-31 199 views
10

VS代码的最新版本已经提供了运行单个测试的简单方法,如Tyler Long's answer指向Debugging xunit tests in .NET Core and Visual Studio Code的问题。如何在Visual Studio代码中运行所有测试代码

但是,我正在寻找如何运行包含在VS代码中的测试套件类中的所有测试

我发现这是增加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" 
} 

回答

8

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

如果按Cmd的 - - P打开命令面板,并键入“测试”,您可以运行运行测试任务命令。默认情况下,这并不做任何事情,但你可以编辑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代码中的命令分别为dotnet builddotnet test

8

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

  1. 安装.NET Core Test Explorer扩展
  2. 打开在VS守则.NET核心测试项目,或者设置dotnet-test-explorer.testProjectPath到.NET核心的文件夹路径在settings.json
  3. 在Explorer视图中的.NET测试浏览器的测试项目,所有的测试将被自动检测到,并且你能够运行所有测试或某个测试

test-explorer

0

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

2

要建立在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代码同时安装,投入命令属性的csproj参考(如GraehamF的答案),导致在Visual Studio中被打开,而不是VS代码中运行测试。

(我会把它放在评论中,但我没有足够的声望点。)