Perform Unit Testing together with the Profiler

It may sometimes be useful to perform unit testing (e.g. using NUnit) together with the profiler, especially when the .NET Memory Profiler API is used.

There are a few different ways of running unit tests under the profiler:

      Start the unit test runner using the Profile application command

      Use NmpCore to start the unit test runner

      Use the NmpCore task in MSBuild (and Visual Studio)

      Use the Run tests under profiler command in Visual Studio

Start the unit test runner using the Profile application command

To run a unit test program under the profiler; you need to select the program to run (e.g., NUnit-console.exe, if running NUnit) and supply any necessary program arguments to the unit test runner.  

If using a test framework that may spawn separate test processes (e.g. VSTest), it may be necessary to make sure that all created processes are profiled (see screenshot) argument, or to prevent separate process using the VSTest argument /NoIsolation.

Note that the possibility to profile all processes is only available in the Enterprise edition.

Use NmpCore to start the unit test runner

The NmpCore tool is well suited to run automated memory unit tests. The unit test runner can be started in the same way as when using the Profile application command in .NET Memory Profiler, i.e. by specifying the unit test runner and its arguments. The following command line can, for instance, be used to start NUnit under NmpCore:

NmpCore [path]\nunit-console.exe [arguments to NUnit]

If using a test framework that may spawn separate test processes (e.g. VSTest), it may be necessary to make sure that all created processes are profiled, using the /profileprocess argument, or to prevent separate processes using the VSTest argument /NoIsolation. For example:

NmpCore /profileprocess:all [path]\vstest.console.exe [arguments to VSTest]

or

NmpCore [path]\vstest.console.exe /NoIsolation [additional arguments to VSTest]

NOTE. If multiple processes are included in a session, it can only be opened using the Enterprise edition of .NET Memory Profiler.

For more information about command line arguments, see Command Line Arguments.

Another option is to create a unit test project in .NET Memory Profiler and then use that project to profiler the test runner.

1.  Create a new profiler project in .NET Memory Profiler (File->New profiler project)

2.  Select Unit Tests as the process to profile.

3.  Select the unit test framework to use (VS Test, NUnit, XUnit, or .NET Core test).

4.  It is recommended that the Tool path to the unit test runner is specified. The profiler will try to locate VS Test and .NET Core Test, but NUNit and XUnit will only be found when using the NmpCore task in a project where the unit test runner is added as a NuGet package.

5.  Specify any additional arguments needed for the test runner.

6.  Click Next to specify additional session settings, or Finish to accept the default settings.

7.  Save the project.

8.  Use NmpCore to start profiling using the saved project. For example by using the command:

NmpCore /project:[path]\TestProject.prfproj

It is also possible to start this project from .NET Memory Profiler, e.g. by right-clicking the project and then select Start profiler project.

The main advantage of using the Unit Test profiling type is that it automatically profiles multiple processes (e.g. additional processes created by the test runner) and that the created session files can be opened by the Professional edition, even if it contains multiple processes.

NOTE. Normally, .NET Memory Profiler keeps the debug symbols file (.PDB) open when reading symbol information for an assembly. However, this will prevent the unit tested assembly from being recompiled while testing. To prevent this, the /shadowsymbols command line argument can be used.

Use the NmpCore task in MSBuild

The NmpCore MSBuild task can also be used to start profiling unit tests. The NmpCore task is available as a NuGet package: SciTech.NmpCore.Task. To automatically run a profiler project after each build:

1.  Add the SciTech.NmpCore.Task package to the Visual Studio project.

2.  Add a profiler project to a Visual Studio project. See Use NmpCore to start the unit test runner for information about how the create the profiler project.

3.  Set the Build action to NmpCore for the profiler project.

For more information about the NmpCore task, see NmpCore MSBuild Task.

Use the Run tests under profiler command in Visual Studio

In the Enterprise edition you can easily run unit tests under the profiler by using the command “Profiler->Run tests under the Profiler” in Visual Studio. For more information, see Run Tests under the Profiler.

.NET Memory Profiler User Manual

© Copyright 2002-2017. SciTech Software AB.

For information about .NET Memory Profiler, see the product site at http://memprofiler.com

.NET Memory Profiler is developed by SciTech Software AB