Search code examples
.net-coreazure-devopscoverlet

Error running unit tests on azure devops with nugget coverlet.msbuild / 2.3.1


When I run the dotnet test task, the tests run correctly but when generating the report file in xml format I get the following error:

/home/adminuser/.nuget/packages/coverlet.msbuild/2.3.1/build/netstandard2.0/coverlet.msbuild.targets(17,5): error : Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare)'.

The project configuration file (.csproj) is as follows:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>

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

  <ItemGroup>
    <PackageReference Include="coverlet.msbuild" Version="2.3.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
    <PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
    <PackageReference Include="NSubstitute" Version="4.2.2" />
  </ItemGroup>

</Project>

Note: The pipeline was running fine but from one moment to the next it started generating the following error


Solution

  • You're using self-hosted agent to run the pipeline, so the command should be executed in your local environment. You can try steps below to resolve the issue:

    1.Clean the package cache, delete the bin and obj folder and run the dotnet test command again.

    2.Update coverlet.msbuild package from 2.3.1 to latest 2.9.0.

    In addition:

    To generate xml report(coverage.cobertura.xml): You should use coverlet.collector package with command dotnet test --collect:"XPlat Code Coverage".

    To generate json report(coverage.json): You should use coverlet.msbuild package with command dotnet test /p:CollectCoverage=true.

    More details check coverlet-coverage/coverlet.