Unit Testing with TestDriven.NET

Introduction

Unit testing is the automated testing of software components. The technique is used to build high-quality, reliable software by writing a suite of accompanying automated tests that validate assumptions and business requirements implemented by your software.

Learning to write good unit tests takes time, and it helps to have an experienced software developer guiding you. This article serves as an introduction to both the tools and techniques used in unit testing.

Prerequisites

Test Fixtures

Create a new project and copy the code below into a new class file.

Study the following code for a moment. The code implements a Test Fixture, which is a normal class decorated with the special attribute [TestFixture]. Test Fixtures contain Test Methods. Test Methods are decorated with the [Test] attribute. Other decorations, such as [TestFixtureSetup] and [TearDown], are used to decorate methods that have special meanings that will be explained later.

Running a Test Fixture

You can right-click on any test fixture file and run it directly from Visual Studio .NET. This is the beauty of TestDriven.NET.

Notice in your Error or Output tabs that a failure message appears.

Double-clicking on the failure will take you to the precise line that failed. Correct this line so it will pass, then re-test the Fixture.

Running a Test Method

You may also right-click anywhere inside a method and run just that one method.

Setup/Teardown Methods

If you have setup code that should be run once before any method or once after all methods, use the following attributed methods:

If you have setup code that should run once before each method or once after each method in your fixture, use the following attributed methods:

Tips on Writing Good Unit Tests

A proper unit test has these features:

Automated

No human input should be required for the test to run and pass. Often this means making use of configuration files that loop through various sets of input values to test everything that you would normally test by running your program over and over.

Unordered

Unit tests may be run in any order and often are. TestDriven.NET does not guarantee the order in which your fixtures or methods will execute, nor can you be sure that other programmers will know to run your tests in a certain order. If you have many methods sharing common setup or teardown code, use the setup/teardown methods shown above. Otherwise, everything should be contained in the method itself.

Self-sufficient

Unit tests should perform their own setup/teardown, and optionally may rely upon the setup/teardown methods described above. In no circumstances should a unit test require external setup, such as priming a database with specific values. If setup like that is required, the test method or fixture should do it.

Implementation-agnostic

Unit tests should validate and enforce business rules, not specific implementations. There is a fine line between the end of a requirement and the beginning of an implementation, yet it is obvious when you are squarely in one territory or the other. Business requirements have a unique smell: there is talk of customers, orders, and workflows. Implementation, on the other hand, smells very different: DataTables, Factories, and foreach() loops. If you find yourself writing unit tests that validate the structure of a Dictionary or a List object, there is a good chance you are testing implementation.

Unit tests are designed to enforce requirements. Therefore, implementation tests enforce implementation requirements, which is generally a Bad Idea. Implementation is the part you don't care to keep forever. Depending on your skill level, implementations may change and evolve over time to become more efficient, more stable, more secure, etc. The last thing you need are unit tests yelling at you because you found a better way to implement a business solution.

This advice runs counter to what you may read in other unit testing literature; most authors recommend testing all public methods of all classes. I find that while this is consistent with the goals of testing all code, it often forces tests that do more to enforce implementation than business requirements.

Business requirements often follow a sequence or pattern, and my view is that the pattern is the real thing to be tested. Writing unit tests for every CustomerHelper class and OrderEntryReferralFactory class often indicates that classes and methods could be organized to better follow the business requirements, or at least wrapped in classes that reflect the requirements.

Comments and Discussions

I think that article about unit testing should be more specific, and it'll be better if you describe unit testing, not used tools (especially commercial ). For example, why i should prefer TestDriven.NET for $95, instead of NUnit, which is free ?
I like VS integration, but in this case it's not required.

So, waiting for more in-depth unit testing article, with good examples of what can be done using this technology.

I'm using it, and have a good understanding of what can be done, but not newbies - they won't get this information from this article

The version of TestDriven.NET intended for use in this article is the "personal" edition, which is free. It is much more convenient than NUnit in my opinion because it is integrated with Visual Studio. NUnit does notice when you recompile, so aside from switching windows it is nearly as convenient.

As for unit testing techniques and methodologies, I understand exactly what you mean. I am writing a very in-depth article on test-driven development that makes use of this unit testing foundation. It's hard to talk much about unit testing without touching on at least one programming methodology and I wanted this article to stay methodology-free