在 Minitest::Spec 测试用例中的所有测试之后执行代码?

问题描述 投票:0回答:1

我正在使用

Minitest::Spec
进行一些 Rails 测试。我有一个测试用例,我需要在其测试之前/之后执行操作。我可以像这样围绕每个测试执行此操作:

class MyTestCase < MiniTest::Spec

  before do
    set_up_whatever
  end

  after do
    clean_up_whatever
  end

  it "does this" do ... end
  it "does that" do ... end

end

但是,我只需要那些

before
after
来运行
MyTestCase
中的所有测试,而不是围绕它们中的每一个。

我知道我可以在定义测试之前在类级别执行设置,并且我知道我可以使用

MiniTest.after_run
在所有测试(包括所有其他测试用例)之后执行清理,但我需要能够在此之后进行清理在执行任何其他用例之前测试用例。

是否可以指定在单个测试用例类中的所有测试(例如

MyTestCase
)执行之后但在其他测试用例执行之前运行的代码?

ruby-on-rails unit-testing minitest
1个回答
0
投票

Jeremy Evans 的 Minitest 扩展

minitest-hooks
就是您所需要的。

gem 'minitest-hooks'

然后,来自自述文件:

require 'minitest/hooks'

describe 'something' do
  include Minitest::Hooks

  after(:all) do
    DB[:table].delete # for example
  end

  # specs go here
end

宝石还包括其他挂钩:

before(:all)

around # each spec (with a block)

around(:all) # whole test class (with a block)

我喜欢 Minitest 的原因之一就是这种模块化。

© www.soinside.com 2019 - 2024. All rights reserved.