Unit testing with VSTS


HTML clipboard

Introduction

Unit testing is validation and verification methodology where the developers test the individual units of source code. Some key points to remember are -
A unit is the smallest part in the application which can be tested. So it can be a method, function or class.

These tests are conducted during development.

Unit testing belongs to the white box testing category.

Objective

The objective of unit testing is to test not only the functionality of the code, but also to verify that the code is structurally sound and robust, and able to respond appropriately in all conditions.

The main objectives of Unit Testing could be:

  • Code review.

  • Coding standard violation.

  • Code coverage.


Code review:

For every day changing code, unit tests allow developers to perform quick checks against individual or collections of methods to ensure that functionality continues to perform as expected, especially after adding more code to the project. As the code is changing drastically, we need one level of review from experts whether the code is potentially acceptable and stable. Example: infinite loop, deadlocks, time expiration, Dispose() method not called for some bigger objects, check the loop statements for performance and scalability, calling GC for garbage collection when required, Depth of Inheritance, Cyclomatic Complexity.

Coding standard violation:

Static analysis features in VSTS allow you to perform a code review against a set of rules that can be customized and extended. With Visual Studio Team System you have fine control over the code analysis rules that apply to class library projects, web site projects, and source code control. With this configurability you can enforce coding standards to improve the quality of your code, while ensuring inapplicable rules never get in your way. There are lots of rules available in code analysis tool of VSTS. But if they are not sufficient we can create our own rules to check for coding standards. We can also create report of failures for each class same like code coverage report which is attached in next section.

Code Coverage:

Code Coverage ensures a good coverage of the use case based test cases. It also helps to identify any unused paths and redundant or dead code.

After running unit tests against your code, you may want to determine how complete your unit tests are and what code has not been tested by the current collection of tests. VSTS Code Coverage provides both a visual tool as well as tabular metrics on the effectiveness of your unit tests.
 
Finding Code coverage is very essential when code is changing daily or weekly basis. There are certain ways to find the code coverage using VSTS. After running unit tests we can get code coverage results in Code Coverage Result pane. We can create report after changes in class and compare it with previous results.

The problem with VSTS 2008 is we are not able to find the code coverage of assemblies situated in GAC. Your options are to not have the dll in the GAC.

Resolution: VSTS 2010 does not have that issue and we can find code coverage of any managed type of dll through code coverage using VSTS 2010, dll can be anywhere including GAC. But keep in mind that code coverage doesn't run in debug mode. Also we need to check the dlls for code coverage from test config file settings.

Procedure for code coverage using VSTS

  • Load the product and test sources in a single solution in Visual Studio Team System.
  • Perform a full build. 
  • Open the Run Configuration you intend to use for this run ('localtestrun.tesrunconfig' is the default) 
  • Select the Code coverage page in the resulting Dialog 
  • Check the Binaries you wish to be instrumented 
  • Execute the tests you desire to run, either through Test Manager or Test View.

When tests are finished, open the Code Coverage window and review the results.

Automate process to test functions that has database entries by creating insert and delete scripts for functions.


Similar Articles