2.3. Unit Testing

Unit testing is a very large topic and could easily be a book by itself. However, we want to provide you with a gentle introduction so that you'll have a firm foundation into the technique. Unit testing is when a developer verifies that a small isolated unit of code is working properly. Testing a small, isolated unit, for example, a method, is important for a number of reasons. First, unit tests are designed to be run often and as such they need to be fast. Some people run their tests hundreds of times a day and they need to be as quick to execute as possible. If a method accesses the file system or a database, for example, then the additional overhead can quickly add up and cause major problems. Smaller tests are generally easier to write and maintain, which will result in your being able to write them quicker with a lower cost of ownership.

Another advantage for isolated tests is that they are less dependent on a particular file or database being in the correct place at the correct time; this can lead to more fragile tests as they are more likely to fail because of environment issues rather than the code issues. We've found that this results in long debugging sessions attempting to identify why a particular test has failed.

The aim of unit testing is to have a suite of unit tests that each exercise the system in a certain fashion. Each unit test is used to verify a particular action about the system, but also have the positive side effect of providing examples ...

Get Testing ASP.NET Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.