You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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"

三、验证测试运行环境

  1. 打开终端,在工作区根目录执行:
    dotnet restore
    
    确保所有依赖包下载完成
  2. 执行测试命令验证项目可用性:
    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:53:20