如何在Ubuntu系统VSCode中配置MSTest测试项目的工作区设置?
Ubuntu下VSCode配置MSTest项目的工作区设置步骤
一、确认测试项目依赖配置
- 检查两个测试项目(Codificer.Testing、ToolKit)的
.csproj文件,确保包含MSTest必需的NuGet包引用:<ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" /> <PackageReference Include="MSTest.TestAdapter" Version="3.5.0" /> <PackageReference Include="MSTest.TestFramework" Version="3.5.0" /> </ItemGroup> - 确保项目目标框架与你的主项目一致(例如
<TargetFramework>net8.0</TargetFramework>)
二、配置VSCode工作区设置
在工作区根目录创建或编辑.vscode/settings.json,添加以下配置:
{ // 指定具体测试项目路径,或用通配符自动匹配 "dotnet.testExplorer.testProjectPath": [ "./Codificer.Testing/Codificer.Testing.csproj", "./ToolKit/ToolKit.csproj" ], "omnisharp.enableMsBuildLoadProjectsOnDemand": false, "csharp.devKit.debugging.enableTestAdapter": true, "csharp.testing.enabledTestFrameworks": ["mstest"] }
- 若希望自动识别所有测试项目,可将
dotnet.testExplorer.testProjectPath设为"**/*Test*.csproj"
三、验证测试运行环境
- 打开终端,在工作区根目录执行:
确保所有依赖包下载完成dotnet restore - 执行测试命令验证项目可用性:
确认两个项目的测试用例能正常执行dotnet test
四、配置测试调试(可选)
若需要调试单个测试用例,可手动创建.vscode/launch.json:
{ "version": "0.2.0", "configurations": [ { "name": "调试Codificer.Testing项目", "type": "coreclr", "request": "launch", "program": "dotnet", "args": [ "test", "${workspaceFolder}/Codificer.Testing/Codificer.Testing.csproj", // 可选:添加过滤器指定单个测试方法 "--filter", "FullyQualifiedName=Codificer.Testing.YourTestClass.YourTestMethod" ], "cwd": "${workspaceFolder}", "console": "internalConsole" }, { "name": "调试ToolKit项目", "type": "coreclr", "request": "launch", "program": "dotnet", "args": [ "test", "${workspaceFolder}/ToolKit/ToolKit.csproj" ], "cwd": "${workspaceFolder}", "console": "internalConsole" } ] }
- 替换
FullyQualifiedName为具体测试方法的完整命名空间+类名+方法名,即可调试指定测试
五、常见问题排查
- 若测试面板加载不出测试:检查
.csproj包引用是否正确,运行dotnet test查看报错信息 - 重启VSCode或重新加载工作区,确保C# Dev Kit和Omnisharp扩展正常启用
- 确认Ubuntu系统已安装与项目目标框架匹配的.NET SDK,可通过
dotnet --version查看版本
内容的提问来源于stack exchange,提问作者IAbstract
相关产品推荐
相关产品推荐

