Unit Testing WebMethods

Any Asp.net developer working with Asp.Net Ajax cannot deny the importance of “Asp.net Ajax WebService” and with WCF coming up this feature is going to be one of the most admired feature of the asp.net web development.

Most of the current application follows layered architecture. Wherein the CRUD functionality lies in the Data Access Layer(DAL) .These methods are in turn invoked through the Business Layer functions which in turn are invoked through the web methods defined in our web services layer.

Unit testing, at least this is one feature whose importance cannot be denied. Unit testing DAL  or Business layer methods are fairly simple but testing web methods are not that straight forward. Don’t worry I am not saying it’s going to be a very challenging task , in order to unit test a web method all you have to do is to add a “Default.aspx” web page into your solutions root directory.

Let’s say you have a web method with the following definition:-

Step 1 : Defining WebMethod

public string HelloWorld()
{
return "Hello World";
}

Step 2: Adding the Default.aspx Page

To your solution(website) just add a new webpage with name "Default.aspx".Note if you have multiple sub directories under the root folder then you have to place this webpage outside any such sub directories i.e. directly under the application directory.

Step 3: Adding a new Test Project to your solution

Select the solution --> File --> Add New Project --> Select TestProject --> Give Some Name(say MyTest) and click Okey.

Step 4: Writing the Unit Test

Writing unit test is fairly simple.Right Click on the TestProject(named MyTest) –> Add –>  Select UnitTest.
Once you have made the selection a modal window(popup window) will come up.This in turn will list down all the projects which are under the current solution.

Once you have the window opened , select your application "WebSite" and expand it.Once expanded it will have two options namely "No Namespace" and "_Asp".Well I guess these categories can differ.

Now you will have to look for your web method(HelloWorld) inside the "No Namespace" category.Once found just check the corresponding checkbox and click okey.And here you have your unit test method for the web method (HelloWorld).

This is how the unit test method will look like in case of above mentioned web method.

/// 
///A test for HelloWorld
///
// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
// whether you are testing a page, web service, or a WCF service.
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\My Application Data\\My Work\\VP\\UI", "/UI")]
[UrlToTest("http://localhost/UI")]
public void HelloWorldTest()
{
Users_Accessor target = new Users_Accessor(); // TODO: Initialize to an appropriate value
string expected = string.Empty; // TODO: Initialize to an appropriate value
string actual;
actual = target.HelloWorld();
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}

The above code is specific to my application development environment.And yes I have used the VS 2008 testing framework for writing the above test case for web method.In order to run the test , simply right click on method and select either run the test or debug the test.

In the above code , you will have to set the expected value and then unit test will evaluate it will the actual returned value.

This article is more verbose but while implementing most of the tasks will be automated, thanks to Visual Studio IDE.

Contact Me

rk.pawan@gmail.com | +1-737-202-7676 | LinkedIn | Facebook | Github |

4 Comments

Comments have been disabled for this content.