Wednesday 25 May 2016

Writing Automated Unit Test Cases and Code Coverage Using Dot Net

To add a new test project 

 1. Right click on the Solution

 2. Add new project ---> Test(Left Side) ---> Unit Test Project ---> Give a name ---> OK

 3. Add the main project dll to the test project
================================================================================================================

Instructions:
==============

1. Use static and private keywords only when it is required. Avoid unnecessary using of these keywords.

2. Which ever the methods are returning void, make them return bool.

3. Make all classes and methods public. Static methods cannot be handled in test cases.

4. Make all the classes and methods public in the test project.

5. The [TestMethod]s should return void. It should not return anything.

6. Try to call all the classes with methods from the main class.
   Test code should be so designed that it should initiate the application from main from
   where it can cover all the classes. This way it will hit most of the lines which will help in code coverage.

7. All the method arguments in the code should be tested.
   Example:
     public bool AddStructureItemsToAnArray(string databaseType, int index, string carModelName, UInt32 CompHash)
        {
            if ((string.IsNullOrEmpty(databaseType)) || (0 > index) || (string.IsNullOrEmpty(carModelName)))
            {
                CDatalogger.SuperTrace("ERROR: Invalid input parametes received");
                return false;
            }
         Logic here
         Logic here
         Logic here
         Logic here
        }

8. Every code should be put in try-catch block and rest things should be captured in finally block.

9. Each error message should be captured in log file through logger class.




No comments:

Post a Comment