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 started with coverlet with xUnit in a .NET Core 2.0 console application

See original GitHub issue

I 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:

  1. Create a new .NET Core console application project using Visual Studio 2017.

  2. Create a new class under test, such as:

    public class TruthTeller
    {
        public bool TellTruth()
        {
            return true;
        }
    }
  1. Create an xUnit test project within the solution and give it a reference to the first project.

  2. 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);
        }
    }
  1. Following coverlet readme, open package manager, select the xUnit project as the Default Project and run:
    Install-Package coverlet.msbuild
  1. From a command prompt at the directory of the xUnit project, run the following command:
    dotnet test /p:CollectCoverage=true
  1. Search for the coverage.json file - it’s nowhere to be found.

The source of the above unsuccessful experiment is in the following repository:

https://github.com/ivansams/CoverletCheck

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
paratroidcommented, Apr 2, 2019

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:

  • Install coverlet 2.6.0 (latest version as of 02/04/19) using dotnet add package coverlet.msbuild
  • In the root of the solution run 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

  • .NET core tests are all using xunit
  • .NET core version is 2.2
  • OS: Windows
  • Coverlet version: 2.6.0

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 run coverlet .\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.

image

image

1reaction
andre-castro-garciacommented, Jul 4, 2018

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting started: .NET Core with command line > xUnit.net
In this article, we will demonstrate getting started with xUnit.net and .NET Core (including .NET 5), showing you how to write and run...
Read more >
Unit testing C# code in .NET Core using dotnet test and xUnit
Learn unit test concepts in C# and .NET Core through an interactive experience building a sample solution step-by-step using dotnet test and ...
Read more >
Getting Started: .NET Framework with Visual Studio ...
Select it, then click "Next". We're picking .NET Core even though we're planning to make a .NET Framework test project, because Visual Studio...
Read more >
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 >
Run MsBuild.Coverlet without dotnet command
I have an old project (VS2105) I'm unable to use the dotnet command so I'm trying to use coverlet.msbuild task. MSBuild.exe my-solution.sln ...
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