Testing is fundamental to get your code into shape and to be sure that what you are doing is behaving as expected. New methodologies like Test Driven Design (TDD) point to that goal. We (developers) are lazy and very self confident, we normally write the code and run it a few times without paying a lot of attention of the requirements, if the code works as we think it should we are happy and move to code something else (or write some blog post :-) ).
So the idea behind TDD is that instead of write your code first and test it later, you start by writing your test (hopefully something that tries to follows the requirements, if not there is no much good on the practice either), them you run your test you see it fail and run the code that will make the test pass.
This post is not about TDD, there are tons of articles out there on how to do it. This post is about how to do TDD on web development (even if you don’t use web controls).
You can write test in different ways, but the most effective one is creating the test in a way that they can be run automatically, and that the test suite can evolving with your code and incorporating more and more test while your code evolve. For that reason in the .Net world there are some frameworks that allow you to write Unit test and run them the console, inside a GUI or as part of your automatic build (within or without a CC process).
Nunit is my framework of choice (for no particular reason besides that I have been using it for a while), please try others as well, there have been a lot of talk about MbUnit. Nunit works very well for me to test libraries or console apps, what is great because we all know that most of your code should reside on libraries, but the reality is that I am a web developer and you like it or not you need to put code in your code behind classes (even if is just to glue everything together) and for testing this code you will need an extension to Nunit called NunitAsp.
Follow the instructions from the NunitAsp site to download, install and set up your testing project, you will need to install Nunit as well if you haven’t done so.
So let’s see the differences in the code. Let’s say that you have a page with this html markup…
…and this code behind:
So what do you do? The solution is simple, you create a TextBoxTester that will represent your hidden input field (I guess that TextBox works with any input field), change the value of the field modifying the Text property of the object and them use the static Submit method on the CurrentWebForm object.
And that’s all is to it, now you can start testing your web control free asp.net pages.