Code Coverage in .Net Core 2.0
Asked Answered
S

3

9

How can I generate code coverage in .Net Core 2.0?

I tried the following:

"TestPlatform\vstest.console.exe" --collect:"Code Coverage"

But I got this error message:

Data collector 'Code Coverage' message: Cannot find CodeCoverage.exe.

I tried dotCover latest version 2017.2 which I am able to get the coverage on the local machine, but when the same is run in the TFS Build no coverage information is generated.

I am not sure when NetCore Test task in TFS will get support for code coverage.

How can I publish the results from DotCover in TFS also, to use DotCover Commandline to generate the Coverage for .Net Core?

Screens answered 5/9, 2017 at 14:34 Comment(2)
@slartidan This is related to SonarQube as well because the current MS Build Task in TFS is a not able to analyze the Net Core. I am using SonarCLI for the analysisScreens
Thanks for the clarification!Crackpot
K
2

VSTest task cannot run .NET core tests as it uses the Test platform version 1. To run .NET core tests, we recommend using the .NET core task(preview) with the test command.

However, Code coverage and other data collection is not supported yet, no agent support.

We are fixing this issue as part of this https://github.com/Microsoft/vsts-agent/pull/1149/files Will update the thread once fix is gone and new agent is released. thread https://github.com/microsoft/vstest/issues/579#issuecomment-324401462

Source Link: VSTest task fails to execute tests in .NET Core 2.0 test project

Kicker answered 6/9, 2017 at 9:45 Comment(3)
I'm aware of the work from the vsts, I wanted to know how to publish the coverage as DotCover uses the TestWindow/vstestconsole.exe where as NetCore doesn't use same.Screens
Code coverage now seems to work when using --collect "code coverage" as command line arguments for the VSTS "dotnet" (option "test") task. It warns that it only works for windows environments though.Ashlynashman
Would you be able to follow this up Patrick (pretty please)? I just tried the command dotnet test c:\sln\test.csproj --collect "code coverage" and it works. I'm on VS2017 15.9.11, could we please get the Test > Windows > Code Coverage menu enabled? Seems like that's the last piece of the puzzle.Mitigate
R
7

Coverlet is a cross platform code coverage available as NuGet package.

Just add it on your test project:

dotnet add package coverlet.msbuild

And run it altogether with dotnet test command as parameter:

dotnet test /p:CollectCoverage=true

Supported Formats:

  • json (default)
  • lcov
  • opencover
  • cobertura

I made more detailed implementation about it here: .Net Core Unit Test and Code Coverage with Visual Studio Code

Ramunni answered 15/6, 2018 at 23:20 Comment(0)
K
2

VSTest task cannot run .NET core tests as it uses the Test platform version 1. To run .NET core tests, we recommend using the .NET core task(preview) with the test command.

However, Code coverage and other data collection is not supported yet, no agent support.

We are fixing this issue as part of this https://github.com/Microsoft/vsts-agent/pull/1149/files Will update the thread once fix is gone and new agent is released. thread https://github.com/microsoft/vstest/issues/579#issuecomment-324401462

Source Link: VSTest task fails to execute tests in .NET Core 2.0 test project

Kicker answered 6/9, 2017 at 9:45 Comment(3)
I'm aware of the work from the vsts, I wanted to know how to publish the coverage as DotCover uses the TestWindow/vstestconsole.exe where as NetCore doesn't use same.Screens
Code coverage now seems to work when using --collect "code coverage" as command line arguments for the VSTS "dotnet" (option "test") task. It warns that it only works for windows environments though.Ashlynashman
Would you be able to follow this up Patrick (pretty please)? I just tried the command dotnet test c:\sln\test.csproj --collect "code coverage" and it works. I'm on VS2017 15.9.11, could we please get the Test > Windows > Code Coverage menu enabled? Seems like that's the last piece of the puzzle.Mitigate
K
0

It had been a long time since this question was asked, but I think my answer below will be helpful to execute tests for the projects on .NET Core 2 and to generate coverage report using DotCover tool.

  1. Download and install Jetbrains dotcover console setup
  2. On the command line execute the command as below, which will identify the test projects in the solution and generate coverage report in HTML format. You can also generate in different formats as JSON, NDependXML or DetailedXML.

dotcover analyse /TargetExecutable:"C:\Program Files\dotnet\dotnet.exe" /TargetArguments:"test Path_To_Your_Solution_File" /Output:report.html /ReportType:HTML

You can also run and generate test coverage over .csproj file.

Katrinakatrine answered 31/1, 2018 at 16:57 Comment(2)
I am using dotcover exactly the same as your example. The output window states that it has executed the tests and that the test run was successful, but then it just hangs and doesn't produce any code coverage results. I am using the latest version of dotcover (2017.3.5). Any ideas?Dative
You should use the 2018 version of dotcover. I have faced the same issue upgrading the version fixed the issue specifically for .net core.Screens

© 2022 - 2024 — McMap. All rights reserved.