TDD and Version Control

personal-projectstddversion controlworkflows

I am currently learning about TDD and trying to put it into practice in my personal projects. I have also used version control extensively on many of these projects. I am interested in the interplay of these two tools in a typical work flow, especially when it comes to the maxim to keep commits small. Here are some examples that come to mind:

  1. I start a new project and write a simple test to create an as-yet-nonexistent class. Should I commit the test before writing the class even though the test doesn't even compile? Or should I stub out the minimum amount of code that is needed to get the test to compile before committing?

  2. I find a bug and write a test to recreate it. Should I commit the failing test or implement the bug fix and then commit?

These are the two examples that come immediately to mind. Feel free to provide additional examples in your answer.

Edit:

I made an assumption in both examples that immediately after writing the test I will write code to make the test pass. Another situation might also arise: I work on a project using TDD for several hours without committing. When I finally make commits, I want to break up my work into small chunks. (Git makes this relatively easy even if you want to want to commit only some of the changes in a single file.)

This means that my question is as much about as what to commit as it is about when to commit.

Best Answer

Should I commit the test before writing the class even though the test doesn't even compile? Or should I stub out the minimum amount of code that is needed to get the test to compile before committing?

Of course not. You should finish both the test and the class. Committing something1 that doesn't even compile makes no sense, and will certainly make people working on the same project angry if you do it regularly.

I find a bug and write a test to recreate it. Should I commit the failing test or implement the bug fix and then commit?

No, do not commit a failing test. LeBlanc's Law states :

Later equals never.

and your test might fail for a long time. It is better to fix the problem as soon as it is detected.

Also, TDD development style tells :

Test-driven development constantly repeats the steps of adding test cases that fail, passing them, and refactoring.

If you check in a failed test, that means you didn't complete the cycle.


1 When I said commit, I meant really commit to the trunk (for git users, push your changes, so other developers would get them).

Related Topic