.net-core - 是什么导致 Docker 镜像中出现 "Could not find data collector ' XPlat 代码覆盖率'”错误?

标签 .net-core dockerfile coverlet

我正在 DevOps Server 中设置一个构建,它只是运行一个 docker 构建,运行一个容器,并从中收集测试结果。 Dockerfile 使用 mcr.microsoft.com/dotnet/core/sdk:2.2 镜像作为其基础,我在其上安装了 powershell。

运行基础镜像并执行 dotnet --version确认它有 .NET Core 2.2.301。我的测试项目引用了 coverlet.collector 1.0.1 和 Microsoft.NET.Test.Sdk 16.1.0。从我看到的 Coverlet 文档来看,这应该足以生成测试覆盖率结果。

当我不使用 Coverlet 时,我的测试运行良好并生成预期的 trx 文件。

这是我的 dockerfile 的简化版本:

FROM myrepo/coresdk22-powershell AS build
SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
USER ContainerAdministrator
# Not shown: some configuration steps using powershell
WORKDIR /app
COPY webapiapp/*.csproj ./webapiapp/
WORKDIR /app/webapiapp
RUN dotnet restore --configfile ../nuget.config
WORKDIR /app/
COPY webapiapp/. ./webapiapp/
WORKDIR /app/webapiapp
RUN dotnet publish -c Release -o out

FROM build AS testrunner
WORKDIR /app/webapiapp.test
COPY webapiapp.test/. .
RUN dotnet build -c Release -o out
WORKDIR /app/webapiapp.test/out
ENV Coverage="XPlat Code Coverage"
ENTRYPOINT dotnet vstest webapiapp.test.dll --logger:trx --collect:$env:Coverage

这是运行命令:
docker run -v "c:\testresults:C:\app\webapiapp.test\out\TestResults" --rm myrepo/webapiapp:$(BUILD.BUILDID)-test
这是 webapiapp.test.csproj:
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="1.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
  </ItemGroup>

</Project>

我得到的不是覆盖率文件,而是“找不到数据收集器‘XPlat 代码覆盖率’”。我错过了什么?

最佳答案

我遇到了同样的错误,我的解决方法是在下面添加 nuget 引用。

<PackageReference Include="coverlet.collector" Version="1.0.1" />

我试图让代码覆盖率在我的 azure devops 管道中工作,这对我有用。

我正在遵循以下教程,使用默认模板 api(WeatehrForecast 示例)

https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/dotnet-core?view=azure-devops

关于.net-core - 是什么导致 Docker 镜像中出现 "Could not find data collector ' XPlat 代码覆盖率'”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57169646/

相关文章:

docker - 为 Web .net 核心应用程序自动生成 Dockerfile

docker 不向网络主机公开端口

c# - 排除 ef 核心迁移文件的代码覆盖率

linux - Oracle Enterprise Linux 5/6 上的 .NET Core

c# - 模拟接口(interface) : Setup of the last mock affects the others 的多个实例

linux - Visual Studio Code 无法加载项目 - 找不到 Microsoft.NET.Sdk.Web

Dockerfile 构建从最终镜像中删除源代码

apache - 如何在 ubuntu docker 容器中自动启动 apache2?

c# - 无法添加封面 nuget 包 - 版本字符串部分太短或太长