Ruby/Unit Test/Unit Testing

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Assert exception raised

require "test/unit" 
class TestRoman < Test::Unit::TestCase 
    def test_range 
    assert_raise(RuntimeError) { 0 } 
    assert_nothing_raised() { 1 } 
    end 
end



A test case for collection class

require "test/unit" 
class SongList 
    def initialize 
        @songs = Array.new 
    end 
    def append(song) 
        @songs.push(song) 
        self 
    end 
    def delete_first 
        @songs.shift 
    end 
    def delete_last 
        @songs.pop 
    end 
    def [](index) 
        @songs[index] 
    end 
end 
class TestSongList < Test::Unit::TestCase 
    def test_delete 
        list = SongList.new 
        s1 = Song.new("title1", "artist1", 1) 
        s2 = Song.new("title2", "artist2", 2) 
        s3 = Song.new("title3", "artist3", 3) 
        s4 = Song.new("title4", "artist4", 4) 
 
 
        list.append(s1).append(s2).append(s3).append(s4) 
        assert_equal(s1, list[0]) 
        assert_equal(s3, list[2]) 
        assert_nil(list[9]) 
        assert_equal(s1, list.delete_first) 
        assert_equal(s2, list.delete_first) 
        assert_equal(s4, list.delete_last) 
        assert_equal(s3, list.delete_last) 
        assert_nil(list.delete_last) 
    end 
end



Structuring Tests

require "test/unit" 
class TestsWhichFail < Test::Unit::TestCase 
    def test_reading 
        assert_not_nil(ARGF.read, "Read next line of input") 
    end 
end



Test case setup

require "test/unit"
class Person
  attr_accessor :first_name, :last_name, :age
  def initialize(first_name, last_name, age)
    raise ArgumentError, "Invalid age: #{age}" unless age > 0
    @first_name, @last_name, @age = first_name, last_name, age
  end
  def full_name
    first_name + " " + last_name
  end
end
 
class PersonTest < Test::Unit::TestCase
  FIRST_NAME, LAST_NAME, AGE = "J", "Y", 25
  def setup
    @person = Person.new(FIRST_NAME, LAST_NAME, AGE)
  end
  def test_first_name
    assert_equal FIRST_NAME, @person.first_name
  end
  def test_last_name
    assert_equal LAST_NAME,  @person.last_name
  end
  def test_full_name
    assert_equal FIRST_NAME + " " + LAST_NAME, @person.full_name
  end
  def test_age
    assert_equal 25, @person.age
    assert_raise(ArgumentError) { Person.new(FIRST_NAME, LAST_NAME, -4) }
    assert_raise(ArgumentError) { Person.new(FIRST_NAME, LAST_NAME, "four") }
  end
end
def test_first_name
  assert(FIRST_NAME == @person.first_name)
end
def assert_equal(expected, actual)
  assert(expected == actual)
end
# $ ruby test/person_test.rb



Test the values in an array

require "test/unit" 
class TestRoman < Test::Unit::TestCase 
    NUMBERS = [ 
    [ 1, "i" ], [ 2, "ii" ], [ 3, "iii" ], 
    [ 4, "iv"], [ 5, "v" ], [ 9, "ix" ] 
    ] 
    def test_simple 
        NUMBERS.each do |arabic, roman| 
            r = arabic.to_s
            assert_equal(roman, r.to_s) 
        end 
    end 
end



The Philosophy of Test-Driven Development

class String
  def titleize
    self.capitalize
  end
end
puts "this is a test".titleize
raise "Fail 1" unless "this is a test".titleize == "This Is A Test"
raise "Fail 2" unless "another test 1234".titleize == "Another Test 1234"
raise "Fail 3" unless "We"re testing titleize".titleize == "We"re Testing Titleize"



Unit Testing: assert_equal

require "test/unit"
class String
  def titleize
    self.gsub(/\s(\w)/) { |letter| letter.upcase }.gsub(/^\w/) do |letter|
      letter.upcase
    end
  end
end
 
class TestTitleize < Test::Unit::TestCase
  def test_basic
    assert_equal("This Is A Test", "this is a test".titleize)
    assert_equal("Another Test 1234", "another test 1234".titleize)
    assert_equal("We"re Testing", "We"re testing".titleize)
  end
end



Unit Testing: hard code value

require "test/unit" 
class TestRoman < Test::Unit::TestCase 
    def test_simple 
    assert_equal("i", 1.to_s) 
    assert_equal("ix", "ix".to_s) 
    end 
end



Varying the Algorithm with the Template Method

require "test/unit"
def empty?(s)
  s.length == 0
end
 
class EmptyTest < Test::Unit::TestCase
  def setup
    @empty_string = ""
    @one_char_string = "X"
    @long_string = "this is a test"
    @empty_array = []
    @one_element_array = [1]
    @long_array = [1, 2, 3, 4, 5, 6]
  end
  def test_empty_on_strings
    assert empty?(@empty_string)
    assert ! empty?(@one_char_string)
    assert ! empty?(@long_string)
  end
  def test_empty_on_arrays
    assert empty?(@empty_array)
    assert ! empty?(@one_element_array)
    assert ! empty?(@long_array)
  end
end
 
# $ ruby empty_test.rb



Writing Unit Tests

require "test/unit"
class Person
  attr_accessor :first_name, :last_name, :age
  def initialize(first_name, last_name, age)
    raise ArgumentError, "Invalid age: #{age}" unless age > 0
    @first_name, @last_name, @age = first_name, last_name, age
  end
  def full_name
    first_name + " " + last_name
  end
end
 
class PersonTest < Test::Unit::TestCase
  def test_first_name
    person = Person.new("J", "Y", 25)
    assert_equal "J", person.first_name
  end
  def test_last_name
    person = Person.new("J", "Y", 25)
    assert_equal "Y", person.last_name
  end
  def test_full_name
    person = Person.new("J", "Y", 25)
    assert_equal "J Y", person.full_name
  end
  def test_age
    person = Person.new("J", "Y", 25)
    assert_equal 25, person.age
    assert_raise(ArgumentError) { Person.new("J", "Y", -4) }
    assert_raise(ArgumentError) { Person.new("J", "Y", "four") }
  end
end
 
# $ ruby test/person_test.rb