15

Occasionally I'll run into the situation where fixing a bug requires that I delete a section of code. The TDD purist would (I assume) advocate writing a failing test, deleting the code, then watching the test pass.

Now, it seems really strange to have a test asserting that some code was removed. Sure, I suppose it would ensure no one dug into source control and put that code back in, but is it worth it? If it is worth it, it certainly seems less valuable than writing a test for code that has been added, right?

2
  • 9
    I think any regression test is useful, irrespective of how the bug was fixed Feb 21, 2013 at 5:32
  • 2
    The test doesn't assert the code was removed - the test asserts the bug is fixed...
    – user253751
    Jun 28, 2017 at 23:48

1 Answer 1

52

You're looking at it the wrong way. The test does not assert that code was removed. The test does assert a certain functionality.

The test does not care about the amount of code required to make it pass, nor does it realize that you have removed some code. The value of having such a test is the very same as any other test that you create due to a bug: you have confidence in the absence of the bug when the test passes and the test's integration into the build process ensures you that the bug will most likely not be reintroduced.

Yet another way to look at it from a TDD perspective is the following: When you know that deleting the code fixes the bug and you then wonder whether to write a test, you already did TDD wrong. Once you start working on the bug you should first write the test that ensures the bug's presence by failing. Only afterwards do you fix the actual bug - which may require removing code or not - and make the test pass. The question you are asking does not even arise in that way.

4
  • 4
    +1, but I can imagine the following situation: the removed code contained some absurd functionality someone added who did not understand the problem domain correctly. Now, during a code review, another dev sees that the whole part is really nonsense and the code will be removed. Having a lot of tests for such nonsense behaviour may bloat your test suite.
    – Doc Brown
    Feb 21, 2013 at 8:51
  • 2
    Clearly the removed functionality handled some input/output wrong. Clearly somebody in the further might misunderstand the problem in the same way. If you fear test suite bloat, i don't think TDD is for you. What is test suit bloat anyway?
    – Dorus
    Feb 21, 2013 at 10:12
  • 3
    @DocBrown: If they are doing TDD, then there must be some test which requires that absurd functionality, otherwise they wouldn't even have been allowed to write that code in the first place! Remember, you are only allowed to write the absolute minimum amount of code to get the test passing. If there isn't such a test, then the code should never have been written in the first place and it can just be deleted. If there is a test that forces that absurd behavior, then that test should be removed, and now we are at the same case I described before: the test is gone, delete the code. Feb 21, 2013 at 11:46
  • In both cases, you never add any tests to the test suite, and in the second case, you even remove one. However, if it turns out that the test actually makes sense, well, then the functionality wasn't so absurd after all. Feb 21, 2013 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.