Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is is possible (and sensible) to 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 fesable.

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

share|improve this question
    
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. –  Matthew Rodatus Oct 25 '11 at 14:51
add comment

7 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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)
share|improve this answer
    
-1 Sorry but Load testing, penetration testing and layout checking are nothing to do with unit testing –  Tom Squires Oct 25 '11 at 10:35
    
@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 '11 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 '11 at 11:48
3  
@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 '11 at 14:43
    
Fair enough. I take it back –  Tom Squires Oct 25 '11 at 15:46
add comment

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...

share|improve this answer
    
+1 Your answer came up as I was pressing "post your answer" to say exactly the same. –  Yannis Rizos Oct 25 '11 at 9:43
1  
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 Rizos Oct 25 '11 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 ? –  NimChimpsky Oct 25 '11 at 11:42
4  
Selenium is a fantastic tool but it has nothing at all to do with unit testing. –  pdr Oct 25 '11 at 12:02
    
True, seems I skipped this information in the question :/ –  WarrenFaith Oct 25 '11 at 15:38
add comment

Yes, if you have the right design patterns in place.

Take a look at Dan Crevier's blog post series entitled "A pattern for unit testable Asp.net pages." This pattern was (and is perhaps still) used in Windows Live SkyDrive (which is referred to in the blog posts by its beta name "Live Folders").

The full example source code is available and linked to in part four.

PageModelPattern.zip

share|improve this answer
add comment

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.

share|improve this answer
add comment

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:

[Test] 
public void SearchForWatiNOnGoogle()
{
  using (var browser = new IE("http://www.google.com"))
  {
    browser.TextField(Find.ByName("q")).TypeText("WatiN");
    browser.Button(Find.ByName("btnG")).Click();

    Assert.IsTrue(browser.ContainsText("WatiN"));
  }
}

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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