Getting Started with Developer Testing

Carl Meyer, a web developer, recently gave a talk at PyCon 2013, Getting Started with Automated Testing. This is a great primer for developer testing. He uses Python in the interactive examples, but the lessons apply to any language.

Why Automate Tests?

  • Make sure your code is working as intended. 
  • Tests are a great documentation tool to explain the intended behavior for the next person maintaining your code. 
  • Manual testing takes time, and if you have to run the tests over and over again (due to changes in your code), you will appreciate the investment you took to automate.  He gave a great example of a friend who had to debug the 14th page of a survey.  Each code tweak meant filling in 13 forms just to get to that 14th page. 
  • Bug reporting is a lot easier if you can point to an automated test that invokes the bug.
  • Great quote: “Writing tests on weekdays is better than debugging code on weekends”

Tips for developers

  • Just get started with your first test. Use assert to get started. (Python, Java, C++, Ruby)
  • Once you have a few tests (as asserts), add a unit test framework to make your life easier. 
  • Don’t let the tool/framework get in the way, just choose a popular one and get started.
  • Test first OR code first?  Your choice.  Try out both and see which suits you better.
  • Benefit of test first is the “outside-in” development methodology. Or, programming-by-wish. (not to be confused with a wish sandwich). Programming by wish is where you write the test for the function/service you wish you had. Once you have that test, then simply implement the function.
  • If you find a bug in your code (I know, it happens), write a test that exposes that bug. The test should fail. Then, when you fix the bug, you have the test already, and the test can run as part of your regression suite from that day forward.
  • If retrofitting a large, untested, codebase, start with integration tests. Even if you only create the integration tests, you’ve won. However, if you end up refactoring that code, your integration tests give the confidence that your refactoring is successful.  While refactoring, go ahead and add some unit tests.

The video