Ruby Minitest: Suite- or Class- level setup?
This is modified from the MiniTest docs (under Customizable Test Runner Types).
class Burger
def initialize
puts "YOU CREATED A BURGER"
end
def has_cheese?
true
end
def has_pickle?
false
end
end
gem 'minitest'
require 'minitest/unit'
MiniTest::Unit.autorun
class MyMiniTest
class Unit < MiniTest::Unit
def before_suites
# code to run before the first test
p "Before everything"
end
def after_suites
# code to run after the last test
p "After everything"
end
def _run_suites(suites, type)
begin
before_suites
super(suites, type)
ensure
after_suites
end
end
def _run_suite(suite, type)
begin
suite.before_suite if suite.respond_to?(:before_suite)
super(suite, type)
ensure
suite.after_suite if suite.respond_to?(:after_suite)
end
end
end
end
MiniTest::Unit.runner = MyMiniTest::Unit.new
class BurgerTest < MiniTest::Unit::TestCase
def self.before_suite
p "hi"
end
def self.after_suite
p "bye"
end
def setup
@burger = Burger.new
end
def test_has_cheese
assert_equal true, @burger.has_cheese?
end
def test_has_pickle
assert_equal false, @burger.has_pickle?
end
end
Note that you I included gem 'minitest'
to use the gem instead of the bundled version which didn't have the MiniTest::Unit.runner
method. Here's the output.
Run options: --seed 49053
# Running tests:
"Before everything"
"hi"
YOU CREATED A BURGER
.YOU CREATED A BURGER
."bye"
"After everything"
Finished tests in 0.000662s, 3021.1480 tests/s, 3021.1480 assertions/s.
2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
So it calls #setup
twice, but .before_suite
and .after_suite
only once, which is what you are looking for I think.
Before/After Suite when using Ruby MiniTest
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?
Creating a test suite using Ruby minitest
There is no Test::Unit::TestSuite
in minitest. You have several options, assuming your tests look something like this:
require 'minitest/unit'
require 'minitest/autorun'
class FSSessionTest < MiniTest::Unit::TestCase
def test_the_truth
assert true
end
end
The vital part here is require 'minitest/autorun'
which uses at_exit
to run all tests it can find, just before the enclosing script exits. I find this to be the easiest way for running my test suites.
Run tests with Rake
For example, you can create a Rakefile
using Rake::TestTask
which runs all the tests in your test/
directory:
require 'rake'
require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = 'tests/**/*_test.rb'
end
Run the tests with
$ rake test
Require tests in a Ruby file
If you frequently only need certain tests, you can also write a test script, something like
require './tests/fs_session_test'
require './tests/resource_test'
require './tests/rest_session_test'
require './tests/server_test'
You could also include require 'minitest/autorun'
at the top of this file to ensure, the tests are run, but i do this at the top of every test file, anyway. Run the suite with
$ ruby test.rb
Result
Both methods give you the same output, for example something like
Run options: --seed 5559
# Running tests:
....
Finished tests in 0.001909s, 2095.3379 tests/s, 2095.3379 assertions/s.
4 tests, 4 assertions, 0 failures, 0 errors, 0 skips
Because mintiest makes use of at_exit
, there is really no need to group the tests before you run them. You never get the output of only one test. Unless, of course you run a test on its own, for example with
$ ruby tests/fs_session_test.rb
Run options: --seed 43007
# Running tests:
.
Finished tests in 0.000672s, 1488.0952 tests/s, 1488.0952 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
I want to call def setup method before all tests in minitest ruby
You can use minitest-hooks
gem with before_all
something like:
require "minitest/autorun"
require 'minitest/hooks/test'
class TestLogin < MiniTest::Test
include Minitest::Hooks
def before_all
puts "setup .."
end
def test_case1
puts "testcase1"
end
def test_case2
puts "testcase2"
end
end
Now when you run the test you should see something like:
Run options: --seed 58346
# Running:
setup ..
testcase1
.testcase2
.
Finished in 0.001259s, 1588.7504 runs/s, 0.0000 assertions/s.
2 runs, 0 assertions, 0 failures, 0 errors, 0 skips
Before/After methods for Ruby Minitest framework
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
Related Topics
How Is Each_With_Object Supposed to Work
Check If an Array Is Subset of Another Array in Ruby
Render Three Different Partials Depending on Button Clicked
Functional Code Examples in Ruby
Interpretation as a Local Variable Overrides Method Name
What Are <-- Ruby Strings Called? and How to Insert Variables in Them
How to Pass Arguments to Define_Method
Add Timestamps to an Existing Table
Vim Slow with Ruby Syntax Highlighting
How to Create an Anchor and Redirect to This Specific Anchor in Ruby on Rails
Putting the Results of Pp (Or Anything Outputted to Console) into a String
Yaml Indentation for Array in Hash
How to Customize the Table Builder Plugin for a Week Calendar
How to Send Message Using Gmail API with Ruby Google API Client
Building Ruby with Rbenv and Ruby-Build Fails with Undefined Symbol: Sslv2_Method