.net 如何在Visual Studio代码中运行所有测试
最新版本的VS代码已经提供了一种简单的方法来运行问题所指出的单个测试 但是,我想知道如何在VS代码中运行测试套件类中包含的所有测试而不进行调试 我找到的唯一方法是在launch.json中添加一个特定的配置,如下所示,但我只能在debug中运行,我希望在没有debug的情况下运行它:.net 如何在Visual Studio代码中运行所有测试,.net,visual-studio-code,.net-core,xunit.net,.net,Visual Studio Code,.net Core,Xunit.net,最新版本的VS代码已经提供了一种简单的方法来运行问题所指出的单个测试 但是,我想知道如何在VS代码中运行测试套件类中包含的所有测试而不进行调试 我找到的唯一方法是在launch.json中添加一个特定的配置,如下所示,但我只能在debug中运行,我希望在没有debug的情况下运行它: { "name": ".NET Core Xunit tests", "type": "coreclr", "request": "launch", "preLaunchTask": "build"
{
"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"
}
通过在终端上执行dotnet测试,可以运行项目中的所有测试。如果您已经打开了终端,那么这很方便,但是您也可以将其添加到VisualStudio代码中 如果按Cmd-Shift-p打开命令选项板并键入test,则可以运行runtest任务命令。默认情况下,这不起任何作用,但您可以编辑tasks.json来告诉它如何为您运行dotnet测试: tasks.json
这两个任务定义将Visual Studio代码中的运行生成任务和运行测试任务命令分别链接到dotnet Build和dotnet Test。有一种更简单的方法来运行所有测试: 安装扩展插件 在VS代码中打开.NET核心测试项目,或将dotnet-test-explorer.testProjectPath设置为settings.json中.NET核心测试项目的文件夹路径 在资源管理器视图的.NET测试资源管理器中,将自动检测所有测试,并且您可以运行所有测试或某个测试
与@Nate Barbettini的答案类似,但适用于.Net核心标准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"
}
]
}
基于GraehamF的答案,dotnet 2.0的tasks.json中所需的配置是不同的
{
"version": "2.0.0",
"tasks": [
{
...
},
{
"label": "test",
"command": "dotnet",
"type": "shell",
"group": "test",
"args": [
"test",
"${workspaceFolder}/testprojectfolder/testprojectname.csproj"
],
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
我发现,当VisualStudio和VS代码都已安装时,将csproj引用放入命令属性(如GraehamF的回答中所示)会导致打开VisualStudio,而不是在VS代码中运行测试
我本来会把它放在评论中,但我没有足够的信誉点。要在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代码界面中,在Mac上运行以下命令Cmd-Shift-p或在Linux和Windows上运行Ctrl-Shift-p,然后键入run Test Task,按Enter键并选择Test
上述配置应适用于截至2020年4月版本1.41的最新内部版本和稳定版本的VS代码
这似乎在VS代码V1.14中有所改变。这是否适用于多个测试项目?我运行了我们使用nunit的dotnet测试,它为每个不是测试项目的项目输出了错误——但在测试项目中也运行了测试。这太糟糕了,不是吗?2是来自帮助的同一条注释,用户界面中的任何一个选项如何操作?settings.json位于何处?我在.vscode中添加了此文件,但它不起作用。@KokHowTeh在.vscode目录中添加此文件对我起作用。否则ctrl+应至少打开它。设置文件路径:.vscode>settings.json-{dotnet-test-explorer.testProjectPath:*/*Tests}
{
"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
}
}
]
}