juin 01

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 :

[AspNetDevelopmentServerHost(@"c:\temp\WebSite\", "/")]
public void MyTestAccessingHttpContext()

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>
public static void ClassInitialize(TestContext testContext)
   //Set an env. variable that can be used with AspNetDevelopmentServerHostAttribute
   Environment.SetEnvironmentVariable("DeployDirectory", testContext.DeploymentDirectory);

[AspNetDevelopmentServerHost(@"%DeployDirectory%\WebSite\", "/")]
public void MyTestAccessingHttpContext()

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.

Tags: | |


dubstep maker

Posted on mardi, 1 octobre 2013 00:54

I think this is among the most vital information for me. And i am satisfied studying your article. However should statement on few common issues, The web site taste is perfect, the articles is in reality excellent : D. Just right process, cheers

Also visit my blog -  dubstep maker - http://mrso-mon.ru/node/120924

remix beats

Posted on mardi, 8 octobre 2013 14:30

Hi, of course this piece of writing is actually good and I have learned lot of things from it regarding blogging. thanks.

My weblog:  remix beats - http://www.newdubstepsongs.webrok.com/

a lot more

Posted on mercredi, 27 novembre 2013 18:19

What's up, of course this article is genuinely fastidious and I have learned lot of things from it regarding blogging. thanks.

Have a look at my web blog ...  a lot more - http://www.tolaire.com/article.php?id=7127

seo articles written

Posted on jeudi, 19 décembre 2013 15:44

What's up, all the time i used to check web site posts here in the early hours in the morning, as i love to find out more and more.

My web page:  seo articles written - http://keystaffsourceblog.com/

crazy science articles

Posted on jeudi, 19 décembre 2013 15:46

Just wish to say your article is as amazing. The clarity in your post is simply spectacular and i could assume you are an expert on this subject. Fine with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the rewarding work.

Also visit my web blog ::  crazy science articles - http://strataeng.com/

app promotion service

Posted on jeudi, 19 décembre 2013 15:52

Hi, Neat post. There is a problem together with your web site in web explorer, would check this? IE still is the market chief and a good part of other people will omit your fantastic writing because of this problem.

Review my web site -  app promotion service - http://xroadsventures.com/

Ajouter un commentaire

  • Commentaire
  • Aperçu immédiat