Search code examples
c#.netmstestcodecovcoverlet

MStest + Coverlet don't getting up to 100% coverage in cli, but I got 100% in codecov


I recently created a solution from scratch with a C# .NET 8.0 project.

The project is only about unit tests and isolated classes, which I intend to be a folder of algorithm exercises.

The project has mstest as test runner and coverlet as coverage collector.

I set properties on the project so that it generates coverage output in lcov.info format:

    <!-- Coverage -->
    <CollectCoverage>true</CollectCoverage>
    <CoverletOutputFormat>lcov</CoverletOutputFormat>
    <CoverletOutput>./lcov.info</CoverletOutput>
    <IncludeTestAssembly>true</IncludeTestAssembly>

The project only has just one class, with a single method with a single line. Additionaly, has just one Test Class with a just single test method.

That lines is covered in test run, but in shell summary I got:

+----------------------------+--------+--------+--------+
| Module                     | Line   | Branch | Method |
+----------------------------+--------+--------+--------+
| algorithm-exercises-csharp | 88.88% | 100%   | 66.66% |
+----------------------------+--------+--------+--------+

+---------+--------+--------+--------+
|         | Line   | Branch | Method |
+---------+--------+--------+--------+
| Total   | 88.88% | 100%   | 66.66% |
+---------+--------+--------+--------+
| Average | 88.88% | 100%   | 66.66% |
+---------+--------+--------+--------+

Then try to verify which part would not be covered.

With Coverage-gutters in Visual Studio Code, I was able to verify that all the code is covered.

Then take the output from lcov.info to codecov. Here the service tells me that I have 100% coverage.

Why is the result by cli different? How can I increase coverage to 100% in CLI?

Command used (at .sln level):

dotnet test --verbosity normal

Runtime and SDK:

dotnet --version
8.0.204

Dependencies:

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="6.0.2" />
    <PackageReference Include="coverlet.msbuild" Version="6.0.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
    <PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
  </ItemGroup>

IDE:

Visual Studio Code + C# Dev Kit

Repository: https://github.com/sir-gon/algorithm-exercises-csharp


Solution

  • You appear to be generating coverage for a file at C:\Users\runneradmin\.nuget\packages\microsoft.net.test.sdk\17.9.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.Program.cs, which is uncovered. If you delete this file and run coverage again, does it show as 100%?