Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run code before each test case in all tests in MiniTest?

Tags:

ruby

minitest

I need to run code before each test in all my tests in MiniTest.

Before I did:

MiniTest::Unit::TestCase.add_setup_hook do
   ...code to run before each test
end

After I upgraded MiniTest to version 4.7.2 it shows the following error:

undefined method `add_setup_hook' for MiniTest::Unit::TestCase:Class (NoMethodError)

I am using Ruby MRI 2.0.0p0.

SOLUTION

module MyMinitestPlugin
  def before_setup
    super
    # ...code to run before all test cases
  end

  def after_teardown
    # ... code to run after all test cases
    super
  end
end

class MiniTest::Unit::TestCase
  include MyMinitestPlugin
end
like image 363
Evgenii Avatar asked Apr 20 '13 07:04

Evgenii


2 Answers

add_setup_hook was removed in 4.6.0. https://github.com/seattlerb/minitest/commit/792a480ebeb32983b9150adae575b7c396e2ae63

Use before_setup instead.

like image 147
lifus Avatar answered Sep 20 '22 09:09

lifus


I think that you're looking for the setup() method.

like image 22
Uko Avatar answered Sep 20 '22 09:09

Uko