使用Ruby MiniTest时套件之前/之后

Nic*_*lev 37 ruby test-suite minitest

RSpec before(:suite)after(:suite)MiniTest 有替代品吗?

我怀疑自定义测试运行器是有序的,但我无法想象这不是一个常见的要求,所以有人可能实现了.:-)

Cal*_*ods 26

有可用的方法setup()teardown()方法.该文档还列出before()after()可用.

编辑:您是否希望在每次测试之前或整个套件完成之前或之后运行某些东西?

  • `setup/teardown`和`before/after`在*each*test之后运行.我需要在*all*测试之前和之后运行一些东西. (12认同)
  • [试试这个](http://bfts.rubyforge.org/minitest/MiniTest/Unit.html#method-c-after_tests)Nickolay - after_tests() (10认同)
  • 没有什么可以用于before_tests,该死的.这些年来,我无法相信这仍然不存在于rspec之外. (10认同)
  • Github为每个TestCase运行一次before(all)方法:https://github.com/seattlerb/minitest/issues/61和与此主题相关的文章:http://chriskottom.com/blog/2014/10/4-奇妙-方式到建立状态合MINITEST / (2认同)
  • 在整个套件完成Minitest> 5.0后运行代码,请参阅[此答案](http://stackoverflow.com/a/18422499/1314725) (2认同)

Ger*_*rby 21

如上面在Caley的回答和评论MiniTest::Unit中所述,包含该功能after_tests.没有before_tests或等价,但是你的minitest_helper.rb文件中的任何代码都应该在测试套件之前运行,这样才能完成这样一个功能的办公室.

警告:在Ruby上还是比较新的,在Minitest还是很新的,所以如果我错了,指正!:-)


小智 19

要使其与当前版本的Minitest(5.0.6)一起require 'minitest'使用,您需要并使用它Minitest.after_run { ... }.

warn "MiniTest::Unit.after_tests is now Minitest.after_run. ..."
Run Code Online (Sandbox Code Playgroud)

https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb https://github.com/seattlerb/minitest/blob/master/lib/minitest/unit.rb


mat*_*att 5

要在每次测试之前运行代码,请使用before.您在一个实例的上下文中操作,可能是由隐式生成的类describe,因此before在每个测试中可以访问设置的实例变量(例如,在it块内).

要在所有测试之前运行代码,只需将测试包装在类,子类MiniTest::Spec或其他内容中; 现在,在测试本身之前,您可以创建一个类或模块,设置类变量,调用类方法等,所有这些都将在所有测试中可用.

例:

require "minitest/autorun"

class MySpec < MiniTest::Spec
  class MyClass
  end
  def self.prepare
    puts "once"
    @@prepared = "prepared"
    @@count = 0
  end
  prepare
  before do
    puts "before each test"
    @local_count = (@@count += 1)
  end
  describe "whatever" do
    it "first" do
      p MyClass
      p @@prepared
      p @local_count
    end
    it "second" do
      p MyClass
      p @@prepared
      p @local_count
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这是输出,以及我在大括号中的注释,解释输出的每一行证明:

once [this code, a class method, runs once before all tests]

Run options: --seed 29618 [now the tests are about to run]
# Running tests:

before each test [the before block runs before each test]
MySpec::MyClass [the class we created earlier is visible in each test]
"prepared" [the class variable we set earlier is visible in each test]
1 [the instance variable from the before block is visible in each test]

before each test [the before block runs before each test]
MySpec::MyClass [the class we created earlier is visible in each test]
"prepared" [the class variable we set earlier is visible in each test]
2 [the instance variable from the before block is visible each test]
Run Code Online (Sandbox Code Playgroud)

(请注意,我并不是说此输出意味着对测试运行顺序的任何保证.)

另一种方法是使用现有before但包装代码在类变量标志中仅运行一次.例:

class MySpec < MiniTest::Spec
  @@flag = nil
  before do
    unless @@flag
      # do stuff here that is to be done only once
      @@flag = true
    end
    # do stuff here that is to be done every time
  end
  # ... tests go here
end
Run Code Online (Sandbox Code Playgroud)