How do I unit test a web forms site? It seems to me that as so much of it depends on state and user input it wouldn't be feasible.

If it's not feasible is there a valid automated alternative?

  • 3
    If you want automated user interface testing, then you will need something like Selenium. But, if you want automated unit testing, then you don't need Selenium. You just need the right patterns in place. See my answer. Oct 25, 2011 at 14:51

8 Answers 8


Yes, you can. You just have to be careful to separate your concerns well. In short, you have to remove all your logic from the code-behind and put it into other classes.

There are two common ways to do this.

The simple way is to rethink all of your event handlers in terms of "What information does the system give me? What information do I need to populate on the page?" and then provide a service class which does that conversion.

In this case, the service layer should know very little about the nature of your presentation layer. You still have to take the data returned from the service and populate the correct components of the WebForm in your code-behind and this remains untested (at least by unit tests, you can still employ integration tests). But this is rarely where code goes wrong, it is much more likely to fail in the logic.

A more complicated, but more effective, way is to use the Model View Presenter pattern. When we tried that, we found that the Presenters quickly became very coupled to the framework and, the more we developed MVP, the more clear it was that MVP really wanted to be MVC but couldn't be.

That said, others have done this very successfully - there is even a webformsmvp framework available to remove the heavy lifting - so your mileage may vary.

  • 1
    @jmoreno: Nice catch! I have forgotten WebForms and MVP even exist over the last 5 years.
    – pdr
    Dec 14, 2016 at 18:03

Quite obviously, an entire web forms page is not a unit, and thus cannot be unit tested. However, there are some things you can do for automated tests:

  • unit test individual components of the page (any methods that perform actual logic)
  • unit test components used by the page (custom controls, underlying business logic, etc.)
  • automated functionality testing (by remote-controlling a browser, or sending HTTP requests through something like cURL)
  • automated penetration testing (there are tools that will try to find possible injection points, and automatically run harmless yet detectable injection attacks on your page)
  • load testing
  • checking the layout against a list of usability and house-style bullet points (although this is very hard to automate; it is usually more feasible to just do it manually every now and then)
  • -1 Sorry but Load testing, penetration testing and layout checking are nothing to do with unit testing Oct 25, 2011 at 10:35
  • 2
    @TomSquires: of course they are not, and if you read my answer carefully, you will notice that I never said they were. But the question seems to confuse the concepts of unit tests and automated testing in general, which is why I listed various meaningful automated tests you can throw at a web form.
    – tdammers
    Oct 25, 2011 at 10:50
  • agree, testing a web page is more likely to be some form of integration testing then a unit test by strict definitions, though its still good to automate whatever you can
    – jk.
    Oct 25, 2011 at 11:48
  • 6
    @TomSquires: totally unjustified downvote. Your own question starts with testing an entire site, which has nothing to do with unit testing. Tdammers identified various ways to test the whole and the parts and identified where unit testing fits in. +1
    – azheglov
    Oct 25, 2011 at 14:43
  • 3
    Fair enough. I take it back Oct 25, 2011 at 15:46

I am sorry for missing the "unit" part of the question...

SeleniumHQ is your friend for tests from the front end. It is not a unit test, more like a black box testing. You still need to think of valid test cases...

  • 1
    +1 Your answer came up as I was pressing "post your answer" to say exactly the same.
    – yannis
    Oct 25, 2011 at 9:43
  • 2
    Also, since op is asking about unit testing, let's just add that what the question is actually on is functional(ity) testing, which is more appropriate (sensible, feasible) when testing user input elements.
    – yannis
    Oct 25, 2011 at 9:45
  • +1 cos I keep meaning to use it, but never got round to it. Has anyone used it, is it good and worth implementing ? Oct 25, 2011 at 11:42
  • 6
    Selenium is a fantastic tool but it has nothing at all to do with unit testing.
    – pdr
    Oct 25, 2011 at 12:02
  • True, seems I skipped this information in the question :/ Oct 25, 2011 at 15:38

Speaking from experience: Only if it's done right. By "right" I mean minimal code-behind and something like the above-mentioned Model-View-Presenter to make the Web Form "dumb". This usually proves to be very difficult with brownfield applications because they weren't designed with this in mind and it's a near-Herculean effort to refactor/rewrite pages to use it.

  • I disagree. You can still isolate the units and write some tests. Leaving the code to rot and having it as a failure point is not good IMO. I'm not saying you'll be able to unit test everything, but you still can refactor and isolate some units of code, which is better than nothing.
    – IamDOM
    May 27, 2020 at 7:43


I find unit web tests are extremely useful, even if it's just to give a general idea of a regression bug or for new projects.

As far as state is concerned, you create your unit tests as you would with non-UI tests - they clear the database out at the start of the test and rebuild the database to contain nothing except the start state. Each unit test then encapsulates a single page, or usually a distinct task on a page.

http://watin.org/ is another web test tool but for C#/.NET. You write the tests as unit tests:

public void SearchForWatiNOnGoogle()
  using (var browser = new IE("http://www.google.com"))


It's currently IE based but has some experimental support for Firefox and Chrome. You can pretty much automate anything you would do in manual tests, including Javascript interaction.

  • Ack that it's 2016. Today's visit to WatiN site indicates to me that WatiN is perhaps dead. The docs are sparse, not up-to-date, and videos do not play. I am not complaining - I have huge gratitude for open source voluntary tools - this comment is to help other come-lately persons like me. The answer is fine, just pointing out that getting started might required self-driven hands-on learning.
    – qxotk
    May 27, 2016 at 15:18
  • 1
    Further help for anyone thinking of the WatiN way, confirmation it's stagnant + where to look in present day here: stackoverflow.com/questions/118531/…
    – qxotk
    May 27, 2016 at 15:23

You can't really unit test a web site, simply because web requests happen on a wire (or through a TCP stack). Thus, the tests do not fit the definition of "unit test", they would be, likely, end-to-end tests.

For those kinds of tests, you can use a suite like Selenium which runs a web browser behind the scenes. A word of warning though: usually this kind of testing is very difficult and unpredictable, as there are many moving parts!

More interestingly though, it worries me a bit why you would need to test web forms at all. Aren't you putting way too much logic in the code behind, and have an anemic business logic by any chance?



Over the past 5 years Jasmine has emerged as a key tool for front-end unit tests. It is often incorporated into automatic build testing with Node and npm

Per https://en.wikipedia.org/wiki/Jasmine_(JavaScript_testing_framework):

Jasmine is an open source testing framework for JavaScript.[2] It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.[3]

Despite all the mentions of javascript it can also be used for unit testing of a plain web form.

  • While javascript is used in webforms its not the primary method of updating the ui Dec 15, 2016 at 9:53
  • True. However the alternative is... ? So that's why, even though not intended for the purpose, Jasmine works very well. Plus the syntax is almost identical to RSpec which Ruby programmers love. Dec 20, 2016 at 1:36


When developing an ASP.NET site we were able to run unit tests on:

  • The domain
  • BL controllers/presenters
  • the codebehind of Web Forms pages

It is possible to TDD all of this, depending on your architecture. The only thing you cannot unit test is the layout of the markup file.

  • 4
    This answer says "yes it's possible" to the question of "how". Not exactly helpful.
    – RubberDuck
    Dec 14, 2016 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.