如何在MiniTest的所有测试中的每个测试用例之前运行代码?

时间:2013-04-20 07:57:57

标签: ruby minitest

我需要在MiniTest的所有测试中的每个测试之前运行代码。

在我做之前:

MiniTest::Unit::TestCase.add_setup_hook do
   ...code to run before each test
end

将MiniTest升级到版本4.7.2后,显示以下错误:

undefined method `add_setup_hook' for MiniTest::Unit::TestCase:Class (NoMethodError)

我正在使用Ruby MRI 2.0.0p0。

module MyMinitestPlugin
  def before_setup
    super
    # ...code to run before all test cases
  end

  def after_teardown
    # ... code to run after all test cases
    super
  end
end

class MiniTest::Unit::TestCase
  include MyMinitestPlugin
end

3 个答案:

答案 0 :(得分:6)

答案 1 :(得分:2)

我认为您正在寻找setup()方法。

答案 2 :(得分:0)

更新2019

请勿为此编写插件,插件仅用于扩展Minitest功能的gem,而不适用于测试作者。

如果您编写Minitest Specs,则可以执行以下操作:

class Minitest::Spec
  before :each do
    [do stuff]
  end
end
相关问题