Background Image
Table of Contents Table of Contents
Previous Page  427 / 478 Next Page
Information
Show Menu
Previous Page 427 / 478 Next Page
Page Background

I’ve outlined the TDD test/code cycle in this book. You’ve started to get a feel for the

TDD workflow, the way you flick between writing tiny amounts of code, and running

your tests. You end up running your unit tests several times a minute, and your func‐

tional tests several times a day.

So, on a very basic level, the longer they take, the more time you spend waiting for your

tests, and that will slow down your development. But there’s more to it than that.

The Holy Flow State

Thinking sociology for a moment, we programmers have our own culture, and our own

tribal religion in a way. It has many congregations within it, such as the cult of TDD to

which you are now initiated. There are the followers of vi and the heretics of emacs. But

one thing we all agree on, one particular spiritual practice, our own transcendental

meditation, is the holy flow state. That feeling of pure focus, of concentration, where

hours pass like no time at all, where code flows naturally from our fingers, where prob‐

lems are just tricky enough to be interesting but not so hard that they defeat us…

There is absolutely no hope of achieving flow if you spend your time waiting for a slow

test suite to run. Anything longer than a few seconds and you’re going to let your at‐

tention wander, you context-switch, and the flow state is gone. And the flow state is a

fragile dream. Once it’s gone, it takes at least 15 minutes to live again.

Slow Tests Don’t Get Run as Often, Which Causes Bad Code

If your test suite is slow and ruins your concentration, the danger is that you’ll start to

avoid running your tests, which may lead to bugs getting through. Or, it may lead to

our being shy of refactoring the code, since we know that any refactor will mean having

to wait ages while all the tests run. In either case, bad code can be the result.

We’re Fine Now, but Integrated Tests Get Slower Over Time

You might be thinking, OK, but our test suite has lots of integrated tests in it—over 50

of them, and it only takes 0.2 seconds to run.

But remember, we’ve got a very simple app. Once it starts to get more complex, as your

database grows more and more tables and columns, integrated tests will get slower and

slower. Having Django reset the database between each test will take longer and longer.

Don’t Take It fromMe

Gary Bernhardt, a man with far more experience of testing than me, put these points

eloquently in a talk called

Fast Test, Slow Test .

I encourage you to watch it.

Thesis: Unit Tests Are Superfast and Good Besides That

|

399