4

I have a C# project in Visual Studio 2017 Professional. It has some unit tests: methods declared with [Test] which display a little green tick icon in the editor, where I can right-click and 'Run' or 'Debug' the test. When I do so, a window opens with all tests in the solution and I can click a green 'play' triangle to run them all.

I can also run my program under the profiler starting from Debug -> Performance Profiler.

So far so good. Now, how do I run a unit test under the profiler?

When I first asked this question I looked for profiling unit tests and saw it was a feature in Visual Studio Professional. But in fact, I'm not using VS's own unit testing support, but rather NUnit, driven by Resharper. Sorry to throw everyone off the scent.

Ed Avis
  • 1,350
  • 17
  • 36

3 Answers3

3

Using VS2017 Professional and the Diagnostic Tools you can profile code between two break points!

Thus, you can debug your unit test (xunit, nunit, mstest, whatever!) with any tool (VS Test Explorer, ReSharper, etc) like this:

  1. enable diagnostic tools while debugging (see answer from tm1)
  2. Set a break point at the start of your unit test, another one at the end (or wherever you like)
  3. Debug the unit test with your favorite tool, you hit the first break point.
  4. Open the Diagnostic Tools window (Debug > Windows > Show Diagnostic Tools)
  5. Start Recording a CPU profile (small record button below all those graphs)
  6. Continue (F5) to the next break point
  7. VS will now create a snapshot for this period and you can dig into the relevant methods. Voila!

Just for completeness, an alternative, commercial solution is Jetbrain's DotTrace, which is part of ReSharper Ultimate at the point of writing. Here you could directly profile your unit test without manual break points.

Patrick Stalph
  • 792
  • 1
  • 9
  • 19
1

Edit

According to @patrick-stalph the 3-step proposed solution does not work in Visual Studio 2017 Professional.


Disclaimer

I'm using Visual Studio Enterprise 2017, so I cannot check, whether this works in Professional, or not.


  1. Open Test Explorer e.g. Ctrl + Q, then type Test Explorer and hit Enter
  2. You should see your tests in Test Explorer. If you cannot, Build.
  3. Choose a test, right click it, and choose Profile Test

Workaround

I must note, that I'm using and , and I haven't seen this feature working. See this issue, why I haven't.
Hence, based on the answer of Jeremy Liberman, I was able to work around using the steps below:

  1. Install xunit.runner.console NuGet package to the test project
  2. Click Analyze > Performance Profiler... from the Toolbar.
  3. Change Target to Executable
  4. Click Start
  5. Follow the Wizard, till you reach Choose the application that you want to profile (.EXE, .DLL, Website)
  6. Choose An executable (.EXE file)
  7. Based on Run tests with the xUnit.net console runner, I've specified the following values:
    • Full path: c:\<...>\src\packages\xunit.runner.console.2.4.0\tools\net472\xunit.console.exe (fit this to your environment)
    • Command-line arguments: <Product>.Tests\bin\Debug\<Product>.Tests.dll
    • Working directory: c:\<...>\src\ (the root of the Solution)
  8. Next, Finish

Note, that this profiles all of the tests in the test assembly. You can choose, which tests you want to run by providing more Command-line arguments to xunit.runner.console. To learn more about the console runner options, run the console runner with no command line options.

ike
  • 95
  • 1
  • 1
  • 8
  • Using VS2017 Professional, nunit3, and the nunit test adapter my Test Explorer shows and runs my unit tests. However, There is no "profile test" button as you described. – Patrick Stalph Sep 18 '19 at 07:46
1

If you can debug and stop at a break-point, you should also be able to get CPU and memory usage according to this page, given you enable diagnostics tools while debugging.

tm1
  • 1,180
  • 12
  • 28