简体   繁体   中英

Profile unit test in Visual Studio 2017 + NUnit + Resharper

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.

Disclaimer

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


  1. Open Test Explorer eg 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.

如果您可以在断点处调试和停止,则还应该能够根据此页面获取CPU和内存使用情况,因为您在调试时启用了诊断工具

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM