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.

Testing Frameworks

There are many testing frameworks available (both commercial and free). The most
commonly used frameworks are xUnit family of frameworks, most notably NUnit, xUnit.net, and MbUnit. Nunit is most widely used as it has been available longer in market and because of its extensibility (it supports plug-ins). We can easily download any of these frameworks from NuGet (http://nuget.org). Visual Studio allows us to use any of these frameworks within the IDE. So, that we have a familiar interface to work with.

In addition to basic testing frameworks, there are also mocking frameworks, purpose of which is to provide mock objects. Mock objects simulate the functionality of real objects. With mock objects w can simulate nonexistent functionality and external resources. It provides means to write unit test following the principle of isolation.

There are many mocking frameworks available free such as Moq, Nunit.Mocks and RhinoMocks. We can download any directly from NuGet. Some of the commercial ones are JustMock from Telerik (http://bit.ly/JustMock) and Isolator from Typemock (http://bit.ly/TypemockIsolator). Microsoft Test Framework has a feature called Fakes which is equivalent of the mock object.

We generally use Microsoft Test Framework as it is included in Visual Studio and does not require any setup or configuration. But not all the features of Microsoft Test Framework are
available in all versions of Visual Studio. For example, Fakes feature to create mocking objects is available in Visual Studio 2012 Premium and Ultimate. That's why, for mocking objects, we use a mocking framework called Moq.

Moq Introduction

Moq is a mocking framework which very simple and easy to use. It has no dependency and we just need Moq.dll. It is open source and free too. We can use it in all editions of Visual Studio including the free ones.

Moq was designed to take advantage of C# 3.0 but it works very well with the most recent version of C# too. It supports mocking interfaces and classes both. Internally, Moq uses Castle DynamicProxy (http://bit.ly/DynamicProxy) as a mechanism to enable mocking. Which means DynamicProxy helps Moq to create .NET proxy on-the-fly at run time. Using this proxy Moq is able to intercept objects without modifying code of class. Once Moq intercepts the object, it can determine its behavior and it can add functionality to the object without modifying code of the object.

By using proxy mechanism mock objects is simple and fast but it has a some limitation: If both interfaces and classes are proxied then only virtual members can be intercepted and mocked. In other words, if we have static classes or members then they can not be mocked with Moq.

Setting Up Moq


Easy way to set up Moq in our application is to install it via NuGet. To open NuGet, go to Tools => Library Package Manager => Manage NuGet Packages for Solution as shown in Figure 1:


Figure 1. Opening the NuGet Package Manager


Once NuGet Package Manager window is opened, type moq in the search box and results will appear as shown in Figure 2:



Figure 2. Installing Moq from NuGet

Click on “Install” button for Moq. In the Select Projects dialog that opens, check the box for the Tests project as shown in Figure 3 and click “OK” and it will set up Moq in our test project and we now ready to use it.



Figure 11-3. Selecting the project in which to install Moq


Benefits of Unit Testing

Following are the main benefits of using unit testing:

Finds problems early: Well written unit tests enables you to easily and quickly pinpoint
problems of the code early in development process. It is particularly true when
implementing test driven development (TDD). With TDD we first write test cases and then write code to pass the tests.

Facilitates change: Unit test helps to ensure that any future changes in the code do not break application. If any change cause a test to fail then the test failure will give us a clear pointer where we need to fix the code.

Simplifies integration: As unit test focuses on testing small pieces of code and for testing
complete module becomes easier - we know that at least the components of the module work properly.

Provides documentation: Unit test provides nice source of documentation about functionality of the application. So, naming the classes and unit tests correctly becomes increasingly important.