Professional Documents
Culture Documents
Programmer Test Principles. BDD Versus TDD. This Test Tool Versus - by Kent Beck - Medium PDF
Programmer Test Principles. BDD Versus TDD. This Test Tool Versus - by Kent Beck - Medium PDF
Kent Beck
BDD versus TDD. This test tool versus that test tool. Test-before versus test-after versus
this-works-trust-me. At some point I got tired of the debates about details. I prefer to
discuss principles.
Details go back and forth without any push to conclusion. Chocolate versus vanilla.
Chocolate. Vanilla. Chocolate. Vanilla.
Principles
Principles, on the other hand, provide a generative basis for discussion. If we agree on
principles but my context is different, then our answers can differ without disagreement.
The principles generates different answers in different contexts.
Disagreement in principle is also more productive than disagreement in detail. I can say
I prioritize speed over completeness while you prioritize completeness over speed. We
can both be “right” in our context, that is our decisions can best meet our needs, but by
discussing at the level of principles we give ourselves the best chance to understand our
differences.
Programmer Tests
Tests are an oracle. Oh great oracle, what will happen if I deploy now? Disaster, my
child. Tests predict deployment consequences.
Programmer tests should be fast. Feedback should not disrupt programming flow. Slow
tests encourage batching coding decisions. Subsequent test failures are harder to debug.
Was it decision 1, decision 2, or the combination of the two?
How fast is fast? Sub-second seems to be one discontinuity. That’s the amount of wait
time that is not worth glancing away. Ten seconds is long enough to sit back and glance
away, but not long enough to start something new. A minute is a context switch. A
minute is long enough that you are tempted to reduce the number of times you run the
tests, increasing the cost of debugging.
Programmer tests should be deterministic. I wouldn’t have thought that this would be
controversial, except I regularly see “flaky” tests. I liked the Facebook policy of simply
deleting non-deterministic tests. If you don’t want to lose coverage, change the design so
it’s testable and write the test again.
Programmer tests should be predictive. If your oracle says, “Go ahead, deploy,” and
deployment fails, you’ll stop believing your oracle.
Programmer tests should be cheap to write. We don’t get paid for tests, we get paid for
code that a) works and b) can be changed. Tests can help with that but, all else equal,
less effort on tests is better.
Programmer tests should be cheap to read. Tests, like code, are read more than written.
Run reliably.
Predict deployability.
Be cheap to read.
Be cheap to change.
That’s a difficult set of constraints to resolve. Some of them are contradictory. Figuring
out which are more important and how best to satisfy the most important — that’s your
job.
Space
My concern as I coach programmers is that they haven’t explored the space of tests. Tests
just have to run in a minute or more. Tests just have to be non-deterministic. Tests just
have change when code structure changes. Nope.
Take these principles. Find a test that violates them. Imagine the equivalent test that
would satisfy the principles. If you can’t write that test, imagine the design of the
software under test that would enable you to write a test satisfying the principles of
programmer tests.