In this blog, we are talking about the use of code coverage to determine how much code is being tested. To determine what proportion of your project’s code is actually being tested by coded tests such as unit tests, you can use the code coverage feature of Visual Studio.
What Is Code Coverage?
Code coverage is to determine to what portion of your project code is being tested by Unit testing; you can use the code coverage feature of Visual Studio. To guard effectively against bugs, your tests should exercise or ‘cover’ a large proportion of your code.
Let’s Start,
Code coverage is an option, when you run the test methods, using Test Explorer.
Analyze code coverage on unit tests in Test Explorer
- On the Test menu, choose Analyze Code Coverage.
Image 1: Analyze code coverage code coverage Results
Image 2: Code coverage results - To see which lines have been run, choose Show Code Coverage Coloring Icon Show Code Coverage Coloring.
Test MethodImage 3: Test method
Actual MethodImage 4: Actual method
Discussion on Code Coverage %
When any programmer starts writing the Unit testing, he will have one question in mind — how much do we need to aim for code coverage percentage. Thus, he will go and keep on asking his Manager about code coverage percentage. The total percentage of code coverage depends on your project manager.
You should focus on writing good unit test cases instead of focusing on the code coverage percentage.
Where is the code coverage file in the project directory?
You can find *.coveragexml file in ~\ProjectName\TestResults\uniqueGuid\Pc_UserName.coverage.
I hope you guys understand how I can do this. Let me know if you face any difficulties.
You can watch my next blog. How To Generate Report In HTML Using *.converge File.
You can watch my previous blog here.
Happy Coding {;} ????