My smiling face

Paul Dynowski

Fledgeling Web Developer, All-Around Swell Guy

Facebook Twitter GitHub LinkedIn Email

Test-driven development

Test, code, refactor. Repeat as necessary.

Dec 12, 2015

What is test-driven development?

Test-driven development is a style of coding that centers on the repetition of a short development cycle. Basically, a programmer adds a test case that defines a new function, adds a minimum amount of code to pass that test (and all previous tests), and then refactors the code.

OK, then. Why would I want to do it?

Convincing. How do I do it?

  1. Add a test - figure out what feature you would like to add, and then write a test to cover that feature.
  2. Run all tests and make sure the new one fails - this will validate that the test setup is working, that the feature you want to add doesn't already exist, and that the new test is actually useful and fails in the expected circumstances.
  3. Write new code - write code so that your new test passes; it doesn't have to be pretty, it just has to work. For now.
  4. Run the tests again - they should all pass. If so, congratulations - you added a new feature and didn't break anything. Move to the next step. Otherwise, go fix your new code.
  5. Refactor your code - clean up your code; improve the readability, split methods to maintain simple, one-function methods, remove duplication, etc.
  6. Test again - make sure your refactoring didn't break anything.
  7. Do it again - write more tests, add more features, be more awesome.

Do you maybe have a fancy graphic to illustrate the process of test-driven design?

Of course I do. Well, Wikipedia else does, and I'm heartlessly swiping it: Test Driven Development Graphic
"TDD Global Lifecycle" by Xarawn - Own work. Licensed under CC BY-SA 4.0 via Commons.

What concerns are there about the process?

Hey, look, another list!

These are all reasonable concerns, of course, but most of them can be obviated by being thorough and conscientious in your programming. Pairing may also help alleviate concerns about blind spots with the method.