The QA engineers behind the
Google testing blog have been sharing their unit testing tips in an ongoing series titled "Testing on the Toilet":
We write flyers about everything from dependency injection to code coverage, and then regularly plaster the bathrooms all over Google with each episode, almost 500 stalls worldwide... We've decided to share this secret weapon with the rest of the world to help spread our passion for testing, and to provide a fun and easy way for you to educate yourself and the rest of your company about these important tricks and techniques.
Whimsical name - but some serious content. The latest installment ("
Refactoring Tests in the Red") tackles a common problem - once a unit test suite becomes substantial, how can the unit tests
themselves be refactored without accidentally invalidating the tests?
If you intentionally break the code under test, the failing test can show you that your assertions are still working. For example, if you were refactoring methods in CombineHarvesterTest
, you would alter CombineHarvester
, making it return the wrong results.
Check that the reason the tests are failing is because the assertions are failing as you'd expect them to. You can then (carefully) refactor the failing tests. If at any step they start passing, it immediately lets you know that the test is broken – undo! When you're done, remember to fix the code under test and make sure the tests pass again.
Other installments of "Testing on the Toilet" include:
- Refactoring Tests in the Red (html / pdf)
- Stubs Speed Up Your Unit Tests (html / pdf)
- JavaScript: Simulating Time in jsUnit Tests (html / pdf)
- Naming Unit Tests Responsibly (html / pdf)
- Better Stubbing in Python (html / pdf)