SeaLights .NET agent - Capturing coverage from a windows process
There are two ways to capture coverage from windows processes using the SeaLights .NET agent
By running the process with our agent using the testListener option
By running the SeaLights .NET agent as a background listener
The Background Listener option allows you to capture coverage from any application without changing the command you use to run it but requires few more steps than testListener option.
Prerequisites
Microsoft run-time components
Install Microsoft Visual C++ Redistributable for Visual Studio 2017 from: https://www.visualstudio.com/downloads/
The 32bit version is: https://go.microsoft.com/fwlink/?LinkId=746571
The 64bit version is: https://go.microsoft.com/fwlink/?LinkId=746572
You can use the PowerShell commands below to install these prerequisites via a script
iwr -OutFile vc_redist.x86.exe -UseBasicParsing -Uri https://go.microsoft.com/fwlink/?LinkId=746571
Start-Process -FilePath vc_redist.x86.exe -ArgumentList "/Q" -Wait
iwr -OutFile vc_redist.x64.exe -UseBasicParsing -Uri https://go.microsoft.com/fwlink/?LinkId=746572
Start-Process -FilePath vc_redist.x64.exe -ArgumentList "/Q" -Wait
Agent Token and Proxy settings
If needed, configure the coverage collector service by editing the SL.DotNet.CoverageCollectorService.exe.config file:
If you have not placed the sltoken.txt in the agents' folder, then, in Sealights.Token put the token you've received from SeaLights
If a proxy is used, add a key named 'Sealights.Proxy' and set the value to a URL like "http://127.0.0.1:8888"
Running the process with the SeaLights .NET agent using the testListener option
To capture coverage from a single application, you can run it with the SeaLights .NET agent, which will capture coverage from it when it runs, and tests are run against it.
SL.DotNet.exe testListener --buildSessionIdFile buildSessionId.txt --target c:\MyApplication\App.exe --workingDir c:\WorkingDir --targetArgs "-Flag1 true MyTest.dll"
The sample command above is replacing the regular command used to startup the application captured: c:\MyApplication\App.exe -Flag1 true MyTest.dll
executed from the c:\WorkingDir
directory
See SeaLights .NET - Command reference: Starting the Test Listener for full parameters details .
Running the SeaLights .NET agent as a background listener
To capture coverage from any application without changing the command you use to run it, you can run the SeaLights .NET agent as a background process, which will capture coverage from any process that has the SeaLights .NET agent defined as a profiler.
Starting the SeaLights .NET agent background listener
To run the background process, you use the startBackgroundTestListener flag and provide a unique session key:
SL.DotNet.exe startBackgroundTestListener --buildSessionIdFile buildSessionId.txt --testListenerSessionKey SL_CovCollectorAgent
Setting the SeaLights .NET agent as a profiler
Now, you need to run (all) your processes - i.e. including your application - with the SeaLights .NET agent defined as a profiler.
This is done by setting the following environment variables in the shell used to run your process:
Cor_Profiler set to
{01CA2C22-DC03-4FF5-8350-59E32A3536BA}
Cor_Enable_Profiling set to 1
Cor_Profiler_Path_32 set to the path of SL.DotNet.ProfilerLib_x86.dll
Cor_Profiler_Path_64 set to the path of SL.DotNet.ProfilerLib_x64.dll
SL_CollectorId set to the same session key provided to the
startBackgroundTestListener
command
You should unset the environment variables before running any process that you do not want to capture coverage from
Setting the profiler in Windows Cmd or Powershell script
You can use the following commands to declare the environments depending on your context
Windows command prompt | Powershell |
---|---|
POWERSHELL
|
POWERSHELL
|
Setting the profiler in Team City
In TeamCity, you can use the dedicated service message in your build script to dynamically update these build parameters right from a build step so that the following build steps will run with a modified set of build parameters.
echo "##teamcity[setParameter name='env.Cor_Profiler' value='{01CA2C22-DC03-4FF5-8350-59E32A3536BA}']"
echo "##teamcity[setParameter name='env.Cor_Enable_Profiling' value='1']"
echo "##teamcity[setParameter name='env.Cor_Profiler_Path_32' value='c:\sealights\x64\SL.DotNet.ProfilerLib_x86.dll']"
echo "##teamcity[setParameter name='env.Cor_Profiler_Path_64' value='c:\sealights\x64\SL.DotNet.ProfilerLib_x64.dll']"
echo "##teamcity[setParameter name='env.SL_CollectorId' value='SL_CovCollectorAgent']"
Stopping the SeaLights .NET agent background listener
Once you have finished collecting coverage from the process, you need to stop the background listener. This is done by using the stopBackgroundTestListener flag with the session key previously used
SL.DotNet.exe stopBackgroundTestListener --buildSessionIdFile buildSessionId.txt --testListenerSessionKey SL_CovCollectorAgent
See 'SeaLights .NET - command reference' for full parameter details of all the commands listed above.