作者:摄影师张恒 | 来源:互联网 | 2023-02-13 13:49
最新版本的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"
}
Jun Han..
47
有一种更简单的方法来运行所有测试:
安装.NET Core Test Explorer扩展
在VS Code中打开.NET Core测试项目,或者设置dotnet-test-explorer.testProjectPath
为.NET Core测试项目的文件夹路径settings.json
在Explorer视图的.NET Test Explorer中,将自动检测所有测试,并且您可以运行所有测试或某个测试
1> Jun Han..:
有一种更简单的方法来运行所有测试:
安装.NET Core Test Explorer扩展
在VS Code中打开.NET Core测试项目,或者设置dotnet-test-explorer.testProjectPath
为.NET Core测试项目的文件夹路径settings.json
在Explorer视图的.NET Test Explorer中,将自动检测所有测试,并且您可以运行所有测试或某个测试
settings.json在哪里?我在.vscode中添加了此文件,但是它不起作用。
2> Nate Barbett..:
您可以通过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 Task和Run Test Task命令链接到dotnet build
和dotnet test
.
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中运行测试.
(我会把它放在评论中,但我没有足够的声望点.)