Minitest – 具有方法级粒度的测试套件

在升级之后,我发现相同的几个测试方法都失败了,所以我想自动测试那些而不是所有类中的所有方法。 我想列出每个类 – 方法对(例如TestBlogPosts.test_publishTestUsers.test_signup )并让它们作为测试套件一起运行。 无论是在文件中还是在命令行中,我都不在乎。

我知道这些技术可以运行几个完整的类 ,但我在这里寻找更精细的粒度。 (类似于命令行上的-n / pattern / – 运行测试方法的子集 – 但跨多个类。)

您可以放弃minitest/autorun并使用自定义的测试选择调用Minitest.run

一个例子:

 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 

结果:

 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 

当您调用以下测试时:

 Minitest.run(%w{-n /MyTest1.test_subtract/}) #select onespecific test method puts '==================' Minitest.run(%w{-n /MyTest2.test_add/}) #select one specific test method 

那你得到

 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 

Minitest.run从命令行获取您使用的相同参数。 因此,您可以在选择中使用-n选项,例如/MyTest1.test_subtract|MyTest2.test_add/

您可以使用不同的Minitest.run -definition定义不同的任务或方法来定义测试套件。

注意:您加载的测试文件中没有可能包含require 'minitest/autorun'