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!
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:
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.