4. MiniTest::Unit vs Test::Unit
require 'minitest/unit'
● MiniTest new in 1.9 "a little leaner"
● has a compatibility layer for Test::Unit
● negative assertions renamed to refute
○ assert_not_nil -> refute_nil
● missing assertions
○ assert_not_raises
○ assert_not_throws
require 'test/unit'
● most of the features people used
gem install test-unit
● little used features: test cases, GUI runners
5. Example Test
require_relative 'romanbug'
require 'test/unit'
class TestRoman < Test::Unit::TestCase
def test_simple
assert_equal "i", Roman.new(1).to_s
assert_equal "ix", Roman.new(9).to_s
end
end
6. Structuring Tests
● prefix methods with test_
● setup/teardown run before/after each test
○ use passed? in teardown to see if the test
passed
7. Running Tests (files in the same folder)
● ruby test_roman.rb
○ Kernel::at_exit
● ruby test_roman.rb -n test_simple
● ruby test_roman.rb -n /simple/
8. Organizing tests
roman/
lib/
roman.rb
other files....
test/
test_roman.rb
other tests....
other files....
13. Shoulda
● less opinionated than RSpec
● code lives inside Test::Unit::TestCases
● makes test cases, so Test::Unit assertions work
14. Shoulda Example
require 'test/unit'
require 'shoulda'
require_relative 'tennis_scorer.rb'
class TennisScorerTest < Test::Unit::TestCase
context "Tennis scores" do
setup do
@ts = TennisScorer.new
end
should "start with a score of 0-0" do
assert_equal("0-0", @ts.score)
end
end
end