Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

RSpec | rspec command | exit status

概要

rspec command | exit status

詳細

rspec commandによるテスト実行時に
全テストが成功ならexit statusは0
そうでなければexit statusは1
になります。

サンプルコード1(hoge_spec.rb)
# encoding: utf-8
require "spec_helper"
require "hoge"

describe Hoge do
  it "hoge1 1" do
    # some spec
  end

  it "hoge1 2" do
    # some spec
  end

  it "hoge2" do
    # some spec
  end
end
実行例:全件成功時
$ rspec -fs
Run options: include {:focus=>true}

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

Hoge
  hoge1 1
  hoge1 2
  hoge2

Finished in 0.001 seconds
3 examples, 0 failures

$ echo $?
0
実行例:1件失敗時

一部コードをエラーになるように修正
hoge_spec.rb

# 略
  it "hoge1 1" do
    # some spec
    raise "NG"
  end
# 略
$ rspec -fs
Run options: include {:focus=>true}

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

Hoge
  hoge1 1 (FAILED - 1)
  hoge1 2
  hoge2

Failures:

  1) Hoge hoge1 1
     Failure/Error: raise "NG"
     RuntimeError:
       NG
     # ./spec/hoge_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds
3 examples, 1 failure

Failed examples:

rspec ./spec/hoge_spec.rb:6 # Hoge hoge1 1

$ echo $?
1
実行例:1件失敗かつ任意のエラーコードを返却したい場合
$ rspec -fs --failure-exit-code 2
Run options: include {:focus=>true}

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

Hoge
  hoge1 1 (FAILED - 1)
  hoge1 2
  hoge2

Failures:

  1) Hoge hoge1 1
     Failure/Error: raise "NG"
     RuntimeError:
       NG
     # ./spec/hoge_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds
3 examples, 1 failure

Failed examples:

rspec ./spec/hoge_spec.rb:6 # Hoge hoge1 1
$ echo $?
2