Tbpgr Blog

元エンジニア 人事 tbpgr(てぃーびー) のブログ

RSpec | rspec command | fail fast option

概要

rspec command | fail fast option

詳細

rspec commandのfail fast optionでテスト失敗時に即実行を終了できます。

rspec --fail-fast

サンプルコード(hoge.rb)

100メソッド定義

# encoding: utf-8

class Hoge
  (1..100).each do |i|
    define_method "hoge#{i.to_s}" do
      "hoge#{i.to_s}"
    end
  end
end

サンプルテストコード(hige_spec.rb)

100メソッドテスト

# encoding: utf-8
require "spec_helper"
require "hoge"

describe Hoge do
  (1..100).each do |i|
    it "hoge #{i.to_s}", :hoge1 => "hoge", hoge: true do
      ret = Hige.new.send "hoge{i.to_s}"
      expect(Array.new([1,2,3]).length).to eq "hoge{i.to_s}"
    end
  end
end

fail fast optionを試す

fail fastを指定しなかった場合

Finished in 0.03415 seconds

Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) Hoge hoge 1
     Failure/Error: ret = Hige.new.send "hoge{i.to_s}"
     NameError:
       uninitialized constant Hige
     # ./spec/hoge_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) Hoge hoge 2
     Failure/Error: ret = Hige.new.send "hoge{i.to_s}"
     NameError:
       uninitialized constant Hige
     # ./spec/hoge_spec.rb:8:in `block (3 levels) in <top (required)>'

# 中略

  100) Hoge hoge 100
     Failure/Error: ret = Hige.new.send "hoge{i.to_s}"
     NameError:
       uninitialized constant Hige
     # ./spec/hoge_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.03415 seconds
100 examples, 100 failures

Failed examples:

rspec ./spec/hoge_spec.rb:7 # Hoge hoge 1
rspec ./spec/hoge_spec.rb:7 # Hoge hoge 2
# 中略
rspec ./spec/hoge_spec.rb:7 # Hoge hoge 100
fail fastを指定した場合

実行時間がFinished in 0.001 secondsになった

$ rspec --fail-fast
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
F

Failures:

  1) Hoge hoge 1
     Failure/Error: ret = Hige.new.send "hoge{i.to_s}"
     NameError:
       uninitialized constant Hige
     # ./spec/hoge_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.001 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/hoge_spec.rb:7 # Hoge hoge 1