Profile Unit Tests
The instructions are provided for Windows only. On Linux and macOS, you can use JetBrains Rider to profile unit tests.
There are three ways of profiling unit tests:
Using dotTrace integrated in Visual Studio (Recommended)
Note that if you use the API to control profiling, this is the only possible way to profile a unit test.
note
JetBrains ReSharper must be installed in Visual Studio.
Open a solution in Visual Studio.
Open a file with unit tests.
Choose a test class or a test method.
Click the corresponding side bar mark in the gutter.
In the context menu, choose Profile:
The Unit Test Sessions window opens.
Specify profiling options.
After test execution is completed, a snapshot will be opened in the dotTrace Viewer.
note
If dotTrace is set to run with administrator privileges (the Run this program as an administrator option is enabled in the application Compatibility properties) and Visual Studio is run under a regular user account, dotTrace will not receive the snapshot data. In such a case, either re-run Visual Studio as administrator or set dotTrace to run under a regular user account.
Select a node in the tree view of the Unit Test Sessions window.
Select Profile Unit Tests in the list on the toolbar.
Specify profiling options.
After test execution is completed, a snapshot will be displayed.
Run dotTrace as a standalone application. The dotTrace Home window will open.
Add a run configuration – the configuration that tells dotTrace how to run the profiled application:
Under Choose what you want to profile, New Process Run, click
Add run configuration.
In the New Run Configuration wizard, choose .NET Core Application and click Next.
Depending on which unit testing framework you use, specify the path to its executable file in the Path field. You can work with the following frameworks/executables:
nunit.exe for NUnit.
nunit-console.exe for NUnit.
MSTest.exe for MSTest.
xUnit-console.exe for xUnit.
Specify the path to the .dll file with your unit tests and add arguments (if necessary) in the Arguments field.
If required, specify a full path to the application Working directory (by default, it's the same directory where the executable is located).
If required, use Set environment variables to specify environment variables. Each variable must be specified on a new line.
Click Save.
Make sure the created run configuration is selected in the New Process Run list.
Under Choose how you want to profile it, specify profiling options:
Profiling Type: typically, you should choose between Sampling (evaluate performance and determine the slowest methods in your app) and Timeline (same as Sampling but with additional timeline data). Learn more about profiling types
Optionally, if your application creates a number of processes and you want to profile only specific ones, create process filters.
For example, if you need to profile NUnit tests running under nunit-console.exe, note that NUnit test runner creates a separate process to run tests. Therefore you need to exclude the nunit-console.exe process from profiling. To do this, add the
nunit-console*
filter.If required, specify other profiling options.
Under Run profiling, select Collect profiling data from start.
Click Start. This will start the profiling session.
Collect a snapshot using the Get Snapshot button in the profiling controller window. Learn more about how to control the profiling session
Detach the profiler using the Detach button in the controller window.
Analyze the collected snapshots using the dotTrace Viewer.
Test Runner | Application | Arguments |
---|---|---|
nunit.exe for nUnit | Path to test runner executable. Example: "C: | Path to the dll file with unit tests. Example: "C: |
nunit-console.exe for nUnit | Path to the test runner executable. Example: "C: | Path to the dll file with unit tests and name of xml file where test results are stored. Example: "C: |
MSTest.exe for MSTest | Path to the test runner executable. Example: "C: | Arguments:
Example:
|
xUnit-console.exe for xUnit | Path to the test runner executable. Example: "C: | Path to the dll file with unit tests and name of xml file where test results are stored. Example: "C: |
The resulting snapshot contains lots of information that pertains not only to the unit tests you're profiling but to the test framework itself, making it a challenging task to extract information about unit tests. If you don't find unit test code in the snapshot at all, make sure the tests are actually running. In some cases, MSTest stops before running tests due to the lack of permissions. If this is the case, try running dotTrace as administrator.