Global setup and teardown blocks in Test::Unit
You could just patch Test::Unit::TestCase
and define a setup
method:
class Test::Unit::TestCase
def setup
puts 'in setup'
end
end
And your subclasses would just use this by default:
class FooTest < Test::Unit::TestCase
def test_truth
assert true
end
end
class BarTest < Test::Unit::TestCase
def test_truth
assert true
end
end
If a test case needed to have its own setup, you would need to call super
first to ensure that the global setup runs:
class BazTest < Test::Unit::TestCase
def setup
super
puts 'custom setup'
end
def test_truth
assert true
end
end
Is having a global setup really something you need to do, or would it be helpful to have a helper method defined on Test::Unit::TestCase
and call that in the tests that need it? The helper method approach is something that I find beneficial on my projects – the setup state and intention is clearer in each individual test and I don't need to jump around to find some "hidden" setup method. Quite often, a global setup is a code smell indicating that you need to rethink part of your design, but YMMV.
Update
Since you're using ActiveSupport, here's a first stab at something that won't require a call to super
each time you define a setup
method in your test case. I don't know how valuable it is, since it requires a call to a different method and any developer can just define their own setup
method in the test case that will invalidate this change. Here it is:
require 'rubygems'
require 'test/unit'
require 'active_support'
require 'active_support/test_case'
class ActiveSupport::TestCase
def setup_with_global
puts 'In Global setup'
setup_without_global
end
alias_method_chain :setup, :global
end
class FooTest < ActiveSupport::TestCase
def setup_without_global
puts 'In Local setup'
end
def test_truth
assert true
end
end
Test modules with Test::Unit
Yeah, your initialize should definitely suggest that you're going towards a class. A module in ruby often feels like an interface in other languages, as long as you implement some basic things when you include the module you'll get a lot for free.
Enumerable is a great example, as long as you define [] and each when you include Enumerable you suddenly get pop, push, etc.
So my gut feeling about testing modules is that you should probably be testing classes that include the module rather than testing the module itself unless the module is designed to not be included in anything, it's simply a code storage mechanism.
How to get the current testcase name in test-unit?
In Ruby 1.9.3, you can do something like this:
def setup
puts self.__name__
end
With Test::Unit, how can I run a bit of code before all tests (but not each test)?
Why do you need it inside the test? You could define it gloabl:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
GLOBAL_DATA = 11
class My_Tests < Test::Unit::TestCase
def test_1()
puts "Testing startup 1"
assert_equal(11, GLOBAL_DATA)
end
end
GLOBAL_DATA could be a (singleton)-class (respective an instance).
If you have only one testclass, you may use TestCase.startup:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
class My_Tests < Test::Unit::TestCase
def self.startup
puts "Define global_data "
@@global_data = 11
end
def test_1()
puts "Testing 1"
assert_equal(11, @@global_data = 11)
end
def test_2()
puts "Testing 2"
assert_equal(11, @@global_data = 11)
end
end
call another testcase from within a testcase in Test::Unit
Looks like I can just use regular methods. The problem for some reason had to do with the Headless gem. I reverted to using Xvfb directly.
With Test::Unit, how can I run a bit of code before all tests (but not each test)?
Why do you need it inside the test? You could define it gloabl:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
GLOBAL_DATA = 11
class My_Tests < Test::Unit::TestCase
def test_1()
puts "Testing startup 1"
assert_equal(11, GLOBAL_DATA)
end
end
GLOBAL_DATA could be a (singleton)-class (respective an instance).
If you have only one testclass, you may use TestCase.startup:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
class My_Tests < Test::Unit::TestCase
def self.startup
puts "Define global_data "
@@global_data = 11
end
def test_1()
puts "Testing 1"
assert_equal(11, @@global_data = 11)
end
def test_2()
puts "Testing 2"
assert_equal(11, @@global_data = 11)
end
end
How to define common setup and teardown logic for all tests in ruby's Test::Unit::TestCase?
Thanks to Andrew I found an answer to this here on stackoverflow.
However in the course of trying to find an answer I also noticed that in the 1.9.x branch the standard testing framework was switched to MiniTest. So actually I'm using that for testing right now. This answer explains how to achieve the same with MiniTest.
Related Topics
Return Index of All Occurrences of a Character in a String in Ruby
Problems Installing Ruby on Mountain Lion - Ruby 1.9.3 Wont' Compile
Testing Error Pages in Rails with Rspec + Capybara
Emacs Is Ignoring My Path When It Runs a Compile Command
Why Can't Singleton Methods Be Defined on Symbols or Fixnums
What's the Difference Between Object and Basicobject in Ruby
How to Make Part of a Regular Expression Optional in Ruby
Implicit Return Values in Ruby
How to Cleanly Initialize Attributes in Ruby with New
Why Does Code Need to Be Reloaded in Rails 3
How to Enable C Extension Support in Jruby
Ruby Source Code Analyzer (Something Like Pylint)
How to Send Email via Smtp with Ruby's Mail Gem
Why Does My Ruby 'Ri' Tool Not Return Results in Command Prompt