Re: On "TDD theory"
- From: Andrew McDonagh <news@xxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 13 Jul 2005 23:35:00 +0100
J Krugman wrote:
In <db3qeg$7u2$1@xxxxxxxxxxxxxxxxx> J Krugman <jkrugman345@xxxxxxxxxxxx> writes:
...I only recently learned about the notion of starting out with a *failing* test, and I confess that I have only a tenuous grasp of what this really means (it is too trivial to write a failing test, so there must be more to it than the name suggests).
Just to clarify what I meant by the last line, the following is a trivial failing test which doesn't seem to me terribly useful:
import junit.framework.TestCase;
public class MyUselessTest extends TestCase { public void testWhoopee() throws Exception { fail("Are we having fun yet?"); } }
I don't see why this is a particularly useful starting point. Hence I conclude that what constitutes the proverbial initial "failing test" has to be something *more* than *simply* a test that fails. I have not been able to find a sufficiently articulate answer to this question. (I.e., everyone seems to assume that the definition of an "initial failing test" is self-evident, but it is not so to me.)
jill
when we say 'start off with a failing test' we don't mean write a test that does nothing but fail, like yours.
We mean write a test that uses the class(es) you are about to develop/enhance.
So, lets pretend we have a Whoopee noise generator.
we start with a test
public void testWhoopeeNoise() {
MockSoundOutputStream mockSpeaker = new MockSoundOutputStream();WhoopeeGenerator whoopeeGen = new WhoopeeGenerator();
whoopeeGen.makeANoise();
assertTrue("no noise!", mockSpeaker.wasNoiseMade() );
}Now, at this point, the test case doesn't even compile, because we haven't got a class called WhoopeeGenerator, nor the MockSoundOutputStream.
This is a failing test, because it doesn't compile.
So we create the necessary classes and the stub implementation of the required methods (e.g WhoopeeGenerator.makeANoise() ).
Still the test fails, cause we haven't actually implemented the classes, we just create empty stubs.
Once we implement just enough of the two classes to make the test pass, we can move onto the next test.
Another good reason for having the test first and having it fail first, because we haven't implemented the necessary code, is to help safe guard ourselves against dodgy tests or unexpected successes.
For example, if we wrote the test first, then wrote the necessary WhoopeeGenerator classes etc, before we even ran the test, we could have easily have made a mistake within the test code which means the test passes regardless of the production code. By Writing the test first, then running it, we expect it to fail as we haven't implement enough to make it pass. If it doesn't fail as expected, then we need to find out why.
We all get caught out with this, if we don't run the test often enough.
Sometimes test will pass because we are using classes that have already been implemented in a way to make the test pass, but because we were the one's who wrote the classes, we didn't know. But without expecting the test to fail, we would not have seen this and blindly carried on.
.
- References:
- On "TDD theory"
- From: J Krugman
- Re: On "TDD theory"
- From: J Krugman
- On "TDD theory"
- Prev by Date: Re: OOP/OOD Philosophy
- Next by Date: Re: OOP/OOD Philosophy
- Previous by thread: Re: On "TDD theory"
- Next by thread: Re: On "TDD theory"
- Index(es):
Relevant Pages
|