How to run a single test from the command line 13
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



You can run this on windows as well as on any other platform to get available tests.
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
sorry, match would read only first match, this would do it:
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/In Windows just use this:
type test/unit/producttest.rb | find "def test"
Replace the slash with backslash of course.
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
Or, Jeff, since you are using TextMate, you can just type Cmd-Shift-r with that test file in the active window.
itself2377@softiesonrails.com
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
cbcommand.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
http://pragmatig.wordpress.com/2008/03/19/testing-a-single-example-spec-testcase-test/
Consequently, it is obvious that natural causes are what first give rise to, irrespective of all empirical conditions, our ideas.
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.