Friday, April 10, 2009

Unit Testing, Code Analysis and Code Metrics in VS.NET 2008


In .NET we prefer to use VS .NET to do unit testing.Both VS 2005 and VS 2008 Team Editions support unit testing.
Way to do :

  1. Add a unit test project to your solution.
    Navigate to the method that needs to be tested. Do a right click : Create Unit Test on the method.



  • In the Wizard provided:Click on Setting. As of now MS does not support prepending Namespace before the class. This is of great value to us as need the namespace to distinguish similar class names in multiple namespaces. So we manual prepend the namespace i.e. 'Manager_' in the current case.



  • A dummy test case gets created:Modify the expected value to match whats expected. We can add more 'Asserts' to improve the test case.




  • To execute the test case, do a right click on the test case and select 'Run Tests':




  • The test will be executed and the run status displayer. We are re-run the test case or we can debug it with the appropriate buttons selected on the top:




  • To view the code coverage results, right click on the test case and select 'Code Coverage Results'.




  • The result will contain all the project which got invoked to make the test run:




  • Details of the method tested:






  • Similarly we can do a Code Analysis based on Coding Standards and generate Metrics

    Coding Standards:




    Coding Metrics:




    • No comments: