Skip to content

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

@NatMarchand

Description

@NatMarchand

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions