NMP Data Collector is a VSTest data collector, available as a NuGet package . It makes it very easy to run unit tests under the profiler within a build process, e.g. a CI/CD pipeline.
To use the data collector for a project, the SciTech.NmpDataCollector package should be added to the project. The data collector is then enabled in a few different ways, depending on how the tests are run:
The data collector is enabled by using the /Collect
command line argument to vstest.console.exe:
vstest.console.exe <Test library> /Collect:"NMP Data Collector"
The data collector is enabled by using the --collect
command line option to the dotnet test command:
dotnet.exe test <Test library or project> --collect "NMP Data Collector"
To run the data collector using the Visual Studio Test explorer, it has to be configured using a .runsettings file. The data collector is enabled by specifying "NMP Data Collector" as the DataCollector in the .runsettings file:
<?xml version="1.0"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="NMP Data Collector" />
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
The data collector can also be enabled in a build pipeline. Below you will find an example of how it can be enabled in an Azure pipeline.
- task: DotNetCoreCLI@2
displayName: 'Run tests under profiler'
inputs:
command: 'test'
projects: '<Test project>.csproj'
arguments: '-c Release --collect "NMP Data Collector"'
In addition to enabling the data collector for the Visual Studio test explorer, the .runsettings file can be used to configure the collector and the profiler.
Profiler sessions can be included as attachments to the test session or individual test cases by enabling the configuration options: SendSessionForTestCase
and SendSessionForTestSession
(available
options are Always
, WhenFailed
, and Never
).
The configuration below will always attach the profiler session to the test session, and additionally attach profiler sessions to each failed test.
<?xml version="1.0"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="NMP Data Collector">
<Configuration>
<SendSessionForTestSession>Always<SendSessionForTestSession>
<SendSessionForTestCase>WhenFailed</SendSessionForTestCase>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
If a test fails, the session will be included as an attachement. For instance in an Azure pipeline:
Use the MemProfiler.BeginTest API to associate profiler snapshots with a unit test. BeginTest
can be called from the test itself.
[TestMethod]
public void TestMethod()
{
Assert.IsTrue(MemProfiler.AreAssertionsEnabled);
using( MemProfiler.BeginTest(nameof(TestMethod));
{
// Do test...
}
}
Or in the test setup method, with a matching call to EndTest
in the tear down method:
[SetUp]
public void Setup()
{
MemProfiler.BeginTest(TestContext.CurrentContext.Test.Name);
}
[TearDown]
public void TearDown()
{
MemProfiler.EndTest();
}
See the API examples for more information about the .NET Memory Profiler API.
Profiler settings for the data collector can be configured using a profiler project file. The project file is specified using the ConfigFile
element
in the Configuration
section. Only the settings from the project file will be used, the process to launch (if specified) will be ignored.
<?xml version="1.0"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="NMP Data Collector">
<Configuration>
<ConfigFile>DataCollectorSettings.prfproj</ConfigFile>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
The following NuGet packages related to automated memory testing using .NET Memory Profiler are available: