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.
No comments:
Post a Comment