Ruby unit test command line options

published Oct 11, 2007

Here are a few tips and tricks for unit tests

To find all of the command line options for the test::unit suite, call a unit test with --help:

ruby some_test.rb --help

The output will look something like this

~/tests $>ruby some_test.rb --help
Test::Unit automatic runner.
Usage: test/unit/graph_test.rb [options] [-- untouched arguments]

    -r, --runner=RUNNER              Use the given RUNNER.
                                     (c[onsole], f[ox], g[tk], g[tk]2, t[k])
    -n, --name=NAME                  Runs tests matching NAME.
                                     (patterns may be used).
    -t, --testcase=TESTCASE          Runs tests in TestCases matching TESTCASE.
                                     (patterns may be used).
    -v, --verbose=[LEVEL]            Set the output level (default is verbose).
                                     (s[ilent], p[rogress], n[ormal], v[erbose])        --                           Stop processing options so that the
                                     remaining options will be passed to the
                                     test.
    -h, --help                       Display this help.

Deprecated options:
        --console                    Console runner (use --runner).
        --gtk                        GTK runner (use --runner).
        --fox                        Fox runner (use --runner).

Running only some tests

The -n option is my favourite. You can use it like this to call a single test by giving it the name of a test method:

ruby some_test.rb -n test_something_very_important

Even better, you can use a regular expression and all of the matching tests will be run. For example, if I had a graph model that tested the creation of .png, .eps and .gif files with methods called test_create_png_file, test_create_eps_file and test_create_gif_file, I could run them all with the command

ruby test/unit/graph_test.rb -n /_create_.*_file/

you can also use the -n option multiple times. So, if I just wanted to test eps and png file creation, I could use

ruby test/unit/graph_test.rb -n test_create_eps_file -n test_create_png_file

The same thing applies to selecting test cases using the -t option.

Cranking up the verbosity

Sometimes you want to see what tests are failing without waiting for all of the tests to run. Use the -v or --verbose options to do this.

ruby test/unit/<some_test>_test.rb -v v

or

ruby test/unit/<some_test>_test.rb --verbose=verbose

Then, instead of just the boring old periods appearing as each test passes

~/rails/grapher/trunk/vendor/plugins/ploticus $>ruby test/ploticus_test.rb 
Loaded suite test/ploticus_test
Started
.................
Finished in 0.10768 seconds.

17 tests, 118 assertions, 0 failures, 0 errors

you’ll see this instead:

~/rails/grapher/trunk/vendor/plugins/ploticus $>ruby test/ploticus_test.rb -v v
Loaded suite test/ploticus_test
Started
test_data_from_columns(PloticusTest): .
test_data_from_columns_with_ragged_data(PloticusTest): .
test_data_from_hash(PloticusTest): .
test_data_from_hash_with_nils(PloticusTest): .
test_data_from_hash_with_ragged_data(PloticusTest): .
test_data_from_rows(PloticusTest): .
test_first_proc_by_name(PloticusTest): .
test_has_proc(PloticusTest): .
test_num_procs_by_name(PloticusTest): .
test_pad_to(PloticusTest): .
test_ploticus_proc_has_line(PloticusTest): .
test_ploticus_proc_line(PloticusTest): .
test_png_graph_creation(PloticusTest): .
test_proc_with_colons_in_it(PloticusTest): .
test_procs_are_enumerable(PloticusTest): .
test_procs_by_name(PloticusTest): .
test_svg_graph_creation(PloticusTest): .

Finished in 0.042398 seconds.

17 tests, 118 assertions, 0 failures, 0 errors

Obviously you don’t want to do this all of the time, but it can come in handy.

Running tests with a GUI

If you get bored with the command line, try using the “runner” option. The tk option worked for me without having to install anything:

ruby some_test.rb -r tk

You’ll see something like this:

Other valid options are console, fox, gtk, gtk2, tk.

You can mix the -n and -t options as well if you want

ruby test/unit/graph_test.rb -r t -n /downloadable/

blog comments powered by Disqus