DomBurf
DomBurf

Reputation: 2522

Code coverage with .NET Core 2.0

I am trying to setup code coverage on our ASP.NET Core 2.0 web application. I use dotcover (from JetBrains) to provide code coverage on all my other builds (by running dotcover from the command-line during the build process).

When I run dotcover from our build server for our .NET Core 2.0 web app I see this.

enter image description here

It states that it has run successfully but then just hangs there and no code coverage files are created.

dotcover analyse /TargetExecutable:"C:\Program Files\dotnet\dotnet.exe" /TargetArguments:"test MyUnitTests.csproj" /Output:report.html /ReportType:HTML /LogFile=dotcover.log 

If I try and add code coverage collection I see this.

enter image description here

dotcover analyse /TargetExecutable:"C:\Program Files\dotnet\dotnet.exe" /TargetArguments:"test MyUnitTests.csproj --collect:coverage" /Output:report.html /ReportType:HTML /LogFile=dotcover.log

And finally if I run dotnet test on its own (without dotcover) it seems to have worked, but again no coverage output is created.

enter image description here

dotnet test "MyUnitTests.csproj" -- collect:coverage

I'm unsure how to generate code coverage for a .NET Core 2.0 app, and not sure how / what data collectors are and how they should be used. Is dotcover the data collector in this example?

Basically, I just want to generate code coverage for a .NET Core 2.0 application.

UPDATE:

As per the suggestion I have installed coverlet as an alternative to dotcover. I have got it working but am getting inconsistent behaviour. When I run my batch file from the server it's all fine.

enter image description here

But when run from TFS I get an error.

enter image description here

System.IO.FileNotFoundException: No test is available in . Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk" and framework version settings are appropriate and try again.

My project does have a reference to that assembly (it's installed by default by VS itself when you create a unit test project).

Why is TFS complaining about that assembly when it's definitely there and can be run manually from the command-line without an error?

Upvotes: 4

Views: 4812

Answers (2)

Bronumski
Bronumski

Reputation: 14282

I have just migrated a project from net461 to dotnet core and I wanted to maintain code coverage. Using the command line suggested by the OP was what I needed. I am migrating fake scripts where I was previously using dot cover to generate the snapshot file to be pushed to teamcity and an html report. I can confirm that the following works for me:

  • DotNet SDK 2.1.2
  • JetBrains.dotCover.CommandLineTools 2018.1.0
    • Also tested positive with 2017.1.20170403.131707
  • Microsoft.NET.Test.Sdk 15.7.0
  • NUnit 3.10.1
  • NUnit3TestAdapter 3.10.0

DotCover cover

<dotcoverpath>\dotCover.exe cover /TargetExecutable:"C:\Program Files\dotnet\dotnet.exe" /TargetArguments:"test --no-build --no-restore ./src/<test-project-root>" /Filters=+:<namespace-to-test> /Output:./bin/DotCover.snapshot

DotCover analyze

I am not doing the following as I use the DotCover report command on the generated snapshot file but I can also confirm that the following is working for me as well.

<dotcoverpath>\dotCover.exe analyze /TargetExecutable:"C:\Program Files\dotnet\dotnet.exe" /TargetArguments:"test --no-build --no-restore ./src/<test-project-root>" /Filters=+:<namespace-to-test> /ReportType=HTML /Output:./bin/coverage.html

Notes

  • In my test run I am not restoring packages (--no-restore) or compiling (--no-build) the project as I have already done this in a previous step. These can be removed to suit your needs.
  • The OP did say that dot cover appeared to hang after the tests had completed. It is worth pointing out that the effort in turning the snapshot at the end of the test coverage into a report can take a while if there is a large amount of code and or assemblies. You do in fact see dot cover pausing at the point the tests have completed whilst it generates the html report. Use the filter (/Filters=) parameter to cut down on the size of things being covered, especially if you don't want to do any coverage on third party components.

/Filters=ARG (Optional) Specifies coverage filters. Syntax: +:module=*;class=*;function=*; Use -:myassembly to exclude an assembly from code coverage. Asterisk wildcard (*) is supported here

Upvotes: 3

Julito Avellaneda
Julito Avellaneda

Reputation: 2385

I don't know if dotcover can works with net core, but you can take a look for coverlet, also you can find an example here: Cross platform code coverage arrives for .NET Core

Upvotes: 3

Related Questions