2011-08-05 15 views
2

私はいくつかのRuby(Railsではなく)を書いていて、テストを書くためにshouldaを使ってtest/unitを使っています。優れたルビーテストのトレーサビリティソリューションはありますか?

私のテストから設計/要件へのトレーサビリティを実現する宝石はありますか?

すなわち:私は、彼らがテスト要件の名前で私のテストをタグ付けして、テストを失敗テストしたりしていないされている要件のレポートを生成したい、など

うまくいけば、それはルビーのためにあまりにもenterpriseyではありません。

ありがとうございます!あなたの要件をテストすることができcucumber

答えて

1

アップデート:このソリューションは、宝石のように提供されています:

http://rubygems.org/gems/test4requirements

がどんな宝石があること テストから設計/要件へのトレーサビリティを実装できるようにしますか?

私は宝石は分かりませんが、あなたの必要性は少しの実験ではどのように解決できるのかというインスピレーションがありました。あなたはこの要件は、各テストを割り当てることができるのTestCase

  • 要件に割り当てることができますRequirementList.new(1,2,3,4)
  • であなたの要件を定義する必要が

    • 試験後の要件
    • との要件にあなたは要件がテストされている概要(成功し)
    を得る結果

    そして今、例:

    gem 'test-unit' 
    require 'test/unit' 
    
    ########### 
    # This should be a gem 
    ########### 
    
    class Test::Unit::TestCase 
        def self.requirements(req) 
        @@requirements = req 
        end 
        def requirement(req) 
        raise RuntimeError, "No requirements defined for #{self}" unless defined? @@requirements 
        caller.first =~ /:\d+:in `(.*)'/ 
        @@requirements.add_test(req, "#{self.class}##{$1}") 
        end 
        alias :run_test_old :run_test 
        def run_test 
        run_test_old 
        #this code is left if a problem occured. 
        #in other words: if we reach this place, then the test was sucesfull 
        if defined? @@requirements 
         @@requirements.test_successfull("#{self.class}##{@method_name}") 
        end 
        end 
    end 
    
    class RequirementList 
        def initialize(*reqs) 
        @requirements = reqs 
        @tests = {} 
        @success = {} 
    
        #Yes, we need two at_exit. 
        #tests are done also at_exit. With double at_exit, we are after that. 
        #Maybe better to be added later. 
        at_exit { 
         at_exit do 
         self.overview 
         end 
        } 
    
        end 
        def add_test(key, loc) 
        #fixme check duplicates 
        @tests[key] = loc 
        end 
        def test_successfull(loc) 
        #fixme check duplicates 
        @success[loc] = true 
        end 
        def overview() 
        puts "Requirements overiew" 
        @requirements.each{|req| 
         if @tests[req] #test defined 
         if @success[@tests[req]] 
          puts "Requirement #{req} was tested in #{@tests[req] }" 
         else 
          puts "Requirement #{req} was unsuccessfull tested in #{@tests[req] }" 
         end 
         else 
         puts "Requirement #{req} was not tested" 
         end 
        } 
        end 
    end #RequirementList 
    
    ############### 
    ## Here the gem end. The test will come. 
    ############### 
    
    $req = RequirementList.new(1,2,3, 4) 
    
    class MyTest < Test::Unit::TestCase 
        #Following requirements exist, and must be tested sucessfull 
        requirements $req 
    
        def test_1() 
        requirement(1) #this test is testing requirement 1 
        assert_equal(2,1+1) 
        end 
        def test_2() 
        requirement(2) 
        assert_equal(3,1+1) 
        end 
        def test_3() 
        #no assignment to requirement 3 
        pend 'pend' 
        end 
    end 
    
    
    class MyTest_4 < Test::Unit::TestCase 
        #Following requirements exist, and must be tested sucessfull 
        requirements $req 
    
        def test_4() 
        requirement(4) #this test is testing requirement 4 
        assert_equal(2,1+1) 
        end 
    end 
    

    結果:

    Loaded suite testing_traceability_solutions 
    Started 
    .FP. 
    
        1) Failure: 
    test_2(MyTest) 
        [testing_traceability_solutions.rb:89:in `test_2' 
        testing_traceability_solutions.rb:24:in `run_test']: 
    <3> expected but was 
    <2>. 
    
        2) Pending: pend 
    test_3(MyTest) 
    testing_traceability_solutions.rb:92:in `test_3' 
    testing_traceability_solutions.rb:24:in `run_test' 
    
    Finished in 0.65625 seconds. 
    
    4 tests, 3 assertions, 1 failures, 0 errors, 1 pendings, 0 omissions, 0 notifications 
    50% passed 
    Requirements overview: 
    Requirement 1 was tested in MyTest#test_1 
    Requirement 2 was unsuccessfull tested in MyTest#test_2 
    Requirement 3 was not tested 
    Requirement 4 was tested in MyTest_4#test_4 
    

    あなたは、これはあなたのための解決策になる可能性が考えられる場合は、私にフィードバックを与えてください。それから私はそれから宝石を造ろうとします。きれいに見えるが、私はそれが私のために働くだろうわからないshoulda

    #~ require 'test4requirements' ###does not exist/use code above 
    require 'shoulda' 
    #use another interface ##not implemented### 
    #~ $req = Requirement.new_from_file('requirments.txt') 
    
    class MyTest_shoulda < Test::Unit::TestCase 
        #Following requirements exist, and must be tested sucessfull 
        #~ requirements $req 
    
        context 'req. of customer X' do 
        #Add requirement as parameter of should 
        # does not work yet 
        should 'fullfill request 1', requirement: 1 do 
         assert_equal(2,1+1) 
        end 
        #add requirement via requirement command 
        #works already 
        should 'fullfill request 1' do 
         requirement(1) #this test is testing requirement 1 
         assert_equal(2,1+1) 
        end 
        end #context 
    end #MyTest_shoulda 
    
  • +0

    それは良いアイデアのように見えます - 私は別の方法で自分自身を圧延について考えていた。私のreqsは、それぞれ[REQnnnn]に対してタグを持つtxtファイルにあります。次に、各テストで 'should'行にそのタグを追加します(私はshouldaを使用しています)。最後に、単体テストを実行するTestRunnerクラスを作成し、出力を取得し、要件txtファイルとのクロスチェックを行います。インスピレーションをありがとう。 –

    +0

    私は、shouldaで使用するためのコード例を追加しました。あなたは何かそんなことを考えますか? Requirement#new_by_fileは、必要条件付きのテキストファイルを読み込みます。特別なフォーマットはありますか? yaml-fileをお勧めします。 – knut

    +0

    このコードを宝石としてプレリリースすることはhttp://rubygems.org/gems/test4requirementsにあります。 – knut

    1

    、それよりも、よりトレーサブル取得していません:)

    ので、単一の要件は機能であり、機能には、テストするシナリオがあります。

    # addition.feature 
    
    Feature: Addition 
        In order to avoid silly mistakes 
        As a math idiot 
        I want to be told the sum of two numbers 
    
        Scenario Outline: Add two numbers 
        Given I have entered <input_1> into the calculator 
        And I have entered <input_2> into the calculator 
        When I press <button> 
        Then the result should be <output> on the screen 
    
        Examples: 
        | input_1 | input_2 | button | output | 
        | 20  | 30  | add | 50  | 
        | 2  | 5  | add | 7  | 
        | 0  | 40  | add | 40  | 
    

    は、その後、あなたはRubyで書かれたステップの定義は、シナリオにマッピングされている

    # step_definitons/calculator_steps.rb 
    
    begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end 
    require 'cucumber/formatter/unicode' 
    $:.unshift(File.dirname(__FILE__) + '/../../lib') 
    require 'calculator' 
    
    Before do 
        @calc = Calculator.new 
    end 
    
    After do 
    end 
    
    Given /I have entered (\d+) into the calculator/ do |n| 
        @calc.push n.to_i 
    end 
    
    When /I press (\w+)/ do |op| 
        @result = @calc.send op 
    end 
    
    Then /the result should be (.*) on the screen/ do |result| 
        @result.should == result.to_f 
    end 
    
    +0

    での使用のための


    コード例。たとえば、私のテストの1つは、約150要素の3D配列であるメソッドの戻り値をチェックします。キュウリでどうすればいいですか? (私はすでにテスト/ユニットでそれを書いています) –

    関連する問題