- The Test-First Technique
- Tests as Specifications
- Building Good Specifications
- Summary
Tests as Specifications
Another important thing to understand about test-driven development is this simple fact: Tests are specifications. A lot of people make the argument that tests aren’t really tests, but are specifications. Others argue that they aren’t really specifications, but are tests as the name implies.
My position is that both sides of that argument are half right. Tests are specifications. Tests are also tests. The two are not contradictory or even complementary; they are synonymous. What really distinguishes an automated test from other kinds of specifications and other kinds of tests is that it is the automation itself.
“Tests Aren’t Tests, They Are Specifications”
A large group of people exists who frequently tell new developers that tests aren’t really tests, or at least that they don’t start off that way. Tests are specifications and the fact that they also do some testing is just a side effect.
You can get into all kinds of mental gymnastics to justify this argument, and a lot of them have to do with definitions of the words test and specification. The best one I’ve heard is that tests cannot be tests without something to test, so a test is a specification until it passes; then it “falls” into the role of a test later in its life.
In my opinion, terminological correctness is just a device working in service of another motivation. That motivation is that when people think of tests as specifications, they write better tests. Another motivation is to circumvent any preconceived notions a student might have attached to the word test. Both are noble.
The “shock and awe” school of andragogy pulls stunts like this all the time. “To teach, I must first dislodge my student from his mental resting place,” teachers say. “Otherwise, hysteresis will drag him back to where he started,” they add.
Consider the following code:
[Test] public void BadSpecification() { var processor = new Processor(); Assert.That(processor.Process(-2), Is.EqualTo(-1)); Assert.That(processor.Process(-1), Is.EqualTo(0)); Assert.That(processor.Process(0), Is.EqualTo(1)); Assert.That(processor.Process(5), Is.EqualTo(216)); Assert.That(processor.Process(25), Is.EqualTo(17576)); }
The people in this camp would argue that this test might be succeeding as a test but failing as a specification and, because being a specification is what a test should really do, it is a poorly written test. By contrast, they would argue that the following test is vastly superior because a human reading it could easily understand the rule:
[Test] public void GoodSpecification() { var anyInput = 4; var processedResult = new Processor().Process(anyInput); Assert.That( processedResult, Is.EqualTo((anyInput + 1)*(anyInput + 1) * (anyInput + 1))); }
Let’s hear from the other side of the argument.
“Tests Aren’t Specifications, They Are Tests”
When I first heard someone say that tests aren’t really tests, my knee jerked and I reacted quite badly. I can’t imagine how badly I would have reacted if I didn’t consider that person a friend, but we probably wouldn’t have become friends if that was his first impression of me.
Some people, when they hear something that they believe to be wrong, immediately throw what they already think they know at the problem to see whether it goes away—and that’s exactly what I did. “No way,” I thought. “Tests aren’t specifications. They are obviously tests. That’s why we call them tests. That’s why we see them fail.”
The old me would have looked at the test with the formula and said it was a bad test because making it pass without really implementing the right rule was easy. Old me also would have said the test with many examples and no explanation of what the rule is was a good test because it forced my production code to do what it really should do.
Tests Are Executable Specifications
The problem was that I was doing exactly what my teacher didn’t want. I was clinging to a preconceived notion and not hearing what he was trying to tell me. It was a reaction to something I knew was not right but it was still holding me back.
Each camp is half right.
The kinds of tests you write in test-driven development are not distinct because they are specifications. Nor are they distinct because they are tests. Programmers have been creating both of those artifacts for, literally, generations. The interesting new bit about TDD is that it produces executable specifications.
The process produces specifications that, by definition, must be precise enough to be run frequently by a machine and, consequently, are forced to always stay up to date. That’s what makes TDD so powerful and that is why, when you have a suite of tests that hasn’t been run for any significant amount of time, an enormous amount of work typically has to be done to make it useful.
Keep in mind that a test is a specification and a test also provides guidance on how to make better tests. If each side of the argument said that one of the two tests I showed earlier was better than the other and each side is half right, then what’s the right answer in the contest between those tests?
The right answer is, “Both of those tests have good things about them but neither is the better test.” Instead of choosing one, make a test that clearly specifies the rule but also cannot easily be cheated. One option is to randomly select a number for the test that uses a formula.
private int AnyInteger() { return new Random().Next(0, 1000); } [Test] public void GoodSpecification() { var anyInput = AnyInteger(); var processedResult = new Processor().Process(anyInput); Assert.That( processedResult, Is.EqualTo((anyInput + 1)*(anyInput + 1) * (anyInput + 1))); }
Another option is to factor the formula test out into a method and then execute that method with several concrete values.
private void RunSpecification(int anyInput) { var processedResult = new Processor().Process(anyInput); Assert.That( processedResult, Is.EqualTo((anyInput + 1) * (anyInput + 1) * (anyInput + 1))); } [Test] public void GoodSpecificationWithExamples() { RunSpecification(-2); RunSpecification(-1); RunSpecification(0); RunSpecification(1); RunSpecification(5); RunSpecification(25); }
I tend toward the former option and, when I’m doing middle-tier development, I don’t much care which option a person chooses because they are both alright and they are both better than the two earlier options offered by thinking of tests exclusively as tests or as specifications.
Incremental Design
A side-effect of test-driven development is that it enables you to work in an incremental fashion regardless of the kind of process you use to regulate work in your organization (for example, Scrum or Waterfall).
Every time you write a test, you extend the body of specifications defining what your software should do a little bit. To make that test pass, you have to change your product’s behavior or design slightly. Before you can start working on the next tiny piece of your product, you have to make all your tests pass.
As a result, test-driven development has the effect of focusing work, driving you to extend your software a little bit at a time, while keeping all the existing features working. In short: It imposes a little bit of agility on your process regardless of organizational constraints.