Unable to get started with coverlet with xUnit in a .NET Core 2.0 console application
See original GitHub issueI would like to make use of coverlet to check unit test coverage in our .NET Core 2.0 projects, which already have a great number of xUnit unit tests.
I am following the readme carefully but the coverage.json
file is not generated and I’m not sure why as setup is mostly automatic and there are no error messages. I apologise for getting stuck at the most basic of levels, but there must be an issue with the coverlet documentation, at least.
To reproduce:
-
Create a new .NET Core console application project using Visual Studio 2017.
-
Create a new class under test, such as:
public class TruthTeller
{
public bool TellTruth()
{
return true;
}
}
-
Create an xUnit test project within the solution and give it a reference to the first project.
-
Create a test for the method created in (2) such as:
public class TruthTellerTests
{
[Fact]
public void TellTruth_MethodCall_ReturnsTrue()
{
var truthTeller = new TruthTeller();
var result = truthTeller.TellTruth();
Assert.True(result);
}
}
- Following coverlet readme, open package manager, select the xUnit project as the Default Project and run:
Install-Package coverlet.msbuild
- From a command prompt at the directory of the xUnit project, run the following command:
dotnet test /p:CollectCoverage=true
- Search for the
coverage.json
file - it’s nowhere to be found.
The source of the above unsuccessful experiment is in the following repository:
Issue Analytics
- State:
- Created 5 years ago
- Comments:14
Top GitHub Comments
Apologises to revive this old thread but I appear to be getting the same thing happening, and this thread is the best match for what I’ve found vs anything else available online. I’m hoping it’s just me missing something really simple in the set-up:
Reproduction steps:
dotnet add package coverlet.msbuild
dotnet test .\path-to-test.csproj /p:CollectCoverage=true
This appears to run ok in Powershell however there is no coverage report that is generated from this in the root of the directory (have checked in other folders too - no coverage report). Additionally there is no coverage output in the Powershell console window either; unsure if it does output to the console however.
Additional information
I can confirm that my .\path-to-test.csproj file has a reference to coverlet in it’s package references. I have also trying installing coverlet console using
dotnet tool install --global coverlet.console
and runcoverlet .\path-to-test.csproj --target "dotnet" --targetargs "test"
. Interestingly this does produce an output coverage file and console coverage output, however both are empty.I have a suspicion that my issue could be to do with the way in which my project directories are set up. The format is:
Root –Folder/Logic1.csproj –Folder/Tests1.csproj –Folder/Logic2.csproj –Folder/Tests2.csproj MySolution.sln
I run the
dotnet test .\Folder\TestsX.csproj /p:CollectCoverage=true
when performing tests. Hope this is enough information, can anyone advise on how to produce code coverage? I’ve also attached what I’m seeing as output in Powershell.@ivansams I have passed for this problem, in that case i have forgot to add ‘coverlet.msbuild’ package in my .csproj
I think that’s the problem, add a new reference to your .csproj and run coverage again:
Reference
<PackageReference Include="coverlet.msbuild" Version="2.0.1" />
Project File https://github.com/ivansams/CoverletCheck/blob/master/CoverletCheckTests/CoverletCheckTests.csproj
Andre Garcia