Sunday, November 28, 2010

Unit Testing an ASP.NET Website

If you are accustomed to building ASP.NET Web Applications using TDD, you might find yourself perplexed when you sit down to work on a client’s ASP.NET Website project and try to run your first test.

In my case I hit the key command I have mapped to ReSharper’s test runner expecting to promptly see a window showing me a nice red test. After watching an icon spin for a minute I did what every awesome developer does, I blamed Studio, killed devenv.exe and tried it all over again. Same results.

It finally occurred to me that issue was that an ASP.NET Website has no DLL output for NUnit to load and run against. Whatever is a TDD fanatic to do without testing first? After typical developer style complaints about “having to work under these conditions,” I hacked together a page where I could give it a list of types representing each test fixture classes and have it render an HTML page somewhat representing the output I have come to expect from the GUI test runner.

Now I could simply navigate to http://localhost:1234/Website/UnitTests.aspx and run my tests. I recently reworked it to dynamically load the test fixtures from the assembly and thought I would post a sample solution.

Remember not to deploy the NUnit Framework DLL or the App_Code/UnitTests directory to your production site.

I have uploaded the source to my GitHub repository.

2 comments:

  1. This is a VERY basic implementation to introduce TDD into an existing project with very little impact to the existing project and team.

    I MEAN VERY BASIC.

    I just tried to use [ExpectedException(typeof(RequiredColumnsNotPresentException))] and after 5 minutes wondering why my test was passing when it should be red I realized that I didn't implement that functionality. :-P

    ReplyDelete
  2. Your long term goal should be to migrate the project from a Website to a Web Application, and use the proper testing framework tools.

    ReplyDelete

Follow me on App.net