How to run a single test from the command line 13

Posted by jeff Wednesday, March 07, 2007 16:02:00 GMT

I usually run my Rails tests by running rake or by having autotest running in the background (on my Mac at work - never had much luck getting autotest to work on my XP box at home).

But there are other ways of running tests that sometimes come in handy. If I just want to run one particular test class (that is, one test file), I do this:

 > ruby test/unit/product_test.rb

This also helps to demonstrate that rake doesn't do any magic to run your tests - it simply invokes the Ruby interpreter on all of your test files.

But sometimes I want to run one specific test method inside that file. On my Mac at work, I can use grep to find out what tests are in the file:

> grep "def test_" test/unit/product_test.rb
def test_create_product
def test_delete_product_if_authorized
def test_calculate_tax

(If anybody knows of an easy way to do this in Windows, please leave a comment).

I can then run just one of them by using the --name option:

> ruby test/unit/product_test.rb --name test_calculate_tax

Loaded suite test/unit/order_test
Started
.
Finished in 0.71622 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

Comments

Leave a response

  1. Bojan Mihelac   March 07, 2007 @ 04:58 PM

    You can run this on windows as well as on any other platform to get available tests.

    ruby -e "File.open('test\unit\city_test.rb') {|f| puts f.read.match(/def test_.*$/)}"

    but I think it would be better to write Rake task that would include test class and print all methods starting with test_.

    regards, Bojan

  2. Bojan Mihelac   March 07, 2007 @ 05:03 PM

    sorry, match would read only first match, this would do it:

    ruby -e "puts File.open('test\unit\city_test.rb').read.scan(/def test_.*$/)"
  3. Ben   March 07, 2007 @ 05:32 PM

    Another useful trick related to this: --name can take a regular expression, so assuming you name your test methods appropriately, you could run a whole set of grouped tests within a file with:

    ruby test/unit/product_test.rb --name /grouping/

  4. Frithjof   March 07, 2007 @ 11:00 PM

    In Windows just use this:

    type test/unit/producttest.rb | find "def test"

  5. Frithjof   March 07, 2007 @ 11:04 PM

    Replace the slash with backslash of course.

  6. Carl Fyffe   March 08, 2007 @ 12:36 AM

    Geoffrey Grosenbach wrote up a cool rake task that does just what you want:

    http://nubyonrails.com/articles/2006/07/28/foscon-and-living-dangerously-with-rake

  7. William Selman   April 08, 2007 @ 05:54 PM

    Or, Jeff, since you are using TextMate, you can just type Cmd-Shift-r with that test file in the active window.

  8. itself2377@softiesonrails.com

  9. jfmiller   April 20, 2007 @ 04:45 PM

    While I'm glad to see Frithjof's cmd line version for windows, may I suggest that it is possible to get a much more powerful CLI in windows by treating IRb as you command line. It's not a friendly as bash, but for someone who know ruby, very useful especially with judicious use of the cb command.

  10. Rob Dupuis   October 15, 2007 @ 03:22 PM

    If you want to run tests from rake you can use:

    rake test TEST=justonefile.rb

    I found my rails env wasn't being properly loaded by running the ruby file itself. eg You can also use testopts to specify other options: http://www.noobkit.com/rake-rake-testtask

  11. grosser   March 19, 2008 @ 09:37 PM

    http://pragmatig.wordpress.com/2008/03/19/testing-a-single-example-spec-testcase-test/

    • simple syntax: rake test:blog:create -> all tests in blog unit, matching create
    • searches for matching files in unit/functional/integration
    • works with rspec too rake spec:blog:create
  12. kino   May 24, 2008 @ 01:09 AM

    Consequently, it is obvious that natural causes are what first give rise to, irrespective of all empirical conditions, our ideas.

  13. Ellroy   May 30, 2008 @ 11:17 PM

    Immanent time (by this preliminary work, here roughly indicated rather than done explicitly) does not yet produce for us any abiding being by orienting the task of clarifying modes of consciousness according to accured insights.

Comment


(won't be published)