The timing seems right to demonstrate a pattern for testing asynchronous processing. This follows up on Steve's assertion: Don't make assumptions, write the test.
For purposes of demonstration our user story will be: As a program I want to output a message after an event is raised. So let us start by writing a test. Our story implies an event reader and a writer. So let us use the runtime execution provided by test runner and make that the reader. The writer will be some other simple class.
namespace Test.AsynchronousProcessing
{
[TestFixture]
public class EventReader
{
[Test]
public void CreateInstance()
{
Assert.IsInstanceOfType(typeof (EventWriter), new EventWriter());
}
}
}
If you run the tests for EventReader, CreateInstance will fail of course. So let us do the minimal necessary to make the test pass: create EventWriter class. By the way...by convention the code listings show only relevant material.
public class EventWriter
{
}
Most product architects using NAnt have never delved into its precursor, Ant. The converse is true as well. After spending some time researching Ant I have a few thoughts (gasp):
I have mentioned the PluralSight guys from time to time - they sort of amaze me. Dads all of them, yet somehow they seem consistently on fire. Andera's recent TDD musings are a must-read if you are ready to push the envelope of where TDD can be use(d)(ful). And that is where I am. For four years I have used an agile development mindset (methodology if I was the architect) - committed to the basic precepts of assert-build-integration.
But sometimes, what you're testing is a lot more complicated, like a WinForms control. There, the thing we have to simulate is the windowing system itself, since that's really the client of my control: Windows sends events to my control, and my control sends painting commands back.
No, this is not a political column, rather another post on the topic of TDD.
A friend of mine and fellow architect wrote: “What about the loss of objectivity when a developer specifies his own test cases? It must require a conscientious mindset.” What about it? Fortunately, the blog format doesn’t require one to give answers rather one can just post opinions. I am debating this issue myself. How can we ask a developer to provide an all-inclusive set of tests even in the purely hypothetical case of a complete spec?
First of all, if we are following the mantra of TDD – add a test, get it to fail, and write code to pass the test – the initial set will not be complete. We then iterate in small steps to eventually achieve a sufficient level of confidence that the solution just meets the acceptance criteria set forth by the client. Thus, we asymptotically approach the perfect test set reaching it after an infinitesimal long time period or as soon as the system is decommissioned.
We don’t think so! After all, if web services will be our future for defining a contract both metaphorically and soon probably legally speaking, we need a better way to both enforce and verify its implementation and fulfillment.
So start with an interface definition, which can be a wsdl, business spec, interface, or whatever else you have (even a verbal from your client will do) and write a test. Make the test work and then write your implementation, and you will be able to sleep at night while your service services clients in an 24x7 global environment.
Craig Andera - one of the PluralSight dudes - writes about his experience using TDD with FlexWikiPad.
But regardless, it's been an interesting experience writing a GUI app, which is not something I usually do. It's been particularly interesting developing it with a TDD approach, as GUIs present some unique challenges.
I hope he blogs about it: the special approach hurdles developers navigate using TDD for GUI components.