Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you profile unit tests in VS2017

VS2017 has the ability to profile applications. Is there also the ability to profile unit tests?

like image 751
Iain Macnab Avatar asked Nov 12 '17 12:11

Iain Macnab


2 Answers

As per the other answer from @Wizou, the option to profile a unit test was missing for me in VS2019 professional.

Instead, what I did was this:

  • First open a command line window, and navigate to the directory where your unit test dll's build output to
  • Run vstest.console.exe telling it the specific dll to load and test to run, e.g. vstest.console.exe MyProject.Test.dll /Tests:TestFooBar, and check that it works
  • Now, do the same thing from within the VS2019 profiler:
  • Choose target of "Executable" and find vstest.console.exe (tip: mine was C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
  • Command Line options as above
  • Working directory where your unit test DLL lives
  • Launch the profiling session, and enjoy the results.

Note: You'll probably find that most of the time is dominated by the test framework loading, static constructors, reflection and JITing things, and there's too much noise to determine which parts of your actual test are slow. To help, this dirty trick is quite effective:

[TestMethod]
public void TestFooBar()
{
    for (int i = 0; i < 10000; i++) // extra iterations for profiling. Vary this number if 10k is too small
    {
        // your normal unit test which you'd only run once goes here
    }
}

like image 138
Orion Edwards Avatar answered Sep 19 '22 22:09

Orion Edwards


I am under VS Professional Edition and it is not available either.

It probably requires VS Enterprise Edition.

See also https://social.msdn.microsoft.com/Forums/en-US/49513731-23e6-4b65-acce-ed0a98e19b59/no-profile-test-option-in-context-menu-on-test-explorer-in-visual-studio-professional-2013?forum=vsunittest and https://visualstudio.microsoft.com/vs/compare/

like image 20
Wizou Avatar answered Sep 22 '22 22:09

Wizou