47

Is there an alternative to RSpec's before(:suite) and after(:suite) in MiniTest?

I suspect that a custom test runner is in order, however I cannot imagine it is not a common requirement, so somebody has probably implemented in. :-)

Nickolay Kolev
  • 1,179
  • 2
  • 9
  • 14

8 Answers8

30

There are setup() and teardown() methods available. The documentation also lists before() and after() as being available.

Edit: Are you looking to run something before each test or before or after the whole suite is finished?

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
Caley Woods
  • 4,707
  • 4
  • 29
  • 38
  • 21
    `setup/teardown` and `before/after` run after *each* test. I need to run something before and after *all* tests. – Nickolay Kolev May 04 '11 at 14:56
  • 11
    [Try this](http://bfts.rubyforge.org/minitest/MiniTest/Unit.html#method-c-after_tests) Nickolay -- after_tests() – Caley Woods May 04 '11 at 19:39
  • Thanks, Caley! I don't know how I missed it. :-) – Nickolay Kolev May 05 '11 at 07:45
  • 14
    nothing for before_tests though, dammit. I can't believe this still doesn't exist outside of rspec after all these years. – gtd Oct 20 '11 at 23:11
  • Update to my comment on May 4, 2011 -- The new documentation appears to be [here](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/minitest/unit/rdoc/MiniTest/Unit.html). The first method listed shows after_tests() – Caley Woods Oct 21 '14 at 16:36
  • 2
    Github issue for a before(all) method to run once per TestCase: https://github.com/seattlerb/minitest/issues/61 and article related to this topic: http://chriskottom.com/blog/2014/10/4-fantastic-ways-to-set-up-state-in-minitest/ – mahemoff Apr 18 '15 at 08:06
  • 2
    See [this answer](http://stackoverflow.com/a/18422499/1314725) for running code after the whole suite is finished with Minitest > 5.0 – Victor Klos Oct 16 '15 at 09:33
  • @CaleyWoods `after_tests` is now replaced by `after_run` – elquimista Feb 14 '17 at 11:54
  • Refer to Gert's answer for before all functionality. – Dylan Vander Berg May 15 '17 at 13:56
25

As noted above in Caley's answer and comments, MiniTest::Unit contains the function after_tests. There is no before_tests or equivalent, but any code in your minitest_helper.rb file should be run before the test suite, so that will do the office of such a function.

Caveat: Still relatively new at Ruby, and very new at Minitest, so if I'm wrong, please correct me! :-)

Gert Sønderby
  • 713
  • 7
  • 16
21

To get this to work with the current version of Minitest (5.0.6) you need to require 'minitest' and use Minitest.after_run { ... }.

warn "MiniTest::Unit.after_tests is now Minitest.after_run. ..."

https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb https://github.com/seattlerb/minitest/blob/master/lib/minitest/unit.rb

dereckrx
  • 446
  • 4
  • 5
6

To run code before each test, use before. You're operating here in the context of an instance, possibly of a class generated implicitly by describe, so instance variables set in before are accessible in each test (e.g. inside an it block).

To run code before all tests, simply wrap the tests in a class, a subclass of MiniTest::Spec or whatever; now, before the tests themselves, you can create a class or module, set class variables, call a class method, etc., and all of that will be available in all tests.

Example:

require "minitest/autorun"

class MySpec < MiniTest::Spec
  class MyClass
  end
  def self.prepare
    puts "once"
    @@prepared = "prepared"
    @@count = 0
  end
  prepare
  before do
    puts "before each test"
    @local_count = (@@count += 1)
  end
  describe "whatever" do
    it "first" do
      p MyClass
      p @@prepared
      p @local_count
    end
    it "second" do
      p MyClass
      p @@prepared
      p @local_count
    end
  end
end

Here's the output, along with my comments in braces explaining what each line of the output proves:

once [this code, a class method, runs once before all tests]

Run options: --seed 29618 [now the tests are about to run]
# Running tests:

before each test [the before block runs before each test]
MySpec::MyClass [the class we created earlier is visible in each test]
"prepared" [the class variable we set earlier is visible in each test]
1 [the instance variable from the before block is visible in each test]

before each test [the before block runs before each test]
MySpec::MyClass [the class we created earlier is visible in each test]
"prepared" [the class variable we set earlier is visible in each test]
2 [the instance variable from the before block is visible each test]

(Note that I do not mean this output to imply any guarantee about the order in which tests will run.)

Another approach is to use the existing before but wrap code to run only once in a class variable flag. Example:

class MySpec < MiniTest::Spec
  @@flag = nil
  before do
    unless @@flag
      # do stuff here that is to be done only once
      @@flag = true
    end
    # do stuff here that is to be done every time
  end
  # ... tests go here
end
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • A warning to anyone using the `prepare` idiom above, as I did, in Rails to set up some database records. If you have even a blank file in the `test/fixtures` directory, anything you put in the database through `prepare` will get wiped by fixture setup. Because the variables themselves are still present, weirdness will ensue. – Leo Oct 23 '13 at 10:01
  • (Of course, really one should use fixtures for this anyway, I was being naughty). – Leo Oct 23 '13 at 10:02
4

One simple way to do this is to write a guarded class method, and then call that in a begin.

A Minitest::Spec example:

describe "my stuff" do
  def self.run_setup_code
    if @before_flag.nil?
      puts "Running the setup code"
      @before_flag = true
    end
  end

  before do
    self.class.run_setup_code
  end

  it "will only run the setup code once" do
    assert_equal 1, 1
  end

  it "really only ran it once" do
    assert_equal 1,1
  end
end

...to get

Run options: --seed 11380

# Running:

Running the setup code
..

Finished in 0.001334s, 1499.2504 runs/s, 1499.2504 assertions/s.

2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
Bill Dueber
  • 2,706
  • 17
  • 16
1

You can just place the code outside of the class.

This is what I do to have a banner.

require 'selenium-webdriver'
require 'minitest/test'
require 'minitest/autorun'

class InstanceTest < Minitest::Test

    def setup
    url     = ARGV.first
    @url    = self.validate_instance(url)
        @driver = Selenium::WebDriver.for :firefox
    end
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
morissette
  • 1,071
  • 1
  • 8
  • 29
  • I voted this answer up but BEWARE, placing code outside the class only works for code that doesn't affect the database. All code outside the class is executed outside the transaction and will clutter the test database if there are any activerecord operations. – StefCo Sep 25 '21 at 10:07
0

Nice thing about minitest is its flexibility. I've been using a custom MiniTest Runner with a +before_suite+ callback. Something like in this example - Ruby Minitest: Suite- or Class- level setup?

And then tell minitest to use the custom runner

MiniTest::Unit.runner = MiniTestSuite::Unit.new
Community
  • 1
  • 1
Laknath
  • 470
  • 5
  • 5
0

You can also add an after test callback by updating your test_helper.rb (or spec_helper.rb) like this

# test_helper.rb

class MyTest < Minitest::Unit
  after_tests do
    # ... after test code
  end
end
hoitomt
  • 670
  • 1
  • 6
  • 16