Oftentimes, when developing or integration-testing a piece of software that is web-based (for example a web site or a web service component such as an Azure Mobile Service), being able to start and stop a web server programmatically on an as-needed basis comes in very handy.

This is where IIS Express comes into play. IIS Express basically is a lightweight, self-contained version of his big sibling, the fully fledged Internet Information Server. Because it doesn’t run as a service and doesn’t require administrator rights, it is ideally suited for developing and testing web sites and services locally – just like Visual Studio starts an instance of IIS Express when you hit F5 on a web application/service.

This post presents a helper class to programmatically interact with the IIS Express web server.

The IisExpress class essentially, is a wrapper around the IIS Express command line (you can read more about the command line interface here). It’s public interface looks like this:

The configuration file

One really nice thing about IIS Express is that it enables you to work with the full web-server feature set – including SSL, URL Rewrite, Media Support, and the IIS extensibility model. (A configuration reference can be found here).

The default IIS/IIS Express configuration file is located in the %userprofile%\Documents\IISExpress\config folder or %userprofile%\My Documents\IISExpress\config folder, depending on your OS. – This is the configuration that the IisExpress class uses as default when no other config file was explicitly specified. However, in most cases you will use an extra file for your purposes that you will commit to your source repository along with the rest of your project, containing the specific configuration settings for your scenario. (Tip: You can start with a copy of your original applicationhost.config.).

Using the class with xUnit.Net

In this section, I will shortly demonstrate how the IisExpress helper could be used for integration testing an ASP.NET MVC application. I will use xUnit.Net here, but the choice of the unit testing framework is largely a matter of personal taste. Essentially, the story remains the same for NUnit or Visual Studio Unit Testing Framework (MSTest) or whatever else…

First of all, we need a mechanism to start and stop IIS Express. This would be done with TestFixtureSetUp/TestFixtureTearDown methods in test frameworks such as NUnit, and with ClassInitialize/ClassCleanup methods in MSTest. When using xUnit.Net, you will have a separate wrapper class for the IisExpress helper (called a ‘fixture’ in xUnit.Net-speak), and implement the IDisposable interface to automatically have IIS Express shutting down when the fixture goes out of scope:

You would then ‘inject’ that fixture in your test class via the IUseFixture<T> interface and initialize the IisExpress helper in your class that contains the actual tests:

As you can see, the IsRunning property is checked before initialization; this is because xUnit.Net creates a new test class instance for each single test method (which is the biggest conceptual difference to other test frameworks).
On initialization of the IisExpress class then, the path to the config file is set and then the Start() method is called with the name of the web application which should be hosted (this is the name of the web application from the config file).


The stand-alone IisExpress helper class is available as a Gist here, the entire sample project with xUnit.Net and ASP.NET MVC can be found here.

Share on Pinterest
There are no images.
Share with your friends

Testing, Test Automation & TDD