-
Notifications
You must be signed in to change notification settings - Fork 392
Closed
Labels
waiting for customerWaiting for customer actionWaiting for customer action
Description
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
Labels
waiting for customerWaiting for customer actionWaiting for customer action