Run DotCover dotnet test with Cake

693 Views Asked by At

Currently I run DotCover for each text project in my solution. I want to change this to only run DotCover once for the whole solution.

I found this command which works:

C:\_git\MyApp\build\tools\JetBrains.dotCover.CommandLineTools.2021.1.3\tools\dotCover.exe dotnet --output=C:\Temp\report.xml --reporttype=xml -- test "C:\_git\MyApp\MyApp.sln"

My build scripts are written in Cake and I like to execute this command from within Cake. However, online I can only find ways to run DotCover for a single test project like this:

var dotCoverCoverSettings = new DotCoverCoverSettings();
dotCoverCoverSettings.WithAttributeFilter( "System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" );

DotCoverCover( tool => {
  tool.DotNetCoreTest(
    "test. dll",
    new DotNetCoreTestSettings() {                    
      Configuration = configuration
    }
  );
  },
  "out.xml",
  dotCoverCoverSettings
);

Is there a method in Cake for running the first DotCover command using dotnet test for a solution file?

I know about StartProcess but I would prefer a specific method to run DotCover. If StartProcess is the only option, is there a built in way to resolve the path to the dotCover exe (NuGet package #tool nuget:?package=JetBrains.dotCover.CommandLineTools) downloaded by cake?

StartProcess(
@"C:\_git\MyApp\build\tools\JetBrains.dotCover.CommandLineTools.2021.1.3\tools\dotCover.exe",
new ProcessSettings {
    Arguments = new ProcessArgumentBuilder()
        .Append( "dotnet" )
        .Append( @"--output=C:\Temp\report.dotCover.dcvr" )
        .Append( "--reporttype=XML" )
        .Append( @"-- test ""C:\_git\MyApp\MyApp.sln""" )
    }
);

Edit:

As @Nils has suggested; Passing the solution file as argument to DotNetCoreTest works. But DotCover "randomly" freezes when invoked using this code:

DotCoverCover( tool => {
    tool.DotNetCoreTest(
      Paths.SolutionFilePath.ToString(),
      new DotNetCoreTestSettings() {                    
        Configuration = configuration,
        NoBuild = true,
        NoRestore = true
      }
    );
    },
    dotCoverOutputFileName,
    dotCoverCoverSettings
);

It prints the results for some of the test projects and then nothing...

Passed!  - Failed:     0, Passed:    51, Skipped:     0, Total:    51, Duration: 220 ms - Proj1.Test.dll (net5.0)
Passed!  - Failed:     0, Passed:    26, Skipped:     0, Total:    26, Duration: 247 ms - Proj2.Test.dll (net5.0)
Passed!  - Failed:     0, Passed:    28, Skipped:     0, Total:    28, Duration: 249 ms - Proj3.Test.dll (net5.0)
Passed!  - Failed:     0, Passed:    31, Skipped:     0, Total:    31, Duration: 123 ms - Proj4.Test.dll (net5.0)
Passed!  - Failed:     0, Passed:   124, Skipped:     0, Total:   124, Duration: 3 s - Proj5.Test.dll (net5.0)
Passed!  - Failed:     0, Passed:   660, Skipped:     0, Total:   660, Duration: 5 s - Proj6.Test.dll (net5.0)

The problem could be that Cake uses other arguments to run DotCover:

C:/_git/MyApp/build/tools/JetBrains.dotCover.CommandLineTools.2021.1.3/tools/dotCover.exe Cover /TargetExecutable="C:/Program Files/dotnet/dotnet.exe" /TargetArguments="test \"../MyApp.sln\" --configuration Release --no-build --no-restore" /Output="C:/Temp/MyApp.sln.dotCover.dcvr"

Arguments "reccomended" by JetBrains (https://youtrack.jetbrains.com/issue/DCVR-9681)

C:\_git\MyApp\build\tools\JetBrains.dotCover.CommandLineTools.2021.1.3\tools\dotCover.exe dotnet --output=C:\Temp\report.html --reporttype=html -- test "C:\_git\MyApp\MyApp.sln"

I run the last command in a loop to see if i could reproduce the "freeze-issue" but sofare it works.

1

There are 1 best solutions below

2
Nils On

Have you tried setting the full path of the solution as first argument of DotNetCoreTest? (I.e. Giving the solution instead of a project.)

In the end, the the DotNetCoreTester simply passes that as first argument to dotnet test. So.. according to dotnet-test docs that can be [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL>].

Maybe the docs on the cake DotNetCoreAliases are simply not quite as accurate.

EDIT:

While DotNetCoreTester works fine with solutions as arguments and thus is able to run multiple test projects at once, dotCover for .NET Framework tests (which is what is called in DotCoverCover) is not able to process this.

The solution given in the linked youtrack-issue is to use dotCover.exe dotnet, the dotCover syntax for .NET Core which is currently not implemented in Cake.

So it seems you are presented with three options:

  • Use the StartProcess syntax you mentioned above
  • Use the dotCover approach for multiple projects, as laid out in the documentation: Search for all test projects in your solution, cover each one on it's own and finally run DotCoverMerge to merge the reports.
  • Create an issue in the Cake project that describes the missing functionality. (And ideally start working on a PR to implement it.)