.net 如何在 Visual Studio Code 中运行所有测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41958510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run all tests in Visual Studio Code
提问by Miguel Gamboa
The latest version of VS Code already provides an easy way of running a single test as pointed on Tyler Long's answerto the question Debugging xunit tests in .NET Core and Visual Studio Code.
最新版本的 VS Code 已经提供了一种运行单个测试的简单方法,正如Tyler Long对Debugging xunit tests in .NET Core 和 Visual Studio Code问题的回答所指出的那样。
However, I am looking how can I run all testscontained in a test suite class in VS Code (without debug)?
但是,我正在寻找如何在 VS Code 中运行测试套件类中包含的所有测试(无需调试)?
The only way I found was adding to launch.jsona specific configuration as the following one, but which I can only run in debug (I would like to run it without debug):
我发现的唯一方法是将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"
}
回答by Jun Han
There is a much easier way to run all tests:
有一种更简单的方法来运行所有测试:
- Install the .NET Core Test Explorerextension
- Open a .NET Core test project in VS Code, or set
dotnet-test-explorer.testProjectPathto the folder path of .NET Core test project insettings.json - 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 Core 测试资源管理器扩展
- 在VS Code中打开一个.NET Core测试项目,或者设置
dotnet-test-explorer.testProjectPath为.NET Core测试项目的文件夹路径settings.json - 在资源管理器视图的 .NET 测试资源管理器中,将自动检测所有测试,并且您可以运行所有测试或某个测试
回答by Nate Barbettini
You can run all tests in a project by executing dotnet teston the terminal. This is handy if you already have the terminal open, but you can add it to Visual Studio code as well.
您可以通过dotnet test在终端上执行来运行项目中的所有测试。如果您已经打开了终端,这很方便,但您也可以将其添加到 Visual Studio 代码中。
If you press Cmd-Shift-Pto open the Command Palette and type "test", you can run the Run Test Taskcommand. By default, this doesn't do anything, but you can edit tasks.jsonto tell it how to run dotnet testfor you:
如果按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"
}
]
}
These two task definitions will link the Run Build Taskand Run Test Taskcommands in Visual Studio Code to dotnet buildand dotnet test, respectively.
这两个任务定义将分别将Visual Studio Code 中的Run Build Task和Run Test Task命令链接到dotnet build和dotnet test。
回答by severecci
To build on GraehamF's answer, the configuration required in tasks.jsonfor dotnet 2.0 is different.
为了以 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"
}
]
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.
我发现当 Visual Studio 和 VS Code 都安装时,将 csproj 引用放在命令属性中(如 GraehamF 的回答)会导致 Visual Studio 被打开,而不是在 VS Code 中运行测试。
(I would have put this in a comment, but I do not have enough reputation points.)
(我会把它放在评论中,但我没有足够的声望点。)
回答by codejockie
To run tests in Visual Studio (VS) Code, you need to add a tasks.jsonfile to the .vscodedirectory (if you don't already have it). Then configure your test task as follows:
要在 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
}
}
]
}
When you have that saved, run the following command Cmd-Shift-Pon Mac or Ctrl-Shift-Pon Linux and Windows within the VS Code interface then type Run Test Task, press Enterand select test.
当你有救了,运行以下命令Cmd- Shift- PMac或Ctrl- Shift-P在VS代码界面在Linux和Windows,然后键入运行测试任务,按Enter选择test。
The above configuration should work for the latest Insider and Stable releases of VS Code as at April 2020 (version 1.41).
上述配置应适用于 2020 年 4 月(1.41 版)最新的 Insider 和 Stable 版本的 VS Code。
回答by GraehamF
Similar to @Nate Barbettini's answer, but for .Net Core Standard 2.0 (netcoreapp2.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"
}
]
}


