Outline for introducing TDD

This outline is meant to serve as a guide for a developer that is experienced with Visual Studio, C# and NUnit to use in introducing Test Driven Development (TDD) to others. You, the trainer, will want discuss the why and how behind the tasks.

This guide was put together in the context of VS2005 & NUnit.

http://nunit.org/

You may opt to have developers work in pairs for this training.

Learning the basics

Start off by discussing why tests are set up in a different project.

Create new projects Polynomial & PolynomialTests

Delete class1.cs under both of them

Include a reference to the project under test

Include a reference to the test framework

Create test fixture BasicPolynomialTests (class)

We will be implementing a compute method which returns the result of f(x) = x^2 + 5x + 6

the using statements that are needed by the test framework

using NUnit.Framework;

using NUnit.Framework.Constraints;

using NUnit.Framework.SyntaxHelpers;

Don't forget to make the test fixture public

Reviewed that there are reflection tags for the test fixture, fixture setup/teardown, test setup/teardown and the test itself

When and when not to use the different setup's/teardowns

We talked about setting up test classes based on what kind of set up they need

Talked about arrange-act-assert (See William Wake) http://weblogs.java.net/blog/wwake/archive/2003/12/

It can take a little bit to show/explain "this method doesn't exist yet, we're writing the way we want it to work"

With a nice IDE the method stub is generated for you (they really liked that) .

Create BasicPolynomial class, make it public - watch for namespace conflict

review the red-green-refactor sequence

make sure everything compiles now and you have a red bar in the test runner

implement the code that computes f(x)

compile and check out the green bar

Now refactor f(x) - it can be refactored to (x+2)(x+3)

Show them were they can find reference material on both the "classic" and new "constraints" assert methods

(we used the new NUnit constraints syntax)

http://nunit.org/index.php?p=assertions&r=2.4.3

Discuss how unit tests can be used to verify expected behavior

I think a string replace does this, is my expectation correct?

Talk about how we can do TDD in environments that don't directly have a testing framework - i.e.

Access

A little thinking for the trainees

Now the user wants to be able to solve all problems of the form Ax ^ 2 + Bx + C

Create a GeneralPolynomialTests fixture

Create a new GeneralPolynomial class

Need a constructor that takes (A,B,C)

Test classes with different set up revisited

Add an empty constructor() that defaults to 2,3,5

Don't forget your test first! (in a new class)

Refactor: the empty constructor can be implemented in a number of ways

  • Setting initial values on declaration of private variables (yuck)
  • Setting the private values in the new empty constructor
  • Constructor chaining GeneralPolynomial():this(2,3,5)

http://www.c-sharpcorner.com/UploadFile/rajeshvs/ConsNDestructorsInCS11122005010300AM/ConsNDestructorsInCS.aspx

Move the new test class to it's own file

Summary

As you can see, when it's listed out, that's a lot of material for a new comer to digest for a fairly simple example.

I worked through the above in about an 1 hour and 45 minutes with a team.

After you've gone through this guide, ask if there are any questions. Let them know you are available to help.

Pick up a slew of books on TDD/xUnit topics and add them to the corporate library.

No comments:

Post a Comment