Organizing and Running Tests

The test cases we’ve seen so far are all runnable Ruby programs. If, for example, the test case for the Roman class was in a file called test_roman.rb, we could run the tests from the command line using this:

 $ ​​ruby​​ ​​test_roman.rb
 Run options: --seed 29842
 # Running:
 ..
 Finished in 0.000407s, 4914.0040 runs/s, 17199.0141 assertions/s.
 2 runs, 7 assertions, 0 failures, 0 errors, 0 skips

Minitest is clever enough to run the tests even though there’s no main program. It collects all the test case classes and runs each in turn.

If we want, we can ask it to run a particular set of test methods based on a naming pattern:

 $ ​​ruby​​ ​​test_roman.rb​​ ​​-n​​ ​​test_range
 Run options: -n test_range --seed ...

Get Programming Ruby 3.3 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.