Rake and Selenium

In the post on Rails and Selenium I included a rake task for running the tests. For the lazy, here it is again.

namespace :test do
  Rake::TestTask.new(:selenium) do |t|
    t.libs << "test"
    t.pattern = 'test/selenium/**/*_test.rb'
    t.verbose = true
  end
  Rake::Task['test:selenium'].comment = "Run the selenium tests in test/selenium"
end

That is handy if you want to run all your tests, but there are a lot of situations when you really don’t want to do that. Like, when you building out new ones as I am doing now.

So how do we constrain what gets run? The TestTask page helps us out by telling us about the TEST argument.

rake test:selenium TEST=selenium/forgot_password_test.rb

This tells rake to only run test methods in selenium/forgot_password_test.rb. One thing to remember is to include the directory it is under the test one (though I suppose you might be able to change t.libs in the actual task to be test/selenium, but…).

Running only a single file is handy, but what if you are following the standard *unit pattern of having a number of logically groups test methods in a single class? By specifying just a filename you get all of them run which is still waaay too long. For this we need to dig a little deeper. Right to the actual test runner in fact.

adam:RB-1.0 adam$ testrb -h
Test::Unit automatic runner.
Usage: /usr/bin/testrb [options] [-- untouched arguments]
 
    -r, --runner=RUNNER              Use the given RUNNER.
                                     (c[onsole], f[ox], g[tk], g[tk]2, t[k])
    -b, --basedir=DIR                Base directory of test suites.
    -w, --workdir=DIR                Working directory to run tests.
    -a, --add=TORUN                  Add TORUN to the list of things to run;
                                     can be a file or a directory.
    -p, --pattern=PATTERN            Match files to collect against PATTERN.
    -x, --exclude=PATTERN            Ignore files to collect against PATTERN.
    -n, --name=NAME                  Runs tests matching NAME.
                                     (patterns may be used).
    -t, --testcase=TESTCASE          Runs tests in TestCases matching TESTCASE.
                                     (patterns may be used).
    -I, --load-path=DIR[:DIR...]     Appends directory list to $LOAD_PATH.
    -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).

Hey! -n looks promising.

But how to we get that information through rake to the runner? Going back to the TestTask page there is another argument; TESTOPTS.

rake test:selenium TEST=selenium/forgot_password_test.rb TESTOPTS="--name=test_forgot_password_no_email"

Eureka! It’s a bit of extra typing, but with this you can run a single selenium script through rake.

Post a Comment

Your email is never published nor shared. Required fields are marked *