Visual Studio 2010 Tools - To improve code quality


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

1.gif

Step 2 : Create Unit Test

Right any method and click "Create Unit Tests"

2.gif
 
Window will pop up. Select all methods and set Test Generation settings for file, class and method name. Click OK.

3.gif
 
Enter project name Unit Test project.

4.gif
 
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.

5.gif
 
Step 3 : Test Unit Test

Click on Test Menu and select Run --> All Test in solutions.
 
6.gif

Here DivideNumbersTest fails because this unit test is trying divide by 0.

7.gif 

Change code to handle if 0 value is passed.

8.gif

Again click on Test Menu and select Run --> All Test in solutions. And now all tests are passed.

9.gif 

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)

10.gif

Test settings windows pops up.
  1. Select Data and Diagnostics on left menu
  2. On Right side bottom screen, select Code Coverage and Test Impact
  3. Select Code Coverage and click Configure
11.gif

A new windows pops up. Select assemblies to be included for Code Coverage. Here   select both MathLibrary and MathUnitTest. Click OK.

12.gif

Step 5: Check Code Coverage

Now again run all Unit Tests.

13.gif
 
All result is passed. Right click any test and select Code Coverage Results.

13a.gif

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.

14.gif
 
The source code is divided in to two colors
  1. Code Covered 
  2. Code Not Covered
15.gif

Step 6: Test Impact Analysis 

The purpose of test impact analysis is to check the impact of any code on Unit Test.

16.gif
 
Change the Subtract and Multiply Numbers method to as shown below. 

17.gif

For Test Impact Analysis, Click on Test --> Windows --> Test Impact View

18.gif
 
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.

19.gif
 
Right Click Unit Test and select Run All Impacted Tests.

20.gif
 
It shows both impacted test is passed.

21.gif
 
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.


Similar Articles