Home Jonas Albert Rafael Noam Grasshopper Dekel Yaacov Roei Philippe Tal
 
  

Unit Testing for ASP.Net on mono

July 24th, 2007 by Konstantin Triger

Hello everyone! This is my first post here, so I would like to introduce myself first. My name is Konstantin Triger and I’m a Team Lead for .Net libraries at Mainsoft, including System.Web, System.Data, System, System.Xml etc. The development of all those libraries is hosted and collaborated with the mono project. For example, during the last year we contributed a lot of functionality for the ASP.Net 2.0 stack in the mono project and currently we are working on an implementation of ASP.NET AJAX support in mono (here is the MS version and here is the mono/Grasshopper one).

As part of developing the ASP.Net 2.0 controls for the mono project we faced a serious problem: how to test code, which must run on top of a Web Server without requiring an actual Web Server? We also wanted something that is easy and fast to use, easy to automate and produces an easily analyzable report… In short, we wanted to run it with our nunit framework.

Wait, but for running an aspx page we need a Web Server, a Web Client – how can one talk about plain unit testing? Well, that’s not exactly true. In order to run an ASP.Net application, the System.Web assembly should be plugged in using its hosting interface. This interface provides an API to abstract a Web request to be handled by the System.Web runtime. This is exactly what IIS does using the ISAPIRuntime.

So, if we could host the System.Web runtime inside our test framework, we would not need a Web Server. I would like to introduce the NunitWeb framework which we developed and does exactly that.

How does it work?

Basically, all we need is to create our special HttpWorkerRequest and call HttpRuntime.ProcessRequest (I omit lots initialization stuff for clarity, but it can be seen here). Next, we needed a possibility to hook into page events (like Page_Load), support for postback(s) … all is there!

Here is an example of an NunitWeb based test (taken from here):

[Test]
[Category ("NunitWeb")]  // not required
public void UseSubmitBehavior () {
    WebTest t = new WebTest (PageInvoker.CreateOnLoad (UseSubmitBehavior_Load));
    string html = t.Run ();
    if (html.IndexOf ("onclick") == -1)
        Assert.Fail ("Button Postback script not created fail");
} 

// the following actually runs in Page_Load event!
public static void UseSubmitBehavior_Load (Page p) {
    Button b = new Button ();
    b.UseSubmitBehavior = false;
    p.Controls.Add (b);
}

…and you can see much more examples – search for “[Category (“NunitWeb”)]” – all the NunitWeb based tests are marked this way.

Here is an example of our tests running on mono:

There are 3,245 unit tests in System.Web running automatically on each commit and build. Of those, 334 are NunitWeb based. In the entire runtime, there are 25,774 unit tests running with each and every build.

Resources:

http://anonsvn.mono-project.com/viewcvs/trunk/mcs/class/System.Web/Test/mainsoft/NunitWeb - NunitWeb framework.

http://anonsvn.mono-project.com/viewcvs/trunk/mcs/class/System.Web/Test - System.Web tests.

Share This
Related Posts

Comments are closed.

  
 

Close
E-mail It