Skip to content

Instantly share code, notes, and snippets.

@holysugar
Last active August 29, 2015 14:17
Show Gist options
  • Save holysugar/67a270478ed9ff41811e to your computer and use it in GitHub Desktop.
Save holysugar/67a270478ed9ff41811e to your computer and use it in GitHub Desktop.
Test::Unit で一度しか呼ばれない startup / shutdown その3 これでよかった…
require 'test/unit'
require 'active_support/all'
module BeforeAfterAll
extend ActiveSupport::Concern
included do
class << self
def beforeall(&block)
@_beforeall = block
end
def afterall(&block)
@_afterall = block
end
def startup
@_beforeall.call if @_beforeall
super if defined?(super)
end
def shutdown
super if defined?(super)
@_afterall.call if @_afterall
end
end
end
end
Test::Unit::TestCase.send(:include, BeforeAfterAll)
class SampleTest < Test::Unit::TestCase
self.test_order = :random
beforeall do
p '> before all'
end
afterall do
p '< after all'
end
test 'x' do p 'test x'; assert true end
test 'y' do p 'test y'; assert true end
sub_test_case 'inner' do
test 'inner::x' do p 'test inner::x'; assert true end
test 'inner::y' do p 'test inner::y'; assert true end
end
sub_test_case 'inner2' do
beforeall do
p '> before inner2'
end
afterall do
p '> after inner2'
end
test 'inner2::x' do p 'test inner2::x'; assert true end
sub_test_case 'inner2a' do
test 'inner2a::x' do p 'test inner2a::x'; assert true end
test 'inner2a::y' do p 'test inner2a::y'; assert true end
end
test 'inner2::y' do p 'test inner2::y'; assert true end
end
sub_test_case 'inner3' do
test 'inner3::x' do p 'test inner3::x'; assert true end
test 'inner3::y' do p 'test inner3::y'; assert true end
end
end
Test::Unit::AutoRunner.run
# $ ruby before_after_all.rb
#
# Loaded suite 2
# Started
# "> before all"
# "test x"
# ."test y"
# ."test inner::x"
# ."test inner::y"
# ."> before inner2"
# "test inner2::x"
# ."test inner2::y"
# ."test inner2a::x"
# ."test inner2a::y"
# ."> after inner2"
# "test inner3::x"
# ."test inner3::y"
# ."< after all"
#
#
# Finished in 0.003206 seconds.
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# 10 tests, 10 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
# 100% passed
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# 3119.15 tests/s, 3119.15 assertions/s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment