question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to get coverage when running tests with Microsoft.AspNetCore.Mvc.Testing

See original GitHub issue

Hello there, I have an issue with a simple repro solution: Three projects :

  • WebApp (netcoreapp3.1 with aspnetcore, very simple api)
  • Business (netstandard2.0 with only one static class/method)
  • Tests (netcoreapp3.1 with xunit and two tests)

The first test instantiate my api controller executes the method and assert on the result. The second one do almost the same but by instantiating the whole aspnetcore pipeline via the WebApplicationFactory.

The version are as follows:

coverlet.msbuild Version="2.9.0"
Microsoft.AspNetCore.Mvc.Testing Version="3.1.5"
Microsoft.NET.Test.Sdk Version="16.6.1"
xunit Version="2.4.1"
xunit.runner.visualstudio Version="2.4.2"
public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        var sut = new DataController();
        var actionResult = sut.Get() as OkObjectResult;
        Assert.NotNull(actionResult);
        Assert.StartsWith("H", (string)actionResult.Value);
        Assert.EndsWith("!", (string)actionResult.Value);
        var t = typeof(WebApplicationFactory<Startup>);
    }

    //[Fact(Skip = "meh")]
    [Fact]
    public async Task Test2()
    {
        var webApplicationFactory = new WebApplicationFactory<Startup>()
            .WithWebHostBuilder(builder =>
            {
                builder.UseEnvironment("Development");
            });
        using var client = webApplicationFactory.CreateClient();
        var r = await client.GetAsync("data");
        var c = await r.Content.ReadAsStringAsync();
        Assert.StartsWith("H", c);
        Assert.EndsWith("!", c);
        r.EnsureSuccessStatusCode();
    }
}

When I run as is I got this coverage :

dotnet test .\Tests\Tests.csproj -p:CollectCoverage=true -p:CoverletOutputFormat="json%2copencover%2ccobertura"

+----------+--------+--------+--------+
| Module   | Line   | Branch | Method |
+----------+--------+--------+--------+
| Business | 0%     | 0%     | 0%     |
+----------+--------+--------+--------+
| WebApp   | 86,36% | 100%   | 80%    |
+----------+--------+--------+--------+

When I skip Test2 I got this coverage:

dotnet test .\Tests\Tests.csproj -p:CollectCoverage=true -:CoverletOutputFormat="json%2copencover%2ccobertura"

+----------+--------+--------+--------+
| Module   | Line   | Branch | Method |
+----------+--------+--------+--------+
| Business | 71,42% | 50%    | 100%   |
+----------+--------+--------+--------+
| WebApp   | 18,18% | 100%   | 20%    |
+----------+--------+--------+--------+

It seems that hosting with WebApplicationFactory makes the coverage unavailable. I can provide the whole solution if needed.

My env :

> dotnet --version
3.1.400-preview-015178

> [System.Environment]::OSVersion.Version
Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      19042  0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
MarcoRossignolicommented, Jul 14, 2020

We’re working on some solutions, but I don’t have eta at the moment, btw you can use a trick used by one of our user https://github.com/coverlet-coverage/coverlet/pull/225#issuecomment-573896446

Also check if your coverage report generator can merge files like reportgenerator https://www.palmmedia.de/OpenSource/ReportGenerator

1reaction
MarcoRossignolicommented, Jul 14, 2020

I can provide the whole solution if needed.

Could be related to known issue https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/KnownIssues.md#1-vstest-stops-process-execution-earlydotnet-test Can you try with collectors integration? https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md

It it doesn’t work please can you provide the solution so I can test on my local?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use code coverage for unit testing - .NET
Code coverage is a measurement of the amount of code that is run by unit tests - either lines, branches, or methods. As...
Read more >
Integration tests in ASP.NET Core
ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server.
Read more >
Troubleshoot code coverage - Visual Studio
In Test Explorer, select Run All to verify that the tests run successfully. Fix any failures before using Analyze Code Coverage.
Read more >
Test ASP.NET Core MVC apps
ASP.NET Core applications support automated integration and functional testing. ... A unit test runs completely in memory and in process.
Read more >
Unit test controller logic in ASP.NET Core
This is performed with the call to mockRepo. Verify , which fails the test if the expected method wasn't called.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found