-
Notifications
You must be signed in to change notification settings - Fork 18
Examples of use
These are drawn (almost) directly from the AltCover build process, and the notes here refer to the Modes of Operation page in places. They are emphatically not exhaustive -- AltCover is amenable to mixing-and-matching each mode of operation with each of its instances (classic tool, running the dotnet
dll, the various dotnet
tools, the cmdlet, the MSBuild tasks) against any sort of target; and there are many different controlling parameters to use. It's only the dotnet test
integration which implies a "One True Way" of working; and that one still provides a lot of parameters for controlling what you get out.
This is an example of "Instrument now, test later, classic mode", generating NCover format output, with all the coverage data held in memory during the unit test process, and written to the report file as a terminating action of the NUnit
process
C:\projects\altcover\_Binaries\AltCover\Debug+AnyCPU\AltCover.exe /sn=C:\projects\altcover\Build\SelfTest.snk -s=Adapter -s=Mono -s=\.Recorder -s=Sample -s=nunit -e=Tests -t=System. -t=Sample3\.Class2 /o=./__UnitTestWithAltCover -x=C:\projects\altcover\_Reports\UnitTestWithAltCover.xml
C:\projects\altcover\packages\NUnit.ConsoleRunner.3.8.0\tools\nunit3-console.exe "--noheader" "--labels=All" "--work=." "--result=./_Reports/UnitTestWithAltCoverReport.xml" "C:\projects\altcover\_Binaries\AltCover.Tests\Debug+AnyCPU\__UnitTestWithAltCover\AltCover.Tests.dll" "C:\projects\altcover\_Binaries\AltCover.Tests\Debug+AnyCPU\__UnitTestWithAltCover\Sample2.dll"
This is the way I used the c.2010 version (in the pre-OpenCover days), inserting the instrumentation step (which injects the coverage tracking into the assemblies, and prepares the coverage report) ahead of the unit tests (during which, the instrumentation records the coverage, and then writes it to the previously prepared report), and is probably the simplest approach for the classic framework or mono use cases.
This can be done as a single step in the "Instrument and test now, classic mode" by putting the test executable call after a --
on the command line
C:\projects\altcover\_Binaries\AltCover\Debug+AnyCPU\AltCover.exe /sn=C:\projects\altcover\Build\SelfTest.snk -s=Adapter -s=Mono -s=\.Recorder -s=Sample -s=nunit -e=Tests -t=System. -t=Sample3\.Class2 /o=./__UnitTestWithAltCover -x=C:\projects\altcover\_Reports\UnitTestWithAltCover.xml -- "C:\projects\altcover\packages\NUnit.ConsoleRunner.3.8.0\tools\nunit3-console.exe" "--noheader" "--labels=All" "--work=." "--result=./_Reports/UnitTestWithAltCoverReport.xml" "C:\projects\altcover\_Binaries\AltCover.Tests\Debug+AnyCPU\__UnitTestWithAltCover\AltCover.Tests.dll" "C:\projects\altcover\_Binaries\AltCover.Tests\Debug+AnyCPU\__UnitTestWithAltCover\Sample2.dll"
Note though that for the default OpenCover format output, this just fills in the visit counts, and does not compute the derived quantities (percentage visited, CRAP score and the like), whether you do it in one line or two, because this is all being done in the short time window allotted to a Process Exit handler.
This is an example of "Instrument now, test later, runner mode" use. It's similar to the above, but instead of handling the coverage in-memory in the unit test process, it gets written to file, and is processed by the enclosing AltCover instance
C:\projects\altcover\_Binaries\AltCover\Debug+AnyCPU\AltCover.exe /sn=C:\projects\altcover\Build\SelfTest.snk -s=Adapter -s=Mono -s=\.Recorder -s=Sample -s=nunit -e=Tests -t=System. -t=Sample3\.Class2 /o=./__UnitTestWithAltCoverRunner -x=C:\projects\altcover\_Reports\UnitTestWithAltCoverRunner.xml
C:\projects\altcover\_Binaries\AltCover\Debug+AnyCPU\AltCover.exe Runner -x C:\projects\altcover\packages\NUnit.ConsoleRunner.3.8.0\tools\nunit3-console.exe -r C:\projects\altcover\_Binaries\AltCover.Tests\Debug+AnyCPU\__UnitTestWithAltCoverRunner -w . -- --labels=All --noheader --work=. --result=./_Reports/UnitTestWithAltCoverRunnerReport.xml "C:\projects\altcover\_Binaries\AltCover.Tests\Debug+AnyCPU\__UnitTestWithAltCoverRunner\AltCover.Tests.dll" "C:\projects\altcover\_Binaries\AltCover.Tests\Debug+AnyCPU\__UnitTestWithAltCoverRunner\Sample2.dll"
This is probably the simplest approach for the classic framework or mono use cases if you want the full OpenCover format options.
This is an example of "Instrument now, test later, runner mode" use
[00:08:13] C:\Program Files\dotnet\dotnet.EXE C:\projects\altcover\_Binaries\AltCover\Release+AnyCPU\netcoreapp2.0\AltCover.dll --single -s=Adapter -s=Mono -s=\.Recorder -s=Sample -s=nunit -e=Tests -t=System. -t=Sample3\.Class2 -x "C:\projects\altcover\_Reports\UnitTestWithAltCoverCoreRunner.xml" /o "C:\projects\altcover\Tests\_Binaries\AltCover.Tests\Debug+AnyCPU\netcoreapp2.0"
[00:08:15] C:\Program Files\dotnet\dotnet.EXE C:\projects\altcover\_Binaries\AltCover\Release+AnyCPU\netcoreapp2.0\AltCover.dll Runner -x "dotnet" -r "C:\projects\altcover\Tests\_Binaries\AltCover.Tests\Debug+AnyCPU\netcoreapp2.0" -- test --no-build --configuration Debug --verbosity normal C:\projects\altcover\Tests\altcover.tests.core.fsproj
This one exploits the way that I've redirected the $(OutputDirectory)
in the project to be relative to $(SolutionDir)
(#include
rant about how dotnet
means you can't have the source tree being read-only) and instruments from the solution build to the project build (so as to avoid --inplace
issues).
This is an example of "Instrument now, test later, collect coverage after that" mode
C:\Program Files\dotnet\dotnet.EXE C:\projects\altcover\_Binaries\AltCover\Release+AnyCPU\netcoreapp2.0\AltCover.dll --inplace --save -s=Adapter -t "System\." -t "Microsoft\." -x "C:\projects\altcover\_Reports\AltCoverFSharpTypesDotNetCollecter.xml"
C:\Program Files\dotnet\dotnet.EXE test --no-build --configuration Debug sample2.core.fsproj
C:\Program Files\dotnet\dotnet.EXE C:\projects\altcover\_Binaries\AltCover\Release+AnyCPU\netcoreapp2.0\AltCover.dll Runner --collect -r "C:\projects\altcover\Sample2\_Binaries\Sample2\Debug+AnyCPU\netcoreapp2.0"
This is another example of "Instrument now, test later, collect coverage after that" mode, just with each step as an MSBuild task. This shows an example of setting an array property; in this case tracking for unit tests and timing information through CallContext
. The CallContext
parameter is of fairly specialized use; it's just that this example is taken from a test that runs through some of the corner cases
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="AltCover.Prepare"
AssemblyFile="$(MSBuildThisFileDirectory)../_Packaging/Unpack/tools/netcoreapp2.0/AltCover.dll"/>
<UsingTask TaskName="AltCover.Collect"
AssemblyFile="$(MSBuildThisFileDirectory)../_Packaging/Unpack/tools/netcoreapp2.0/AltCover.dll"/>
<ItemGroup>
<CallContext Include="0" />
<CallContext Include="[Fact]" />
</ItemGroup>
<Target Name="MSBuildTest">
<Exec Command="dotnet build $(MSBuildThisFileDirectory)../Sample4/sample4.core.fsproj" />
<AltCover.Prepare
InputDirectory="$(MSBuildThisFileDirectory)../Sample4/_Binaries/Sample4/Debug+AnyCPU/netcoreapp2.0"
OutputDirectory="$(MSBuildThisFileDirectory)../Sample4/_Binaries/Sample4/Debug+AnyCPU/netcoreapp2.0/__Saved$([System.DateTime]::UtcNow.ToString().Replace(':','-').Replace('/','-').Replace(' ','+'))"
Report="$(MSBuildThisFileDirectory)../_Reports/MSBuildTest.xml"
CallContext="@(CallContext)"
/>
<Exec Command="dotnet test --no-build $(MSBuildThisFileDirectory)../Sample4/sample4.core.fsproj" />
<AltCover.Collect
RecorderDirectory="$(MSBuildThisFileDirectory)../Sample4/_Binaries/Sample4/Debug+AnyCPU/netcoreapp2.0"
/>
</Target>
</Project>
Having previously added the nuget package
C:\Program Files\dotnet\dotnet.EXE test -v n /p:AltCover=true
C:\Program Files\dotnet\dotnet.EXE altcover --inplace -x "C:\projects\altcover\_Reports\DotnetCLIIntegration.xml" -i "C:\projects\altcover\_DotnetCLITest\_Binaries\Sample4\Debug+AnyCPU\netcoreapp2.0"
C:\Program Files\dotnet\dotnet.EXE altcover Runner -x "dotnet" -r "C:\projects\altcover\_DotnetCLITest\_Binaries\Sample4\Debug+AnyCPU\netcoreapp2.0" -- test --no-build --configuration Debug
altcover --inplace -x "C:\projects\altcover\_Reports\DotnetGlobalIntegration.xml" -i "C:\projects\altcover\_DotnetGlobalTest\_Binaries\Sample4\Debug+AnyCPU\netcoreapp2.0"
altcover Runner -x "dotnet" -r "C:\projects\altcover\_DotnetGlobalTest\_Binaries\Sample4\Debug+AnyCPU\netcoreapp2.0" -- test --no-build --configuration Debug
Invoke-AltCover -Report $x -OutputDirectory $o -InputDirectory $i -AssemblyFilter "Adapter" -InformationAction Continue
Invoke-AltCover -InformationAction Continue -Runner -RecorderDirectory $o -WorkingDirectory "./Sample2" -Executable "dotnet" -CommandLine @("test", "--no-build", "--configuration", "Debug", "sample2.core.fsproj")