我如何有效地强制Minitest按顺序运行我的测试?

时间:2022-10-15 23:31:00

I know. This is discouraged. For reasons I won't get into, I need to run my tests in the order they are written. According to the documentation, if my test class (we'll call it TestClass) extends Minitest::Unit::TestCase, then I should be able to call the public method i_suck_and_my_tests_are_order_dependent! (Gee - do you think the guy who created Minitest had an opinion on that one?). Additionally, there is also the option of calling a method called test_order and specifying :alpha to override the default behavior of :random. Neither of these are working for me.

我知道。这是不鼓励的。由于我不会进入的原因,我需要按照它们编写的顺序运行我的测试。根据文档,如果我的测试类(我们称之为TestClass)扩展Minitest :: Unit :: TestCase,那么我应该能够调用公共方法i_suck_and_my_tests_are_order_dependent! (哎呀 - 你觉得创造Minitest的那个人对那个人有意见吗?)。此外,还可以选择调用名为test_order的方法并指定:alpha来覆盖默认行为:random。这些都不适合我。

Here's an example:

这是一个例子:

class TestClass < Minitest::Unit::TestCase

  #override random test run ordering
  i_suck_and_my_tests_are_order_dependent!

  def setup
    ...setup code
  end

  def teardown
    ...teardown code
  end

  def test_1
    test_1 code....
    assert(stuff to assert here, etc...)
    puts 'test_1'
  end

  def test_2
    test_2_code
    assert(stuff to assert here, etc...)
    puts 'test_2'
  end

end

When I run this, I get:

当我运行这个时,我得到:

