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

时间:2011-05-04 09:43:04

标签: ruby test-suite minitest

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

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

8 个答案:

答案 0 :(得分:25)

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

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

答案 1 :(得分:21)

如上所述,在Caley的回答和评论中,MiniTest::Unit包含函数after_tests。没有before_tests或等价物,但minitest_helper.rb文件中的任何代码都应该在测试套件之前运行,这样就可以完成这样一个功能。

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

答案 2 :(得分:19)

要使用当前版本的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.rb https://github.com/seattlerb/minitest/blob/master/lib/minitest/unit.rb

答案 3 :(得分: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

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

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

答案 4 :(得分:3)

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

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

答案 5 :(得分:0)

关于minitest的好处是它的灵活性。我一直在使用带有+ before_suite +回调的自定义MiniTest Runner。像这个例子中的东西 - Ruby Minitest: Suite- or Class- level setup?

然后告诉minitest使用自定义跑步者

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

答案 6 :(得分:0)

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

# test_helper.rb

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

答案 7 :(得分:0)

您可以将代码放在课堂外。

这就是我要做横幅的工作。

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