Avram
Avram

Reputation: 303

How do you run just one test assertion with rails Test::Unit or minitest?

I'm working on a project that has legacy tests written in minitest. How do you run just one assertion out of a test file?

Note: I have the answer for my question, I'll post it in a moment. I'm putting this here so that a) it can benefit others, and b) if I either lose track of my answer, or forget that I figured this out, hopefully I'll find this myself. And have a good laugh.

Upvotes: 0

Views: 819

Answers (1)

Avram
Avram

Reputation: 303

Answering my own question, so that others can benefit, and if I forget, hopefully I'll find it...

I discovered two discinct ways to run these tests, as well as how to specify the options. The syntax differs slightly depending on how you run them. However, the most important details are these:

  • If you specify an exact name, you must prefix the actual test name with "test: " and suffix it with ". " - both of those trailing spaces are critical
  • You can supply a regular expression, it will recognize an option of the form /whatever/ as a regex
  • the name of the option is -n or --name

Here are two ways of running a single assertion from a single test file:

bundle exec ruby -Itest path/to_test.rb '--name test_: the exact test name as discovered by running with -v. '

bundle exec rake test TEST=path/to_test.rb TESTOPTS="--name 'test_: the exact test name as discovered by running with -v. '"

And here's how it looks if you use a regex:

bundle exec rake test TEST=path/to_test.rb TESTOPTS="--name '/test name as discovered/'"

Note that there are different ways to deal with the quoting/escaping of course, and it seems you can use -n=value in addition to -n value.

Upvotes: 1

Related Questions