简体   繁体   English

Ruby Minitest框架的Before / After方法

[英]Before/After methods for Ruby Minitest framework

I'm using the latest version of Ruby(2.0.0) and Minitest (5.0.8). 我使用的是最新版本的Ruby(2.0.0)和Minitest(5.0.8)。

I am looking for a way to create 2 methods, a before and an after. 我正在寻找一种方法来创建2个方法,一个之前和之后。 The before method should run before Minitest even starts on the testcases and the after should run once ALL of the test have finished. before方法应该在Minitest甚至在测试用例上启动之前运行,并且应该在所有测试完成后运行after。

I already use the setup() and teardown() methods that run before and after each individual test but I'm looking for something that wraps around the whole Minitest suite. 我已经使用了在每次单独测试之前和之后运行的setup()和teardown()方法,但我正在寻找包含整个Minitest套件的东西。

I have seen: 我见过:

Ruby Minitest: Suite- or Class- level setup? Ruby Minitest:套件级或类级设置?

and

Before/After Suite when using Ruby MiniTest 使用Ruby MiniTest时套件之前/之后

Which are both out of date with the latest version of Minitest. 哪些都与Minitest的最新版本过时了。

Is this still possible? 这还有可能吗?

The before method is easy, you just configure your test setup to call a method before the Minitest starts. before方法很简单,您只需将测试设置配置为在Minitest启动之前调用方法。

The after method can be achieve by using the method Minitest.after_run(&block) provided by the Minitest API. after方法可以通过使用Minitest API提供的Minitest.after_run(&block)方法来实现。 Ex: 例如:

Minitest.after_run do
  puts 'All tests finished'
  my_method_call()
end

You may want to have a look at minitest-hooks. 你可能想看看minitest-hooks。 minitest-hooks extends minitest with before(:all) and after(:all) , which do what you want when the structure of your describe block is flat. minitest-hooks使用before(:all)after(:all)扩展minitest,当你描述块的结构是平的时,它会做你想要的。 When you have nested describe blocks, you may want to take an approach that is similar to Thiago's answer . 当您有嵌套的描述块时,您可能希望采用类似于Thiago答案的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM