I tried to hang this shelf
on this wall.
I made a real mess of it
The shelf was nowhere near flat.
Could Test Driven Development (TDD)
or even Behaviour Driven Development (BDD)
have saved the day?
Hi, this is Gary
Welcome to Development That Pays.
A couple of episodes ago, I went through the process of hanging a shelf
in what looked suspiciously like a Waterfall process:
- I did all of the measuring - all of the transferring
- all of the drilling - popped in all of the wall plugs
- and brute-forced the screws in.
Alas, the end result was a shelf
that was wonky (not level)
But it got me thinking.
Could tests have saved me?
Is it possible to hang a shelf in a test driven way?
Is it possible to hang a shelf in a behaviour driven way?
I think we should find out.
Here's me hanging a shelf in my waterfall-like style.
Let's stop it... there.
Here's an opportunity for a test:
could come back at any time and check that the line really is level.
That could be the first unit test
More marking and measuring.
Here we have lots of opportunities for unit tests: one for each of the measurements.
What happened next? Oh yes...
I drilled all the holes
I popped in all the wall plugs.
Stop.
This is certainly a point where the unit tests could - and should - be performed.
But it's actually a few steps too late.
I've done far too much work between tests.
Lets rewind a bit.
Drill one hole.
And perform the "unit tests" to ensure the hole went in in the right place.
If the test fails: if the hole has gone in the wrong place,
Now's the time to fix it.
There's no point in continuing until all the Unit Tests pass.
Once the tests are passing, we can move on.
Drill another hole. Run the tests.
Correct as necessary.
And so on until we have all four holes in the right place
We know they are in the right place because all the unit tests are passing
What's next? In with all four plugs.
No harm in repeating the unit tests (But it's likely that they are passing)
In go the screws.
Unfortunately, it's no longer trivial to run our unit tests.
But there is another test we can run:
we can check for level.
And given that we've had "green lights" up to this point,
the chances are good that this test will pass too.
And it does. The shelf is perfectly flat.
So much for Test Driven Development (TDD)
What about Behaviour Driven Development (BDD)?
A shelf is a bit passive. It doesn't have have much in the way of behaviours.
But if we are generous with our definitions,
we could say that one behaviour that we would like
Is that when we put things on a shelf, they don't slide off
Or put it another way, the shelf should be level.
It can be argued, then, that testing the shelf for level is not only a Unit Test.
It's also a Behavioural Test.
In the sequence we've been through, this "behavioural test" was the last thing that we did.
AFTER all the work had been done.
The very opposite of "behaviour driven".
Is behaviour driven development even possible in this case?
Turns out that the method my Dad used to hang a shelf
look a lot like a behaviour driven process.
first step is marking the centre of the shelf.
We could have a test for that -
to verify that the mark really is in the centre of the shelf.
That would be a unit test.
It's the first and last time unit test that features in this process
Moving on...
Check for level. That's the behavioural test.
There it is again.
There's the behavioural test again
This time it fails.
Again, no point in continuing until "all the tests are passing"
A quick tap with a mallet.
Run the tests again - this time passing.
Which means we can move on
There's the behavioural test again
And again one final time.
Would you believe it:
It is possible to hang a shelf in a test-driven way.
AND its also possible to hang a shelf in a behaviour-driven way.
Which is better?
I have my opinion... but I'd much rather hear from you.
Please take a moment and let me know in the comments below.
Thank you very much for watching.
There's a new episode of Development That Pays each and every Wednesday.
The easiest way to get it is to subscribe to this channel.
Hit the big red button...
and I look forward to talking to next time.