Recently, the project involved applications developed by C #. During the test, we need to analyze the code coverage rate of C #. I asked some people and found some on the Internet, the system found a lot of information, but it was not completely complete. In actual use, it still took a lot of detours. Up to now, two feasible solutions have been successfully tested. I will write them here to share with you. It may have been used by many people.
Method 1: Use the vsinstr tool provided by
Prerequisites: Install VS2010 or VS2012.
There are three basic steps.
1. Insert the DLL to be tested.
"C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Team Tools \ Performance Tools \ x64 \ vsinstr"-coverage "D: \ xxx. dll"
The original dll is automatically renamed as *. orig, which is equivalent to a backup. We can see that the DLL size of the Inserted Pile has increased.
2. Start coverage data monitoring and collection.
Start vsperfmon-coverage-output: "D: \ xyz. coverage"/CS/user: everyone
3. Run the program to be tested or wait for execution.
4. Stop the executed program and close the application directly.
5. Stop coverage listening and generate data.
Vsperfcmd/shutdown
6. Find D: \ xyz. coverage and double-click it to open it with. The block coverage rate is displayed. If the source code is available, you can view the block coverage rate in the code.
Method 1:
1. In addition to viewing the report, this method does not need to start VS or limit how the tested code is tested. You can also manually or other automation programs.
2. Currently, DLL plug-ins need to be installed one by one. I have not tried the exe file.
3. The generated coverage report is a binary file and needs to be viewed in.
4. Currently, only block-level coverage data is available, and line-level data is not found.
Method 2: Use the free OpenCover Tool
Prerequisites:
1. Install the OpenCover tool. You can download the msi or zip package directly.
2. Download The ReportGenerator tool to convert the XML report generated by OpenCover into a more readable HTML report.
Specific steps:
1. In VS, you can use C # To write a small piece of code, including a branch, which is used to view the coverage and coverage. Compile it into leleapplicaiton1.exe
2. Use opencoverto start the tested leleapplicaiton1.exe on the command line and add relevant parameters.
-Output: c2.xml indicates the report file name to be generated.
-Targetdir: Tell OpenCover where the PDB corresponding to this exe is. Both methods are required for PDB files.
-Register is not very clear about its purpose, but it is required. This parameter is not added to the coverage data handler for the first time.
For more detailed parameters, see the OpenCover manual.
Specific commands:
Opencover.lele.exe-register-target: "C: \ Users \ rickyqiu \ Documents \ Visual Studio 2012 \ Projects \ ConsoleApplication1 \ ConsoleApplication1 \ bin \ Debug \ ConsoleApplication1.exe"-output: c2.xml-targetdir: "C: \ Users \ rickyqiu \ Documents \ Visual Studio 2012 \ Projects \ ConsoleApplication1 \ ConsoleApplication1 \ bin \ Debug"