dotnet 测试无法找到具有友好名称“代码覆盖率”的数据收集器

rol*_*i09 10 .net xml unit-testing mstest code-coverage

几个月前,当我们的项目仍在 .NET 4.7.2 中时,我们曾经使用该Visual Studio Test任务在 Azure DevOps Server 上运行单元测试。我们使用参数Test files来指定哪些测试程序集需要运行并且Code Coverage在那里工作得很好。

自从我们将项目迁移到 .NET 5.0 以来,我们过去常常dotnet test运行单元测试,但现在Code Coverage不再工作了。

这是一个例子:

D:\test-2-1\_tool\dotnet\dotnet.exe test A.Test.dll B.Test.dll C.Test.dll --logger trx --results-directory D:\test-2-1\_temp --settings D:\test-2-1\5\s\.runsettings
Microsoft (R) Test Execution Command Line Tool Version 16.9.4
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 22 test files matched the specified pattern.
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'
Run Code Online (Sandbox Code Playgroud)

内容.runsettings

<DataCollectionRunSettings>
    <DataCollectors>
        <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
            <Configuration>
                <CodeCoverage>
                    <ModulePaths>
                        <Include>
                            <ModulePath>{someRegexToLimitCodeCoverageToSpecificTestAssemblies}</ModulePath>
                        </Include>
                    </ModulePaths>
                </CodeCoverage>
            </Configuration>
        </DataCollector>
    </DataCollectors>
</DataCollectionRunSettings>
Run Code Online (Sandbox Code Playgroud)

在另一个较小的项目中,Code Coverage仍然有效,但这里我们指定应该测试哪些项目文件,而不是哪些已编译的测试程序集

这是现在要走的路吗?如果是,是否也可以通过指定测试程序集使其恢复正常工作?

提前致谢!

ffo*_*onz 2

@LoriB 我的 Azure Devops 管道中的问题是开发人员使用构建的 dll 作为测试的项目参数。这适用于运行单元测试,但不适用于代码覆盖率。我现在不再将其用作**/*Tests.dll参数,而是将其更改为**/*Tests.csproj现在代码覆盖率有效。

在问题的示例中,该行

D:\test-2-1\_tool\dotnet\dotnet.exe test A.Test.dll B.Test.dll C.Test.dll --logger trx --results-directory D:\test-2-1\_temp --settings D:\test-2-1\5\s\.runsettings

应该改为

D:\test-2-1\_tool\dotnet\dotnet.exe test A.Test.csproj B.Test.csproj C.Test.csproj --logger trx --results-directory D:\test-2-1\_temp --settings D:\test-2-1\5\s\.runsettings

甚至更好

D:\test-2-1\_tool\dotnet\dotnet.exe test **/*.Test.csproj --logger trx --results-directory D:\test-2-1\_temp --settings D:\test-2-1\5\s\.runsettings

编辑:

但回答你的问题:

dotnet.exe 的帮助是这样说的:

用法:dotnet test [选项] <项目| 解决方案> [[--] ...]]

参数:<项目| SOLUTION> 要操作的项目或解决方案文件。如果未指定文件,该命令将在当前目录中搜索一个文件。

尽管我找到了带有 dll 的示例,但 dotnet.exe 要求提供项目解决方案文件。而不是图书馆或任何其他集会。由于我无法获得与 dll 一起使用的代码覆盖率,因此我建议不要使用程序集作为输入。仅项目或解决方案文件。