What is the best course of action in TDD if, after implementing the logic correctly, the test still fails (because there is a mistake in the test)?
For example, suppose you would like to develop the following function:
int add(int a, int b) {
return a + b;
}
Suppose we develop it in the following steps:
Write test (no function yet):
// test1 Assert.assertEquals(5, add(2, 3));
Results in compilation error.
Write a dummy function implementation:
int add(int a, int b) { return 5; }
Result:
test1
passes.Add another test case:
// test2 -- notice the wrong expected value (should be 11)! Assert.assertEquals(12, add(5, 6));
Result:
test2
fails,test1
still passes.Write real implementation:
int add(int a, int b) { return a + b; }
Result:
test1
still passes,test2
still fails (since11 != 12
).
In this particular case: would it be better to:
- correct
test2
, and see that it now passes, or - delete the new portion of implementation (i.e. go back to step #2 above), correct
test2
and let it fail, and then reintroduce the correct implementation (step #4. above).
Or is there some other, cleverer way?
While I understand that the example problem is rather trivial, I'm interested in what to do in the generic case, which might be more complex than the addition of two numbers.
EDIT (In response to the answer of @Thomas Junk):
The focus of this question is what TDD suggests in such a case, not what is "the universal best practice" for achieving good code or tests (which might be different than the TDD-way).