Click here to Skip to main content
15,886,110 members
Articles / Operating Systems / Windows

Unit Testing with TestDriven.NET

Rate me:
Please Sign up or sign in to vote.
4.42/5 (10 votes)
5 Jan 2007CPOL4 min read 64.6K   43   15
Learn about unit testing from an expert.

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.

SampleFixture.cs
C#
using System;
using NUnit.Framework;

namespace UnitTest
{
    [TestFixture]
    public class SampleFixture
    {
        // Run once before any methods
        [TestFixtureSetUp]
        public void InitFixture()
        {
        }

        // Run once after all test methods
        [TestFixtureTearDown]
        public void TearDownFixture()
        {
        }

        // Run before each test method
        [SetUp]
        public void Init()
        {
        }

        // Run after each test method
        [TearDown]
        public void Teardown()
        {
        }

        // Example test method
        [Test]
        public void Add()
        {
            Assert.AreEqual(6, 5, "Expected Failure.");
        }

    }
}

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.

Image 1

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

Image 2

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.

Image 3

Running a Test Method

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

Image 4

Image 5

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:

Image 6

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:

Image 7

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralA good start.... Pin
sgclark8-Jan-07 6:12
sgclark8-Jan-07 6:12 
GeneralRe: A good start.... Pin
Ben Allfree8-Jan-07 6:26
Ben Allfree8-Jan-07 6:26 
GeneralRe: A good start.... Pin
sgclark8-Jan-07 6:55
sgclark8-Jan-07 6:55 
GeneralRe: A good start.... Pin
Colin Angus Mackay8-Jan-07 6:45
Colin Angus Mackay8-Jan-07 6:45 
GeneralRe: A good start.... Pin
sgclark9-Jan-07 2:49
sgclark9-Jan-07 2:49 
GeneralHuh. Pin
roberthking8-Jan-07 4:21
roberthking8-Jan-07 4:21 
GeneralRe: Huh. Pin
Ben Allfree8-Jan-07 4:45
Ben Allfree8-Jan-07 4:45 
GeneralRe: Huh. Pin
Colin Angus Mackay8-Jan-07 6:41
Colin Angus Mackay8-Jan-07 6:41 
GeneralRe: Huh. Pin
roberthking8-Jan-07 6:41
roberthking8-Jan-07 6:41 
GeneralRe: Huh. Pin
Ben Allfree8-Jan-07 7:42
Ben Allfree8-Jan-07 7:42 
Thanks for the clarification Robert! I could tell you were an avid reader, nobody else would take the time to post.

I agree about recursive definitions. I'll think about how to improve that statement as well as how to provide some clear examples. Database initialization is a great topic. You will like the upcoming TDD article , I think.

#3 is a bold statement that could use more explanation. TDD helps to avoid tests that favor implementation over business rules, but many programmers think in code, thus their tests reflect that thought model. That is the Bad Idea of which I speak.


You and I may not initially agree on testing approaches, but I will try to make my case and explain why I disagree that implementation tests are invaluable for system integration (regression, db changes, etc as you mentioned).

The problems caused by implementation tests can best be seen in two areas.

The first area is comprised of teams that write unit tests after the code is written. Their tests almost always enforce what the code does instead of what it should do. Or more importantly, the tests focus on the current solution rather than the problem intended to be solved. Teams usually come to hate unit tests because the tests begin to prevent progress over time. They enforce implementation decisions that were made before the problem was fully understood, or by a less experienced programmer, or perhaps decisions just made in a hurry for a quick fix.

The second area where you can see the harm of implementation tests is more theoretical, but still clear. First, go read the famous Fred Brooks article about No Silver Bullets. In it, he speaks about two types of complexity: essential and accidental. He uses the terms to differentiate between complexity that is inherent in the problem to be solved versus complexity rooted in the current tools and technology used to solve the problems. If you agree with his analysis as I do, you will classify the complexity you see into those two buckets, or maybe even subdivide from there. Essential complexity is what you want to capture in testing. Accidental complexity is what you want to *cover through* testing. So while I am still advocating 100% code coverage with tests, I am advocating that assertions and test methods should reflect the essential patterns and complexity of the business problem rather than being concerned about testing every class directly. In the perfect test harness, you can refactor and otherwise change your implementation without touching your tests.

Letting go of implementation tests is a very uncomfortable proposition for many developers. If your tests cover the business problem, it will naturally test the necessary aspects of implementation. For example, testing that you can connect to the database does nothing to advance your solution that wouldn't also be covered by a business-oriented test such as "customers who update their email address are sent a confirmation message to the new address".

Your tests should be runnable wherever you deploy your application. If you can get that working, system integration is not so difficult.

Ben



GeneralRe: Huh. Pin
roberthking8-Jan-07 9:55
roberthking8-Jan-07 9:55 
GeneralRe: Huh. Pin
Ben Allfree8-Jan-07 13:19
Ben Allfree8-Jan-07 13:19 
GeneralArticle Pin
Stanislav Panasik18-Dec-06 21:02
Stanislav Panasik18-Dec-06 21:02 
GeneralRe: Article Pin
Ben Allfree18-Dec-06 21:08
Ben Allfree18-Dec-06 21:08 
GeneralRe: Article Pin
Stanislav Panasik19-Dec-06 1:45
Stanislav Panasik19-Dec-06 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.