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?
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
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.
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
How to run code before each test case in all tests in MiniTest?
add_setup_hook
was removed in 4.6.0.
https://github.com/seattlerb/minitest/commit/792a480ebeb32983b9150adae575b7c396e2ae63
Use before_setup
instead.
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
Related Topics
How to Test Exception Raising in Rails/Rspec
How to Display the Current Date in "Mm/Dd/Yyyy" Format in Rails
Differencebetween 'Try' and '&.' (Safe Navigation Operator) in Ruby
How to Set Private Instance Variable Used Within a Method Test
Set Ruby Variable If It Is Not Already Defined
How to Integrate Paypal with Ruby on Rails
Create Directory If It Doesn't Exist with Ruby
Name of This Month (Date.Today.Month as Name)
To Change Directory Inside a Ruby Script
What Is the Use of Secret_Key_Base in Rails 4
How to Get a Backtrace from a Systemstackerror: Stack Level Too Deep
Rails - Rspec - Difference Between "Let" and "Let!"
Ruby on Rails: How to Get Error Messages from a Child Resource Displayed
Why Does My Rails Rollback When I Try to User.Save
Switch Theme in an Existing Jekyll Installation
Keep Getting Oauth::Unauthorized Error When Using Oauth and Twitter Ruby Gems