Library tutorials & articles

Test-Driven Development in .NET

Introduction & Unit Tests

Introduction

Although developers have been unit testing their code for years, it was typically performed after the code was designed and written. As a great number of developers can attest, writing tests after the fact is difficult to do and often gets omitted when time runs out. Test-driven development (TDD) attempts to resolve this problem and produce higher quality, well-tested code by putting the cart before the horse and writing the tests before we write the code. One of the core practices of Extreme Programming (XP), TDD is acquiring a strong following in the Java community, but very little has been written about doing it in .NET.

What Are Unit Tests?

According to Ron Jeffries, Unit Tests are "programs written to run in batches and test classes. Each typically sends a class a fixed message and verifies it returns the predicted answer." In practical terms this means that you write programs that test the public interfaces of all of the classes in your application. This is not requirements testing or acceptance testing. Rather it is testing to ensure the methods you write are doing what you expect them to do. This can be very challenging to do well. First of all, you have to decide what tools you will use to build your tests. In the past we had large testing engines with complicated scripting languages that were great for dedicated QA teams, but weren't very good for unit testing. What journeyman programmers need is a toolkit that lets them develop tests using the same language and IDE that they are using to develop the application. Most modern Unit Testing frameworks are derived from the framework created by Kent Beck for the first XP project, the Chrysler C3 Project. It was written in Smalltalk and still exists today, although it has gone through many revisions. Later, Kent and Erich Gamma (of Patterns fame) ported it to Java and called it jUnit. Since then, it has been ported to many different languages, including C++, VB, Python, Perl and more.

Comments

  1. 12 May 2009 at 20:55

    Well explained. More about TDD using C# can be found here Link Text

  2. 22 Aug 2007 at 12:01

    Hello, would it be possible to post all of the code regarding the "Using Mock Objects - DotNetMock" article.  The page outlines the process and most of the code, but it'd be great to see the UI that is actually being tested against the mocked version - i.e. it is slightly confusing about where the line is drawn on how much of the actual UI is being tested and being able to execute the real code against the mocked code would clarify how the view and the controller interact.

    Many thanks

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Test-Driven Development in .NET.

Leave a comment

Sign in or Join us (it's free).

Peter Provost
AddThis

Related podcasts

  • The Past, Present and Future of .NET Unit Testing Frameworks

    Scott gets a rare chance to sit down in person with developers from three .NET Unit Testing Frameworks. Charlie Poole from NUnit, Jeff Brown from MbUnit, Brad Wilson from xUnit.NET as well as Roy Osherove, the author of the upcoming "Art of Unit Testing."

Events coming up

We'd love to hear what you think! Submit ideas or give us feedback