BT

Unit Testing Tips from Google

by Kurt Christensen on Apr 27, 2007 |
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)

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT