Running integration tests using Cassini and NUnit

Recently, at work, I had the opportunity to do some work on a greenfield ASP.Net MVC application. Part of the development involved creating a suite of integration tests/specs. I had worked on projects where we had written integration tests before but they relied on having an installation of the system, this usually means configuring IIS, around for the tests to run against. This has not always been available on the CI server. A better solution is to create an instance of a web server as part of the TestFixtureSetUp. Here’s how easy it is with Cassini.

Create an abstract class that forms the base class for the integration tests.

using System.Configuration;
using Cassini;
using NUnit.Framework;

namespace TimEscott.Blog.WebApp.Tests.Integration
{
    public abstract class IntegrationTestBase
    {
        private readonly IntegrationTestConfig config;
        private Server webServer;

        protected IntegrationTestBase()
        {
            config = (IntegrationTestConfig) ConfigurationManager.GetSection("IntegrationTest");

            if(config == null)
            {
                throw new IntegrationTestConfigurationException("Could not load configuration information to run the integration tests.");
            }
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            webServer = new Server(config.Port, config.VirtualPath, config.PhysialPath);
            webServer.Start();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            if(webServer != null)
            {
                webServer.Stop();
            }
        }

        protected string BuildTestUrl(string path)
        {
            return string.Format("http://localhost:{0}/{1}", config.Port, path);
        }
    }
}

So now I can create an integration test, using WatiN:

using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using WatiN.Core;

namespace TimEscott.Blog.WebApp.Tests.Integration.Home
{
    [TestFixture]
    public class When_loading_the_home_page : IntegrationTestBase
    {
        [Test]
        public void Should_contain_the_correct_title()
        {
            using (var ie = new IE())
            {
                ie.GoTo(BuildTestUrl("Home"));

                Assert.That(ie.ContainsText("Welcome to ASP.NET MVC!"), Is.True);
            }
        }

    }
}

Here is the config class and the app.config for completeness:

using System.Configuration;

namespace TimEscott.Blog.WebApp.Tests.Integration
{
    public class IntegrationTestConfig : ConfigurationSection
    {
        [ConfigurationProperty("Port", IsRequired = true)]
        public int Port
        {
            get { return int.Parse(this["Port"].ToString()); }
        }

        [ConfigurationProperty("VirtualPath", IsRequired = true)]
        public string VirtualPath
        {
            get { return this["VirtualPath"].ToString(); }
        }

        [ConfigurationProperty("PhysialPath", IsRequired = true)]
        public string PhysialPath
        {
            get { return this["PhysialPath"].ToString(); }
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="IntegrationTests"
             type="TimEscott.Blog.WebApp.Tests.Integration.IntegrationTestConfig, TimEscott.Blog.WebApp.Tests.Integration"/>
  </configSections>
  <IntegrationTests Port="4015"
                   VirtualPath="/"
                   PhysialPath="C:\Projects\Blog\TimEscott.Blog.WebApp\" />
</configuration>

This in conjunction with something like Ayende’s SqlCEDbHelper as a mechanism to help create a test database, gives a very portable test suite which can run anywhere that .Net and Cassini are installed.

Scott Hanselman describes one way to remove the pre-requisite we have here (having the Cassini dll installed in the GAC). But for the sake of having one dll installed on an internal server I am currently not too worried about having this dependency.

A nice addition might be to actually check that the port is free, if not find one that is?!

Advertisements

~ by Tim on 9 December 2008.

6 Responses to “Running integration tests using Cassini and NUnit”

  1. Interesting stuff, looks familiar though 😉

  2. Hi Ben,

    That must be because I have shown you this stuff before! 🙂

  3. Knew it!

  4. excellent work

  5. […] https://timescott.wordpress.com/2008/12/09/running-integration-tests-using-cassini/ […]

  6. Here is a branch of cassini specifically designed for testing scenarios.

    http://cassinidev.codeplex.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

 
%d bloggers like this: