Header

  1. View current page

    딥뿔이 자라나는 노트

Profile_image?t=1225424611&type=big
나를 바꾼 똑똑한 생활 습관, 스프링노트 - 여러분도 지금 시작해보세요!
47

Heckle

테스트를 테스트한다 - 루비 세디즘

"RSpec/Test::Unit is your safety belt. RCov is your airbag. Heckle is your helmet, neck brace and full fireproof suit."

"루비 프로그래밍을 하는데 RSpec이 안전벨트, RCov를 에어백이라고 한다면, Heckle은 헬멧과 목 보호대, 완벽한 방화복입니다."  -- Aslak Hellesoy

 

Heckle은 Ruby2Ruby와 ParseTree를 이용해 루비 코드를 바꿉니다. 그렇다면, 테스트는 반드시 실패해야합니다. 만일 테스트가 성공한다면, 그 테스트에는 '빈틈'이 있는 것입니다. 이런 식의 코드 변형을 통한 테스트는 루비 코드를 특히나 테스트를 더욱 견고하게 해줍니다.

 

구구단 예제

  • gugu_spec.rb

 

  1. context "구구단" do
      specify "5단을 잘 출력한다" do
        5.dan.to_a.should_be_eql [5,10,15,20,25,30,35,40,45]
      end
    end

 

  • gugu.rb
  1. class Fixnum
      def dan
        raise if self < 2 || self > 9
        (1..9).map{|i| self*i}
      end
    end

 

실행 결과:

 

  1. [deepblue:ruby/test/heckle] deepblue% spec gugu_spec.rb --heckle Fixnum.dan . Finished in 0.000549 seconds 1 specification, 0 failures **********************************************************************
    ***  Fixnum#dan loaded with 4 possible mutations
    ********************************************************************** 4 mutations remaining...
    3 mutations remaining...
    2 mutations remaining...
    1 mutations remaining... The following mutations didn't cause test failures: def dan
      raise if ((self < -5) or (self > 9))
      (1..9).map do |i|
        (self * i)
      end
    end def dan
      raise if ((self < 2) or (self > 14))
      (1..9).map do |i|
        (self * i)
      end
    end

 

테스트가 부족함을 알 수 있다. 테스트를 보강하자.

 

  1. context "구구단" do
      specify "5단을 잘 출력한다" do
        5.dan.to_a.should_be_eql [5,10,15,20,25,30,35,40,45]
      end
     
      specify "우리나라 구구단은 2단부터 9단까지뿐이다" do
        lambda{ 1.dan }.should_raise
        lambda{ 10.dan }.should_raise
      end
    end

 

  1. [deepblue:ruby/test/heckle] deepblue% spec gugu_spec.rb --heckle Fixnum.dan .. Finished in 0.000841 seconds 2 specifications, 0 failures **********************************************************************
    ***  Fixnum#dan loaded with 4 possible mutations
    ********************************************************************** 4 mutations remaining...
    3 mutations remaining...
    2 mutations remaining...
    1 mutations remaining... The following mutations didn't cause test failures: def dan
      raise if ((self < 2) or (self > 6))
      (1..9).map do |i|
        (self * i)
      end
    end

 

아직도 부족하다

 

  1. context "구구단" do
      specify "5단을 잘 출력한다" do
        5.dan.to_a.should_be_eql [5,10,15,20,25,30,35,40,45]
      end
     
      specify "우리나라 구구단은 2단부터 9단까지뿐이다" do
        lambda{ 1.dan }.should_raise
        lambda{ 10.dan }.should_raise     (2..9).each do |i|
          lambda{ i.dan }.should_not_raise
        end
      end
    end

 

  1. [deepblue:ruby/test/heckle] deepblue% spec gugu_spec.rb --heckle Fixnum.dan .. Finished in 0.000974 seconds 2 specifications, 0 failures **********************************************************************
    ***  Fixnum#dan loaded with 4 possible mutations
    ********************************************************************** 4 mutations remaining...
    3 mutations remaining...
    2 mutations remaining...
    1 mutations remaining...
    No mutants survived. Cool!

 

드디어 성공했다!

 

참고 자료

History

Last edited on 11/22/2007 01:04 by deepblue

Comments (0)

You must log in to leave a comment. Please sign in.