I have an existing asp.net webforms application that I would like to add some unit testing to but am unsure of exactly how to go about it.
The application is databas
For database related unit testing we use a seperate and stable test database to execute unit tests against.
We also wrap data modification tests in a TransactionScope
that is not commited, therefor rolling back the data changes for subsequent test runs. We've found this works really well.
In terms of tooling, I too am in search of some way to test ASP.NET webforms apps. For the model portions (data access, etc) I use NUnit, a local sql express db, and wrote code to create the schema (from a .sql file we check in) before all of the tests run.
Between tests, I use the NUnit DataRollback extension to rollback any db changes after each run.
I still don't know of a good way to test the logic in the pages though, akin to Rails' functional tests. From a coverage perspective, it's devastating. You can push a lot of logic back into your models, but invariably there is code in your code behind files that is important, can break, and you'd like to get unit tested.
I would most likely test the backing methods that your web forms call (bypassing the forms validation), this will let you test the logic and data access.
As for the test data, a seperate test database would be the best option. Otherwise i suggest having the test methods remove the test data once its completed.
The first thing you need to decide is: What is your motivation for adding unit tests?
There are many excellent reasons for having unit tests (I rigorously practice TDD myself), but knowing which one is the main driving force in your case should help you decide which tests to first write.
In most cases you should concentrate on writing unit tests for the areas of your application that have been causing you the most pain in the past.
Much experience has shown that when software originally was written without unit tests, it can be hard to subsequently retrofit unit tests. Working Effectively with Legacy Code provides valuable guidance on how to make an untested software projects testable.