After an upgrade, I'm finding the same several test methods failing, so I'd like to automate testing just those instead of all methods in all classes. I want to list each class-method pair (e.g. TestBlogPosts.test_publish
, TestUsers.test_signup
) and have them run together as a test suite. Either in a file or on the command-line, I don't really care.
I'm aware of these techniques to run several entire classes, but I'm looking for finer granularity here. (Similar to what -n /pattern/ does on the command-line - to run a subset of test methods - but across multiple classes.)
You could renounce minitest/autorun
and call Minitest.run
with your self defined test selection.
An example:
gem 'minitest'
require 'minitest'
#~ require 'minitest/autorun' ##No!
#Define Test cases.
#The `puts`-statements are kind of logging which tests are executed.
class MyTest1 < MiniTest::Test
def test_add
puts "call %s.%s" % [self.class, __method__]
assert_equal(2, 1+1)
end
def test_subtract
puts "call %s.%s" % [self.class, __method__]
assert_equal(0, 1-1)
end
end
class MyTest2 < MiniTest::Test
def test_add
puts "call %s.%s" % [self.class, __method__]
assert_equal(2, 1+1)
end
def test_subtract
puts "call %s.%s" % [self.class, __method__]
assert_equal(1, 1-1) #will fail
end
end
#Run two suites with defined test methods.
Minitest.run(%w{-n /MyTest1.test_subtract|MyTest2.test_add/}) #select two specific test method
The result:
Run options: -n "/MyTest1.test_subtract|MyTest2.test_add/" --seed 57971
# Running:
call MyTest2.test_add
.call MyTest1.test_subtract
.
Finished in 0.002313s, 864.6753 runs/s, 864.6753 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
When you call the following test:
Minitest.run(%w{-n /MyTest1.test_subtract/}) #select onespecific test method
puts '=================='
Minitest.run(%w{-n /MyTest2.test_add/}) #select one specific test method
then you get
Run options: -n /MyTest1.test_subtract/ --seed 18834
# Running:
call MyTest1.test_subtract
.
Finished in 0.001959s, 510.4812 runs/s, 510.4812 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
==================
Run options: -n /MyTest2.test_add/ --seed 52720
# Running:
call MyTest2.test_add
.
Finished in 0.000886s, 1128.0825 runs/s, 1128.0825 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
The Minitest.run
takes the same parameters you use from the command line. So you can use the -n
option with your selection, e.g. /MyTest1.test_subtract|MyTest2.test_add/
.
You could define different tasks or methods with different Minitest.run
-definition to define your test suites.
Attention:
No test file you load may contain a require 'minitest/autorun'
.