Wednesday, August 27, 2014

Reusable command line code coverage using MSTest

Its a universal fact that running tests and code coverage using Visual Studio takes up a lot of time and resources and eventually you need to reboot your machine to get back to speed when yours is a big solution with lot of tests and projects.

Running Tests via command line is a very effective solution to above mentioned problems.

Nam G has an excellent post about how to proceed with it. Hence i have just provided the batch file here which can be useful and be shared. Make sure you run this batch file in the VS command prompt.
Just copy the below code in notepad and save it as batch file

 cd C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools  
 erase H:\coverage\*.*  
 vsinstr /coverage %1  
 vsperfcmd /start:coverage /output:H:\coverage\code  
 mstest /testcontainer:%2 /test:%3 /resultsfile:H:\coverage\testResults.trx  
 vsperfcmd /shutdown  

Explanation of above script is as below :
"H:\Coverage" is the base directory where we will store all the output files, you can changes this as per your wish. Just make sure to change the same in entire script though.

%1, %2, %3 are all the parameters that script needs.

%1 is the path of the dll that you want the cover via code coverage. The path is the same as your Test Project output directory. For eg, if you have a project called Web, and correcponding Test project, the path for %1 would be Test/bin/Web.dll  and not Web/bin/Web.dll.

%2 is the path of Test dll

%3 is the name of Test as per MSDN, you can directly use the Name of your TestClass as well.

Line 1 change the path of the prompt to the performance tools directory where commands like vsinstr etc are housed.
Line 2  Delete all the previous execution output files from our base directory
Line 3  instrument our class to be covered
Line 4  Set the coverage output file.  In the script above code is the name of the file
Line 5 Run all the test using MSTest and specify the results file path.
Line 6 shutdown the monitoring

If you excute the script above , There should be a file "code.coverage" which can be opened in Visual Studio and you can see all the coverage details the way you have been doing. And the file testResults.trx can also be opened in VS and will list the details of Test run.  

No comments:

Post a Comment

There was an error in this gadget