VSCode中.NET Core Test Explorer扩展无法检测测试的问题
dotnet test失败,但命令行正常 问题描述
通过VSCode的.NET Core Test Explorer扩展运行测试时,输出如下错误:
Finding projects for pattern /Users/tommie/dev/.../CrescentAPITests/Tests
Found 3 matches for pattern in folder /Users/tommie/dev/.../CrescentAPITests
Evaluating match /Users/tommie/dev/.../CrescentAPITests/CrescentAPITests.cs
Adding directory /Users/tommie/dev/.../CrescentAPITests
Evaluating match /Users/tommie/dev/.../CrescentAPITests/CrescentAPITests.csproj
Evaluating match /Users/tommie/dev/.../CrescentAPITests/CrescentDBTests.cs
Executing dotnet test -t -v=q in /Users/tommie/dev/***/CrescentAPITests
[ERROR] Error while executing dotnet test -t -v=q -
但直接在命令行执行dotnet test -t -v=q时,测试用例能正常识别:
tommie@Tommies-MacBook-Pro CrescentAPITests % dotnet test -t -v=q Test run for /Users/tommie/dev/.../CrescentAPITests/bin/Debug/net6.0/CrescentAPITests.dll (.NETCoreApp,Version=v6.0) Microsoft (R) Test Execution Command Line Tool Version 17.3.1 (x64) Copyright (c) Microsoft Corporation. All rights reserved. The following Tests are available: CrescentDBTests.HandleResultParsing.Pass CrescentDBTests.HandleResultParsing.ParseEquipmentResult CrescentDBTests.HandleResultParsing.ParseDistanceAlongResponse CrescentDBTests.HandleResultParsing.ParseGetTraceResponse CrescentAPITests.HandleResultParsing.Pass CrescentAPITests.HandleResultParsing.ParseEquipmentResult CrescentAPITests.HandleResultParsing.ParseDistanceAlongResponse CrescentAPITests.HandleResultParsing.ParseGetTraceResponse
目前不清楚扩展执行命令时具体触发了什么错误。
排查与解决建议
验证扩展工作目录
扩展输出的执行目录是/Users/tommie/dev/***/CrescentAPITests,确认该路径和你命令行操作的目录完全一致(注意路径中的星号是否为实际路径省略,还是扩展解析异常)。若路径不符,可在VSCode设置中搜索.net test explorer test project path,手动指定测试项目CrescentAPITests.csproj的完整路径。获取扩展详细日志
打开VSCode输出面板,切换到.NET Test Explorer频道查看完整日志;若默认日志信息不足,可在设置中将Dotnet Test Explorer: Logging Level设为Debug,重新运行测试后获取更详细的错误细节(比如权限问题、环境变量差异)。检查版本兼容性
确认.NET Core Test Explorer扩展版本支持.NET 6.0(你的项目目标框架为net6.0),若版本过旧则更新至最新版。同时验证全局dotnet版本与项目使用版本一致,执行dotnet --version即可查看。清理项目缓存
执行dotnet clean和dotnet restore清理项目的bin/obj目录,重新生成项目后再用扩展运行测试,缓存文件损坏可能导致扩展解析失败。检查扩展命令参数
扩展输出的命令末尾多了一个多余的-,可能是参数拼接错误。在VSCode设置中检查Dotnet Test Explorer: Dotnet Test Arguments,确认是否存在无效配置,或重置该设置为默认值。
内容的提问来源于stack exchange,提问作者Tommie Jones

