In this book, were testing with NUnit, a .NET testing framework written by Jim Newkirk, Michael Two, Alexei Vorontsov, and Charlie Poole, based on the original NUnit by Philip Craig. You can find it at http://www.nunit.org . NUnit is much the same as all the Extreme Programming test frameworks (xUnits), with two important differences:
NUnit uses the attribute feature of .NET to identify tests. As youll see, test fixtures are identified by the attribute [TestFixture] and individual tests by [Test]. This means that you can write tests that dont inherit from a fixed TestCase superclass and NUnit can still find them. At first I thought this was just gratuitously different, but Im finding that it works just fine and I rather like it.
NUnit allows you to write tests in any .NET language. So even though NUnit itself is written in C#, you can write your tests in Microsoft Visual Basic or C++ or, I suppose, even in ML or Eiffel. Language interoperation is a key characteristic of .NET and one that I consider to be quite important.
When you write tests using NUnit, you can run the NUnit application and it will run and report on your tests. There are two versions of the app, one that runs at the command prompt and a GUI version. I prefer the GUI version and will be using it throughout most of this book. When I say NUnit, Im usually talking about that GUI, which looks like this:
When you press the Run button, NUnit looks at your program, finds all the tests, and runs them. Each test fixture in your program has its own setup and teardown methods , and each test case runs with a fresh setup. This means that your tests are all automatically independent and that NUnit will run them all. If the tests all run correctly, NUnit displays a green bar, as just shown. (Trust me, the bar in the graphic above is green.) If any of the tests dont run, the bar is red.
Extreme Programming teams write lots of tests, and the rule is that we release code back to the repository only if all the tests run correctly. This may seem like a big burden , but it turns out not to be. And imagine how confident you can be in your code if you have lots of tests and every one of them is working correctly!
But enough warmup. Lets take a look at what its all about.