Skip to content

The AltCover data collector and `dotnet test`

Steve Gilham edited this page Jul 2, 2020 · 2 revisions

One major limitation encountered in naive integration with dotnet test is that the vstest.console process imposes a 100ms guillotine, even though .net Core imposes no time-limit of its own, which severely constrains any end-of-test-process flushing or processing of code visit data.

The AltCover data collector is a response to this, by hooking the test driver process through its defined plug-in API, rather than using the general purpose EndProcess event for tidying up.

The AltCover.targets file automates the use of the collector by overriding the default VSTest target from Microsoft.TestPlatform.targets, as well as performing a number of other utility functions when running dotnet test /p:AltCover=true.

Before the actual test operation, the target AltCoverPreFlight is executed to handle the /p:AltCoverForce parameter, and either remove stale data or fail the test. The optional build step then happens (unless --no-build is specified) and then the AltCoverRunPreparation target performs the instrumentation on the binaries currently present in the project output. If that step fails, then from build 7.1.778, the VSTest target terminates.

Assuming all is well, the AltCover.RunSettings task is then executed. This either creates a new run settings file containing a single InProcDataCollector element like

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <InProcDataCollectionRunSettings>
    <InProcDataCollectors>
      <InProcDataCollector friendlyName="AltCover" uri="InProcDataCollector://AltCover/Recorder/{altcover version}" assemblyQualifiedName="AltCover.DataCollector, {altcover assembly name}" codebase="{altcover assembly location}">
        <Configuration>
          <Offload>true</Offload>
        </Configuration>
      </InProcDataCollector>
    </InProcDataCollectors>
  </InProcDataCollectionRunSettings>
</RunSettings>

if no run settings are specified, or copies any existing settings and injects the InProcDataCollector element into the copy. The resulting run settings are then used to perform the tests.

There are no other configuration settings passed in here; the assemblies under test are already instrumented, so the data collector simply locates the injected AltCover.Recorder.g.dll in the test environment and sets a flag to indicate to the visit recording mechanism that it will handle the actual result flushing during the TestSessionEnd step, and not leave it to the possibly truncated EndProcess handler.

After the VSTest target, the AltCoverGenerateCoverageResult executes, either as an error handler (if /p:AltCover=true /p:AltCoverFailFast=false and the pre-flight step succeeded), or as a normal target in sequence (if /p:AltCover=true) to generate the coverage report and restore the uninstrumented state of the project.