In this article, I am going to present a step by step demo on how to improve code quality by using Visual Studio Tools like:
- Unit Testing: Writing piece of code to test another piece of code or functionality.
- Code Coverage: This tells what lines of code getting executed during testing process.
- Test Impact Analysis: This tells what test need to be run depending on under line code changes.
Following tables shows features availability in different Visual Studio 2010 editions.
|
Professional |
Premium |
Ultimate |
Unit Testing |
Yes |
Yes |
Yes |
Code Coverage |
No |
Yes |
Yes |
Test Impact Analysis |
No |
Yes |
Yes |
Step 1 : Create class library
Class Library Name: MathLibrary
Class Name: CalculateBasic
Methods: AddNumbers, SubtractNumbers, MultiplyNumbers and DivideNumbers as shown below
Step 2 : Create Unit Test
Right any method and click "Create Unit Tests"
Window will pop up. Select all methods and set Test Generation settings for file, class and method name. Click OK.
Enter project name Unit Test project.
New project created "MathUnitTest" with class CalculateBasicTest.cs having test methods for AddNumbersTest, SubtractNumbersTest, MultiplyNumbersTest and DivideNumbersTest as shown below. Remove Assert.Inconclusive statement from each method.
Step 3 : Test Unit Test
Click on Test Menu and select Run --> All Test in solutions.
Here DivideNumbersTest fails because this unit test is trying divide by 0.
Change code to handle if 0 value is passed.
Again click on Test Menu and select Run --> All Test in solutions. And now all tests are passed.
Step 4: Enable Code Coverage and Test Impact Analysis
To enable Code Coverage and Test Impact Analysis, click on Test --> Edit Test Settings --> Local (local.testsettings)
Test settings windows pops up.
- Select Data and Diagnostics on left menu
- On Right side bottom screen, select Code Coverage and Test Impact
- Select Code Coverage and click Configure
A new windows pops up. Select assemblies to be included for Code Coverage. Here select both MathLibrary and MathUnitTest. Click OK.
Step 5: Check Code Coverage
Now again run all Unit Tests.
All result is passed. Right click any test and select Code Coverage Results.
It shows code coverage in terms Not Covered and Covered lines by Unit Tests. Here Add, Subtract and Multiply method Covered Lines = 3 and Not Covered lines = 0. This shows that Unit Test covered all code. The Divide method Covered Lines = 4 and Not Covered lines = 2. This shows Unit Test has not covered 2 lines of code. Right Click as show below and click Go to source code.
The source code is divided in to two colors
- Code Covered
- Code Not Covered
Step 6: Test Impact Analysis
The purpose of test impact analysis is to check the impact of any code on Unit Test.
Change the Subtract and Multiply Numbers method to as shown below.
For Test Impact Analysis, Click on Test --> Windows --> Test Impact View
Build the solution and the Test Impact view window shows the list of impacted test.Here is shows Multiply and Subtract Numbers method as we have changed earlier.
Right Click Unit Test and select Run All Impacted Tests.
It shows both impacted test is passed.
Again from here you can view the Code Coverage results.
Conclusion:
In this article I have tried to explain that by using Unit Testing, Code Coverage and Test Impact analysis how we can improve the quality of code. Analyzing these things helps during our code review process and also if there is any code change we can do the Test impact analysis.