CustomNet CustomNet - 4 months ago 31
Ruby Question

Before/After methods for Ruby Minitest framework

I'm using the latest version of Ruby(2.0.0) and Minitest (5.0.8).

I am looking for a way to create 2 methods, a before and an after. The before method should run before Minitest even starts on the testcases and the after should run once ALL of the test have finished.

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.

I have seen:

Ruby Minitest: Suite- or Class- level setup?

and

Before/After Suite when using Ruby MiniTest

Which are both out of date with the latest version of Minitest.

Is this still possible?

Answer

The before method is easy, you just configure your test setup to call a method before the Minitest starts.

The after method can be achieve by using the method Minitest.after_run(&block) provided by the Minitest API. Ex:

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