Maciej Rumianowski's Weblog 

Test First Programming aka TDD

by Maciej Rumianowski

Posted on Sunday Aug 17, 2014 at 04:34PM in General

Idea of Test First Programming / Test-driven Development is simple and presented below on the diagram. First write test and see it fails, then implement solution in a simple way and see green indication of success. Next refactor code so that it does not smell bad.

TDD stepsTest-driven Development steps. Diagram based on one in "Instant Cucumber BDD How-to" and other TDD books

You ain't gonna need it

Business requirements are probably already complicated and there is no need to complicate things further. Design and code upfront might be done on wrong assumptions and result in a lot of unnecessary lines of code. TDD help to focus on requirements one at a time, but still keep already implemented parts working correctly. It doesn't mean that there should be no designing/thinking at all before implementation, but just keep it to a necessary level and reiterate after gaining some more understanding of a problem. I find also that thinking to much ahead and trying to come up with a model of Everything influences badly the whole TDD process. It is then hard to do refactoring and use tests to come up with a nice solution. It becomes just testing and not TDD.

Copy and Paste

Sometimes changes to to the code are very similar to already implemented features. It is just another CRUD operation on database or there is not much business logic. In this case there is little sense in doing TDD, but that doesn't mean TDD is wrong. You still need to write unit tests (if you care about coverage), but refactoring step is not needed. However, you have to always ask yourself is it really a simple case or there are hidden requirements tangling implementation?

That's a loooong test!

During TDD tests are indicators of the quality of your code. Not only by the logic they are testing and that you have covered each branch of the code, but also by the length, setup and quality of the test code. Tests are your first class citizens and whenever they get to long or need enormous setup it means something is wrong with the class under test and overall design/architecture. It is time to apply refactoring step before trying to implement additional feature.

These are my notes on Test Driven Development, wait for more and in the meantime share your thoughts in comments.

No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed