[Cucumber] 如何用 rakefile 重跑 Cucumber Fail 的 Features ???

凌晨12:42

[Cucumber] 如何用 rakefile 重跑 Cucumber Fail 的 Features ???




這週被指派 一個任務, 希望能將 Cucumber Fail 的 features 重跑, 但是要怎麼做到呢??? 後來想了一下 Ruby 其實有一個強大的功能 rakefile ,  它其實像是 c 的 make 和 java 的 ant 一樣, 是用來編譯代碼的. 但奇怪的是, Ruby 根本不需要編譯, 為什麼還需要 Rake ?  與其說 Rake 是一個代碼構建工具,不如說 Rake 是一個任務管理工具. 更詳細的說明可以參考這篇 : [Ruby] Ruby的頭號Gem:Rake



此外, 若去查 cucumber -h , 你會發現其實 Cucumber 已經有支援 rerun 的功能 :

Use --format rerun --out rerun.txt to write out failing features. You can rerun them with cucumber @rerun.txt. 

有了這些概念後, 我們可以來實作一個 rakefile , 裡面包含兩個 task :


  • :run 用來執行任務
  • :rerun 用來重跑任務



require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'

namespace :test do

    desc 'ExecuteTestCase'
    task :execute do
        Cucumber::Rake::Task.new(:run) do |t|
            t.cucumber_opts = "-f pretty --format rerun --out rerun.txt"
        end

        is_first_run_successful = run_rake_task ("run")

        if not is_first_run_successful
            Cucumber::Rake::Task.new(:rerun) do |t|
                t.cucumber_opts = "@rerun.txt"
            end

            run_rake_task("rerun")
        end
    end

    def run_rake_task(name)
        begin
            Rake::Task[name].invoke
        rescue Exception => e
            puts e
            return false
        end
        true
    end
end



    完成後我們就來執行 :


    $ rake test:execute


    當第一個任務失敗之後就會啟動 rerun ,因此就能夠輕易的需求了.

    至於 Cucumber::Rake::Task 是做什麼用的, 就下回分曉啦... Yeah :)



    [Reference]


    1. RAKE

    2. [Ruby] Ruby的頭號Gem:Rake

    3. Auto Retry Failed Cucumber Tests

    You Might Also Like

    0 意見