Yesterday, was day #1 in my quest to understand Test Driven Development in .NET, Unit Testing, and NUnit.
Although I think people might use Test Driven Development and Unit Testing interchangeably, there appears to be a very important difference.
Test Driven Development
Test Driven Development (TDD) has to do with the development process. The idea here is to build tests first, before you write code. You write a test and then you write code so that the test won't fail, writing the simplest possible solution (no matter how insanely idiotic it seems) to make the test pass. You then continue in this iterative development behavior.
- Write a test.
- Run it and watch it fail.
- Write simplest possible code to make test run successfully.
- Refactor as necessary.
According to Jim Newkirk, TDD's main objective is not testing software (this is a side effect). TDD's main objective is to aid programmers and customers during the development process with unambiguous requirements.
Unit Testing
I believe Unit Testing is more about testing software. You create your application in the development methodology of your choice and then write tests. Here the tests did not drive the development of your classes and software, but were something added at the end to verify all works well.
Even in TDD you will have tests that were created not during the development process but created later to test more complex scenarios (i.e. testing of various units in combination).
NUnit
NUnit appears to be the “tool of choice” for doing TDD and Unit Testing. It is free, which sometimes explains why it is the tool of choice :) It appears incredibly easy to use. You simply build tests in the language of your choice and decorate them with NUnit attributes (e.g. [Test], [TestFixture], etc.) so that NUnit knows they are tests. Inside each test you test true-false conditions using the Assert statement in the NUnit Framework. Below is an example taken from the Getting Started Word Document for NUnit.
namespace bank
{
using NUnit.Framework;
[TestFixture]
public class AccountTest
{
[Test]
public void TransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);
}
}
}
More on this later. I just wanted to introduce the tool. I haven't built a single test yet, so I don't want to get ahead of myself :)
Resources I Used to Get Started
There are tons of articles and examples on TDD, Unit Testing, and NUnit on the Internet. A lot of the content is redundant, but certainly there is something to be said about reading different styles of writing as a certain style make speak to you more than others.
I found the following resources very helpful:
1) MSDN Architecture Webcast: patterns & practices Live: Test Driven Development - Level 200
Personally, I would start here as the content is very basic, yet interesting. Jim talks about TDD and walks you through the process of building NUnit tests from a TDD perspective.
2) Article - Unit Testing in .NET by Justin Gehtland on TheServerSide.NET
A well-written article that talks about the philosophy as well as gives some good code examples.
3) Article - Unit Testing and Test-First Development by Eric Gunnerson on MSDN
Again, a good balance between the philosophy and code examples, which is where i wanted to focus on Day 1.
4) Getting Started Document as part of the NUnit Documentation
When you download NUnit, they have a Getting Started Word Document with a banking example. Pretty basic after you looked at the above, but I wouldn't feel complete if I didn't go through it :)
5) Darrell Norton's blog post on .NET Test Driven Development
For a good list of articles, books, and software about TDD, you have to read Darrell's post and bookmark it.
Software
For a complete list, see Darrell's post above. I have no intention in the next 4 weeks to use anything but NUnit and perhaps a couple of addons. Darrell mentions a few commercial products that you may be interested in.
1) NUnit - The software. It comes with a gui and console version. I want to know both very well.
2) NUnit Addin - I don't plan on downloading for a bit (if at all), but keeping it in mind. Integrates NUnit into the Visual Studio Environment.
3) VSNUnit - I believe this in an alternative to NUnit Addin for integrating into the Visual Studio Environment.
4) NUnitASP - An extention to NUnit for automatically testing ASP.NET web pages.
Next Steps
I have already downloaded NUnit. Now I need to create some tests to get my feet wet. More on this in Day 2.