views:

305

answers:

7

I am going to be giving a presentation on TDD and I always struggle with what to put in the demo when giving this presentation. I usually only have about an hour to do the actual coding portion of the demo so it can't be too extravagant. However, using a "classic" example (stack,queue) is a bit simplistic for most developers. It leaves them feeling as though this won't work in a real world solution.

There are a lot of concepts one needs to make TDD work well on their project besides understanding the core testing cycle (Test, Code, Refactor).

  • IoC / Dependency Injection / Dependency Inversion
  • MVP / MVC (in order to handle testing of user interfaces)
  • Mocking

So the question is...

As a developer what kind of real world example would you like to see that incorporates these aspects and:

  • Fits into an hour or so
  • Doesn't overwhelm you
  • Isn't so simple that you can't apply it to your current project
+4  A: 

Here are some very simple examples that I can recommend off the top of my head. I've used some of these for introducing TDD to junior devs:

  • For Ioc/DI - Provide a way to save a certain set of data in either a plain text file or an XML File. Point here is that the main application shouldn't have any dependency on the filewriters that you have
  • For MVP/MVC - Make an implementation for simple data entry (e.g., First Name, Last Name, Birthday) that would work for both a web application and a console application
  • For Mocking - Create tests that would mimic parsing a plain text file with a fixed data format, including error conditions -- without actually reading a file
Jon Limjap
Could you elaborate a little more on the IoC/DI example. Do you mean that you plan to inject the actual IFileWriter service into the main application but have two different implementations of it.
Josh
Yep, something like that. It's very, very rudimentary however; I don't think it will touch on all the aspects of how IoC/DI is supposed to work.
Jon Limjap
+2  A: 

I think people would find it useful to see the differences between state-based unit testing and interaction-based unit testing.

Gilligan
+2  A: 

There is one common element in every TDD presentation I ever saw - they are way too simple to actually show the great value of TDD. They usually start with a simple app of less then 3 lines and shows how to add a feature to it.

In case you're not giving an introduction to TDD kind of presentation I would like to see how in a real application TDD helps you add feature.

Another element that is missing from most TDD presentations is Mocking and why should you use it. You should explain how isolating your testing code helps TDD (even without concrete examples).

Dror Helper
I go over mocking pretty heavily, but when you start mocking a database things get interesting. If I hand roll a mock then people lose focus of the application testing. If I use a mocking framework they get lost in the details of the technology... see my delima
Josh
Have a hand rolled mock that you prepared earlier?
Len Holgate
I typically do this, but it feels a little like I am just saying "Pay no attention to the man behind the curtain" :)
Josh
You could offer to run an extra session after to go over the mock in detail for those that are interested...
Len Holgate
Hmm... I like it. Good Idea :)
Josh
+1  A: 
Len Holgate
I want this to be a discussion that is language agnostic, but I would be using C# for the demo.
Josh
+1  A: 

What I would like to see is how to integrate TDD of new development (that integrates with) with our old code that was not written with TDD in mind.

NotDan
This is usually really difficult to do because projects are so domain specific. Most people advise against trying to retroactively test legacy applications, and focus instead on moving forward with TDD.
Josh
+2  A: 

I once sat in on a demonstration where the speaker had a few of us come up and develop a simple calculator using TDD. It's a simple enough example to be grokked immediately by anyone, but just complicated enough to illustrate how valuable TDD methodology can be.

Bill the Lizard
I have a calculator example, but typically it leaves out all the biggies like IoC/DI, and Mocking
Josh
A: 

A TDD Demo that might be not-too-technical.

Here is the kind of requirements we get a lot of.

  1. Get data from a CSV-format file (user-supplied spreadsheets, and all the problems implied by that)

  2. Pluck out the relevant fields.

  3. Create an instance of some application class (an "Employer" or an "BusinessThing" or a "DomainSpecificEvent" [don't want to use real names from my domain, but you get the idea.]

  4. Exercise that class to do some value-added processing.

  5. Ideally, persist it in a database, but you'd run out of time and have to build mocks and blah-blah-blah.

Here's the outline for the presentation.

  1. Review some requirements (get data, do calculation, persist in DB). 5 minutes.

  2. An architecture (read via CSV, create business domain entity, persist). 5 minutes.

  3. Some UT's for Business Domain Entity -- load, calculate, display, provide keys, summarize, whatever. This can take 15 minutes to define some tests. Then 15 minutes to define the class to pass the tests.

  4. Some UT's for CSV Parser -- read file, ignore headers and footers, find appropriate columns, create entity (use Mock for this.) Then the class that uses csv to do these things. This can take 10 minutes to define some tests. Then 10 minutes to define the class to pass the tests.

  5. Maybe an integration test using the unittest framework. Reads a CSV, emits entities (not mocks). Another 5-10 minutes.

Time should be about up.

S.Lott
Having done several presentations on TDD, I can tell you now that this is all pretty ambitious. Not that it can't be done, but typically anyone who comes to a TDD presentation is not already familiar with concepts like composition, IoC, and dependency injection. Because TDD forces you to employ those (best) practices, you end up having to explain them as well. Simple things like what you have laid out here end up taking 2 or 3 times longer. I have basically come to the conclusion that a good presentation on TDD simply cannot be done well in an hour. It really needs to be broken into sessions.
Josh
@Josh: I've found that you can go far without explaining IoC, dependency injection and other subtleties. Indeed, I find that less explanation and more examples gets people started. IoC, specifically, is one of those things that -- while true -- isn't worth a lot of time when showing how testing can drive development. I try to show how to write proper tests and leave the jargon for later.
S.Lott