Quantcast
Channel: Pierre-Emmanuel Dautreppe - TDD
Viewing all articles
Browse latest Browse all 16

How to setup an ASP.NET unit test ?

$
0
0

In some cases, when you are writing a unit test, you might need to have a valid HttpContext. Fortunately, you have the ability to do that with the Visual Studio's unit tests.

Let's use a static web site

So to do that, you can simply use a local existing web site. And can define your unit test as following :

[TestMethod]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:7778/Default.aspx")]
[AspNetDevelopmentServerHost(@"c:\temp\WebSite\", "/")]
public void MyTestAccessingHttpContext()
{
   Assert.IsNotNull(HttpContext.Current);
}

So what does that mean ?

  • HostType specifies that you are writing an ASP.NET Unit test
  • UrlToTest defined the URL of your web site
  • AspNetDevelopmentServerHost is optional. It's required only if the website you want to use is not hosted in IIS. In that case, you will just specify a folder that is located on your computer and that will be mounted as a WebSite using the Visual Studio Web Server (Cassini). The port specified in the UrlToTest will be used to mount your web site.

How to use a "dynamic" web site ?

That's pretty cool, but the main drawback is that your website must exist on your disk. So when you have this kind of test in your team, all the team members' computer must be setup the same way to be sure to find the local website. And of course, this is the same thing for you conitnuous integration server.

To avoid this, you could have a website located in your solution (and so with a path that will change from environment to environment) and refer to this website. You could also have a folder defined in your test project and use it as a local web site. That's this second solution I will explain here (the first one is just similar, but even simpler).

First step : create your website

Here the usual way I create my minimal test web site.

My website just contain a empty Default.aspx page. The readme file is just some documentation to explain why I use this website.

I use two parent folders : 

  • The WebSite folder is the one that I will be using as a reference in my test. In my scenario, this must be unique (meaning that if you have several test projects, and some of them are using the same technique, just choose a unique name for each web site folder).
  • The __Deployment__ folder is the folder where I include all my deployable artifacts that I need for my tests. This will be more explained in the Second Step.

Second step : deploy your website

For now my website just exist in my solution, but I want it to be deployed for each test run. To do that, I need to update the testsettings of my solution.

In a basic scenario, we can just update the Local.testsettings file that is available under Solution Items. Of course if you have multiple "testsettings files", you may need to update all of them.

So what are we doing here ? We are asking MsTest to deploy, each time we are doing a test run, to deploy our "__Deployment__" folder. As a consequence, this folder will be copied to our 

Last step : use it in your test

So now when we run our test, we can have access to this website as it will be available in our current folder. So how can we transform our test code ?

/// <summary>
/// Initialization of the test class
/// </summary>
/// <param name="testContext">The test execution context</param>
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
   //Set an env. variable that can be used with AspNetDevelopmentServerHostAttribute
   Environment.SetEnvironmentVariable("DeployDirectory", testContext.DeploymentDirectory);
}

[TestMethod]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:7778/Default.aspx")]
[AspNetDevelopmentServerHost(@"%DeployDirectory%\WebSite\", "/")]
public void MyTestAccessingHttpContext()
{
   Assert.IsNotNull(HttpContext.Current);
}

So in the initialization of our test (here I have chosen the ClassInitialize to run it only once before the first test of the class), we can create an environment variable that can be used in our aspNetDevelopmentServerHost.

With such a configuration, our test will work perfecly no matter which client computer or build server.


Viewing all articles
Browse latest Browse all 16

Latest Images

Trending Articles





Latest Images