altcover

F# Fake and Cake integration v7.x and later

APIs for use with build scripting tools are provided in the AltCover.Cake.dll and AltCover.Fake.dll assemblies, which are present in the AltCover.Api nuget package

Fake versions are normally supported for six months after release (when Fake itself deprecates old versions), but deprecation of older versions is not eager. Cake version support is based on actual semantic API versioning, with the intent of going as far back as can be compatible with supporting the current release. Check the AltCover release notes to see how far back support actually extends : see here and here.

NOTE: dotnet SDK v7.0.100 requires special treatment for driving AltCover through dotnet test. See here and here.

Fake integration

Found in AltCover.Fake.dll
Detailed API documentation is presented here.

To use the Fake dotnet test API Fake.DotNet.DotNet.test

Driving dotnet test in a Fake script

In the project(s) to be covered, insert at least

    <PackageReference Include="altcover.api" Version="<whatever>">
      <IncludeAssets>build;</IncludeAssets>
    </PackageReference>

with the relevant script fragment (based on the AltCover build script here)

#i "nuget: https://api.nuget.org/v3/index.json"

#r "nuget: AltCover.Api"
#r "nuget: Fake.DotNet.Cli"

let ForceTrue =
  AltCover.DotNet.CLIOptions.Force true

let p =
  { AltCover.Primitive.PrepareOptions.Create() with
      CallContext = [| "[Fact]"; "0" |]
      AssemblyFilter = [| "xunit" |] }

let prepare =
  AltCover.AltCover.PrepareOptions.Primitive p

let c =
  AltCover.Primitive.CollectOptions.Create()

let collect =
  AltCover.AltCover.CollectOptions.Primitive c

open AltCover.Fake.DotNet // extension method WithAltCoverOptions

Fake.DotNet.DotNet.test
  (fun to' -> to'.WithAltCoverOptions prepare collect ForceTrue)
  "dotnettest.fsproj"

Cake integration

Applies to Cake 2.0 and up (with harmless warnings if used with Cake 3.0 or later)

Found in AltCover.Cake.dll
Detailed API documentation is presented here.

To use the Cake dotnet test API DotNetTest

In the test project(s), insert at least

    <PackageReference Include="altcover.api" Version="<whatever>">
      <IncludeAssets>build;</IncludeAssets>
    </PackageReference>

In your .cake file include

#addin "nuget:?package=Microsoft.TestPlatform.ObjectModel&Version=16.1.1"
#addin "nuget:?package=PowerShellStandard.Library&Version=5.1.0"
#addin "nuget:?package=altcover.api&Version=<whatever>"
#addin "nuget:?package=altcover.cake&Version=<whatever>"

the first two needed to silence warnings.

Implement the needed interfaces (as documented here) e.g. by overriding the default types

which have empty or false values for all relevant properties, changing values as required e.g.

  class MyPrepareOptions : AltCover.Cake.PrepareOptions
  {
    public string ReportName { get; set; }
    // here, for sake of example, reportDirectory being a script parameter
    public override string Report => Path.Combine (reportDirectory, ReportName);
    public override IEnumerable<string> CallContext => new string[] {"[Fact]", "0"};
    public override System.Diagnostics.TraceLevel Verbosity => System.Diagnostics.TraceLevel.Verbose;
  }

then your test-with-coverage phase looks like

{
    // do any required customizations here such as
    var prep = new MyPrepareOptions() {
        ReportName = "coverage.build.cake." + cakeversion +".xml"
    };

    var altcoverSettings = new AltCover.Cake.CoverageSettings {
        PreparationPhase = prep,
        CollectionPhase = new CollectOptions(),
        Options = new TestOptions()
    };

    var testSettings = new DotNetTestSettings {
        Configuration = configuration,
        NoBuild = true,
    };

    // mix-in the AltCover coverage settings explicitly
    testSettings.ArgumentCustomization = altcoverSettings.Concatenate(testSettings.ArgumentCustomization);

    // test using the default alias
    DotNetTest("./_DotnetTest/cake_dotnettest.fsproj", testSettings);

As the AltCover.Cake assembly targets Cake 2.0 and netcoreapp3.1 for compatibility, when used for Cake 3.0 there will be harmless warnings like

The assembly 'AltCover.Cake, Version=[whatever], Culture=neutral, PublicKeyToken=null'
is referencing an older version of Cake.Core (2.0.0).
For best compatibility it should target Cake.Core version 3.0.0.

but there will be no warnings about obsolescent types or methods being used.