Translate

How to run test cases | Process of running test cases

After creating the test application, it is time to execute. There are few ways of doing it but the most common way is, choose Test => Run => All Tests just like shown in Figure 1. Another way is, right click on any code page or test method and select Run Tests just like shown in Figure 2.


Figure 1. Running all test cases using Test menu option



Figure 2. Running test cases using context menu of code file


Either option runs the test methods (annotated with [TestMethod]). Also, the Test Explorer window opens just like shown in Figure 3, displaying status and results of test methods:


Figure 3. Test Explorer window showing successful tests

In Figure 3 above, the status of the test cases is “Passed” designated by white check inside the green circle. When any test gets fail (ex: by changing message expected in ViewBag in the Index() test method), it is labeled with white cross (X) inside red circle just like shown in Figure 4. A test may fail due to any reasons:

• Return values of the method is incorrect (per our Assert statements).

• The output parameters are incorrect.

Any unexpected exception occurred

Any expected result did not occur

Any bug exists in test code



Figure 4. Test Explorer window showing failed test

To see example fail test in Index() test method of the HomeControllerTest class, change
expected string which represents the value in the ViewBag.Message property and then compile the application and run tests again.

Clicking on Run link under Test Explorer window gives more granular options to run the tests just like shown in Figure 5 below:


Figure 5. Options to run tests with the Run link

How to create Test Project | Test Project Creation

The steps to create test project are given below:

1. Open solution in Visual Studio (if it is not already open).


2. Right click on solution name from Solution Explorer and choose Add => New Project same as shown in Figure 1 (In other way, we can select solution name and choose File => New Project).


Figure 1. Adding a new project to an existing solution

3. From Add New Project dialog box, select Test from left pane and select Unit Test Project
from central pane just like shown in Figure 2:


Figure 2. Creating a Unit Test Project using Microsoft Test Framework

4. Now provide project name, same name as the main project name and add suffix .Tests.
(Addition of suffix is convention and not a requirement. The purpose of adding the suffix is to show clearly the project by just looking at the name.) and then click “OK”

5. Once project is added then we will have a project with a reference to Microsoft Test
Framework with a single class as shown in Figure 3 below:


Figure 3. Default Class Created by New Test Project

6. To make the code accessible to test project, we need to add a reference in test project of the existing project. Right click on References node under test project from Solution Explorer and choose Add Reference same as shown in Figure 4 below:


Figure 4. Adding reference to test project
  
7. From Reference Manager window, select Solution from left pane and from center pane check the box corresponding to project we want to test same as shown in Figure 5 below:


Figure 5. Adding main project as reference to the test project


The preceding process is not difficult and strongly encourage us to create our test project at same time when we create MVC application. Simply check “Create a unit test project” check box when choosing the project options. Visual Studio will create Unit Test project with methods automatically to test the controller action methods.

In addition, if we use Empty project template or Test Project template in Visual Studio then we will not have any controllers, nor corresponding test classes. Only if we use a more functional template (like Internet Application or Intranet Application) then we get the controllers and corresponding test classes.

Please note that if we add a controller to MVC project then it does not add test class in test project automatically. Similarly, if we add action method in controller class then it does not add test method in test class automatically.