undefined method `i_suck_and_my_tests_are_order_dependent!' for TestClass:Class (NoMethodError)

If I replace the i_suck method call with a method at the top a la:

如果我用顶部的方法替换i_suck方法调用a la:

def test_order
  :alpha
end

My test runs, but I can tell from the puts for each method that things are still running in random order each time I run the tests.

我的测试运行,但我可以从每个方法的puts中告诉我每次运行测试时事物仍以随机顺序运行。

Does anyone know what I'm doing wrong? Thanks.

有谁知道我做错了什么?谢谢。

5 个解决方案

#1


4  

If you just add test_order: alpha to your test class, the tests will run in order:

如果只是将test_order:alpha添加到测试类中,测试将按顺序运行:

class TestHomePage

def self.test_order
 :alpha
end

def test_a
 puts "a"
end

def test_b
 puts "b"
end

end 

#2


3  

i_suck_and_my_tests_are_order_dependent! may be a later addition to minitest & not available as a Ruby core method. In that case, you'd want to force use of your gem version:

i_suck_and_my_tests_are_order_dependent!可能是minitest的后续添加,不作为Ruby核心方法提供。在这种情况下,您需要强制使用您的gem版本:

require 'rubygems'
gem 'minitest'

#3


2  

I think that the method *test_order* should be a class method and not a instance method like so:

我认为方法* test_order *应该是一个类方法,而不是像这样的实例方法:

# tests are order dependent
def self.test_order
  :alpha
end

#4


1  

Note that, as of minitest 5.10.1, the i_suck_and_my_tests_are_order_dependent! method/directive is completely nonfunctional in test suites using MiniTest::Spec syntax. The Minitest.test_order method is apparently not being called at all.

请注意,从minitest 5.10.1开始,i_suck_and_my_tests_are_order_dependent!方法/指令在使用MiniTest :: Spec语法的测试套件中完全不起作用。 Minitest.test_order方法显然根本没有被调用。

EDIT: This has been a known issue since Minitest 5.3.4: see seattlerb/minitest#514 for the blow-by-blow wailing and preening.

编辑:自Minitest 5.3.4以来,这是一个众所周知的问题:请参阅seattlerb / minitest#514,了解哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇

You and I aren't the ones who "suck". What's needed is a BDD specification tool for Ruby without the bloat of RSpec and without the frat-boy attitude and contempt for wider community practices of MiniTest. Does anyone have any pointers?

你和我不是那些“吮吸”的人。我们需要的是一个Ruby的BDD规范工具,没有RSpec的膨胀,没有兄弟会的态度和对MiniTest更广泛的社区实践的蔑视。有没有人有任何指针?

#5


0  

The best way to interfere in this chain may be to override a class method runnable_methods:

干扰此链的最佳方法可能是覆盖类方法runnable_methods:

def self.runnable_methods
  ['run_first'] | super | ['run_last']
end

#Minitest version:
def self.runnable_methods
  methods = methods_matching(/^test_/)

  case self.test_order
  when :random, :parallel then
    max = methods.size
    methods.sort.sort_by { rand max }
  when :alpha, :sorted then
    methods.sort
  else
    raise "Unknown test_order: #{self.test_order.inspect}"
  end
end

You can reorder test any suitable way around. If you define your special ordered tests with

您可以通过任何合适的方式重新排序测试。如果您定义特殊的有序测试

test 'some special ordered test' do 
end

, don't forget to remove them from the results of super call.

,不要忘记从超级调用的结果中删除它们。

In my example I need to be sure only in one particular test to run last, so I keep random order on whole suite and place 'run_last' at the end of it.

在我的例子中,我需要确保只在一个特定的测试中运行最后,所以我在整个套件上保持随机顺序并在其末尾放置'run_last'。

#1


4  

If you just add test_order: alpha to your test class, the tests will run in order:

如果只是将test_order:alpha添加到测试类中,测试将按顺序运行:

class TestHomePage

def self.test_order
 :alpha
end

def test_a
 puts "a"
end

def test_b
 puts "b"
end

end 

#2


3  

i_suck_and_my_tests_are_order_dependent! may be a later addition to minitest & not available as a Ruby core method. In that case, you'd want to force use of your gem version:

i_suck_and_my_tests_are_order_dependent!可能是minitest的后续添加,不作为Ruby核心方法提供。在这种情况下,您需要强制使用您的gem版本:

require 'rubygems'
gem 'minitest'

#3


2  

I think that the method *test_order* should be a class method and not a instance method like so:

我认为方法* test_order *应该是一个类方法,而不是像这样的实例方法:

# tests are order dependent
def self.test_order
  :alpha
end

#4


1  

Note that, as of minitest 5.10.1, the i_suck_and_my_tests_are_order_dependent! method/directive is completely nonfunctional in test suites using MiniTest::Spec syntax. The Minitest.test_order method is apparently not being called at all.

请注意,从minitest 5.10.1开始,i_suck_and_my_tests_are_order_dependent!方法/指令在使用MiniTest :: Spec语法的测试套件中完全不起作用。 Minitest.test_order方法显然根本没有被调用。

EDIT: This has been a known issue since Minitest 5.3.4: see seattlerb/minitest#514 for the blow-by-blow wailing and preening.

编辑:自Minitest 5.3.4以来,这是一个众所周知的问题:请参阅seattlerb / minitest#514,了解哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇

You and I aren't the ones who "suck". What's needed is a BDD specification tool for Ruby without the bloat of RSpec and without the frat-boy attitude and contempt for wider community practices of MiniTest. Does anyone have any pointers?

你和我不是那些“吮吸”的人。我们需要的是一个Ruby的BDD规范工具,没有RSpec的膨胀,没有兄弟会的态度和对MiniTest更广泛的社区实践的蔑视。有没有人有任何指针?

#5


0  

The best way to interfere in this chain may be to override a class method runnable_methods:

干扰此链的最佳方法可能是覆盖类方法runnable_methods:

def self.runnable_methods
  ['run_first'] | super | ['run_last']
end

#Minitest version:
def self.runnable_methods
  methods = methods_matching(/^test_/)

  case self.test_order
  when :random, :parallel then
    max = methods.size
    methods.sort.sort_by { rand max }
  when :alpha, :sorted then
    methods.sort
  else
    raise "Unknown test_order: #{self.test_order.inspect}"
  end
end

You can reorder test any suitable way around. If you define your special ordered tests with

您可以通过任何合适的方式重新排序测试。如果您定义特殊的有序测试

test 'some special ordered test' do 
end

, don't forget to remove them from the results of super call.

,不要忘记从超级调用的结果中删除它们。

In my example I need to be sure only in one particular test to run last, so I keep random order on whole suite and place 'run_last' at the end of it.

在我的例子中,我需要确保只在一个特定的测试中运行最后,所以我在整个套件上保持随机顺序并在其末尾放置'run_last'。