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



是否有替代RSpec的before(:suite)after(:suite)在MiniTest?

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

setup()teardown()两种方法。文档还列出了可用的before()after()

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

如上文Caley的回答和评论所述,MiniTest::Unit包含after_tests函数。没有before_tests或同等的东西,但是您的minitest_helper.rb文件中的任何代码都应该在测试套件之前运行,这样就可以执行这样的函数。

警告:我在Ruby还是新手,在Minitest还是新手,所以如果我说错了,请纠正我!: -)

要使用当前版本的Minitest(5.0.6),您需要使用require 'minitest'Minitest.after_run { ... }

warn "MiniTest::Unit.after_tests is now Minitest.after_run. ..."

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

要在每个测试之前运行代码,使用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

下面是输出,以及我用大括号括起来的注释,解释了每一行输出所证明的内容:

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]

(请注意,我并不是说这个输出暗示任何关于测试运行顺序的保证。)

另一种方法是使用现有的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

一种简单的方法是编写一个受保护的类方法,然后在begin中调用它。

A Minitest::Spec示例:

describe "my stuff" do
  def self.run_setup_code
    if @before_flag.nil?
      puts "Running the setup code"
      @before_flag = true
    end
  end
  before do
    self.class.run_setup_code
  end
  it "will only run the setup code once" do
    assert_equal 1, 1
  end
  it "really only ran it once" do
    assert_equal 1,1
  end
end

…获取

Run options: --seed 11380
# Running:
Running the setup code
..
Finished in 0.001334s, 1499.2504 runs/s, 1499.2504 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

您可以将代码放在类的外部。

这是我做的一个横幅。

require 'selenium-webdriver'
require 'minitest/test'
require 'minitest/autorun'
class InstanceTest < Minitest::Test
    def setup
    url     = ARGV.first
    @url    = self.validate_instance(url)
        @driver = Selenium::WebDriver.for :firefox
    end

minitest的优点是它的灵活性。我一直在使用自定义MiniTest Runner +before_suite+回调。就像这个例子- Ruby Minitest:套件级别还是类级别的设置?

然后告诉minitest使用自定义runner

MiniTest::Unit.runner = MiniTestSuite::Unit.new

您还可以通过更新test_helper来添加after测试回调。(或spec_helper.rb)

# test_helper.rb
class MyTest < Minitest::Unit
  after_tests do
    # ... after test code
  end
end

相关内容

最新更新