Ruby Minitest:套件级别还是类级别设置?
使用内置的 Ruby Minitest 框架,有没有办法在整个套件运行之前运行一些代码,甚至在整个 TestClass 运行之前运行一次?我在 这个问题的答案中看到 Test::Unit: :after_tests 可用于在运行所有测试后运行代码;在所有测试运行之前是否有类似的方法来运行代码?
我想使用此功能在测试运行之前初始化测试数据库,并在测试全部运行后将其拆除。
谢谢!
Using the built-in Ruby Minitest framework, is there a way to run some code once before the entire suite runs, or even once before an entire TestClass runs? I see in the answer to this question that Test::Unit::after_tests can be used to run code after all tests have been run; is there a similar method to run code before all tests have run?
I would like to use this functionality to initialize a test database before the tests run and tear it down after they have all run.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是从 MiniTest docs(在可自定义测试运行器类型下)修改的。
请注意,我包含了
gem 'minitest'
来使用 gem,而不是没有MiniTest::Unit.runner
方法的捆绑版本。这是输出。所以它调用
#setup
两次,但.before_suite
和.after_suite
仅调用一次,我认为这就是您正在寻找的。This is modified from the MiniTest docs (under Customizable Test Runner Types).
Note that you I included
gem 'minitest'
to use the gem instead of the bundled version which didn't have theMiniTest::Unit.runner
method. Here's the output.So it calls
#setup
twice, but.before_suite
and.after_suite
only once, which is what you are looking for I think.获取 MiniTest 套件中所有测试运行前后时间的另一种方法是将
if
块放在setup
& 中。teardown
方法来控制这些块仅被调用一次。通过这种方式,您可以在测试套件开始时加载一次浏览器和其他依赖项(例如页面对象),然后在所有测试完成后关闭浏览器。
以下是使用 MiniTest 5.5.1 和 Watir 的示例:
An alternative way to get a handle on the time before and after all the tests in a MiniTest suite have run is to put
if
blocks in thesetup
&teardown
methods to control that these blocks only get called once.In this way, you can load a browser and other dependencies such as page objects just once at the beginning of your test suite and then close the browser at the end when all tests have completed.
Here's an example of this using MiniTest 5.5.1 and Watir: