Chapter 13. Unit Testing: Code Quality Assurance

image with no caption

Are you sure your software is working right now? Really sure? Before you sent that new version to your users, you presumably tried out the new features to ensure they all worked. But did you try the old features to ensure you didn’t break any of them? All the old features? If that question makes you worry, your program needs automated testing. Automated tests ensure your program’s components work correctly, even after you change your code.

Unit tests are the most common, most important type of automated test. And Ruby includes MiniTest, a library devoted to unit testing. This chapter will teach you everything you need to know about it!

Automated tests find your bugs before someone else does

Developer A runs into Developer B at a restaurant they both frequent...

Developer A:

Developer B:

How’s the new job going?

 
 

Not so great. I have to head back into the office after dinner. We found a bug that’s causing some customers to be billed twice as often as they should be.

Ouch. How did that get onto your billing server?

 
 

We think it might have gotten introduced a couple of months ago. One of our devs made some changes to the billing code then.

Wow, that long ago... And your tests didn’t catch it?

 
 

Tests?

Your automated tests. They didn’t fail when the bug got introduced?

 
 

Um, we don’t have any of those.

What?!

 

Your customers ...

Get Head First Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.