3

I currently dynamically generate tests using a JSON file, like so:

Rakefile:

#...
Rake::TestTask.new(:run_spec_tests) { |t|
  t.libs = ['lib']
  t.pattern = 'test/test_spec.rb'
  t.verbose = true
  t.warning = false
}

test/test_spec.rb

class TestSpec < Minitest::Test
  cases = JSON.parse(open('test/tests.json', 'r').read)
  cases.each do |testcase|
    # run hundreds of tests
    define_method("test_example_#{testcase['example']}") do
      puts "Testing example #{testcase['example']}"
      actual = mything.render(testcase['input'])
      assert_equal testcase['output'], actual, testcase['output']
    end
  end
end

Every object in that JSON file has an associated number, so I'd like to be able to reference just one of those tests and have Rake::TestTask do its thing.

Unfortunately, there doesn't seem to be a way to pass arguments to a TestTask like

rake test run_spec_test 123

so that the TestTask can just run one of the previously dynamically defined methods.

Am I missing something?

ray
  • 1,966
  • 4
  • 24
  • 39

1 Answers1

0

You must use environment variables. See the documentation of TestTask

This is to run a single test from a single file:

$ bundle exec rake test TEST=test/gnuplot_printer_test.rb TESTOPTS='"--name=/^test: grouping and xtics - a nice example$/"'

But be aware of -n not working at the moment as reported in https://github.com/ruby/rake/issues/490

P.S. This is different from minitest that generates method names by replacing spaces with underscores _.

akostadinov
  • 17,364
  • 6
  • 77
  • 85