Tbpgr Blog

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

RSpec | rspec command | format option

概要

rspec command | format option

詳細

rspec commandのformat optionで出力フォーマットを制御できます。

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

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

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

  it "hoge1 2" do
    # some spec
  end

  it "hoge2" do
    # some spec
  end
end

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

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

describe Hige do
  it "hige1 1" do
    # some spec
  end

  it "hige1 2" do
    # some spec
  end

  it "hige2" do
    # some spec
  end
end

rspec formatを試す

ドキュメントフォーマット rspec -fs

ドキュメントフォーマット

$ rspec -fs または rspec -fd
Run options: include {:focus=>true}

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

Hige
  hige1 1
  hige1 2
  hige2

Hoge
  hoge1 1
  hoge1 2
  hoge2

Finished in 0.00101 seconds
6 examples, 0 failures
進捗率フォーマット rspec -fp

進捗率フォーマット
各テスト結果が ".F*" のフォーマットで表示されます

$ rspec --format progress
Run options: include {:focus=>true}

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

Finished in 0.00101 seconds
6 examples, 0 failures
$
HTMLフォーマット rspec -fh

HTMLフォーマット

$ rspec -fh > result.html

HTMLの結果をブラウザで確認します。
フィルタ用のチェックボックスがあるので、成功ケース・失敗ケースだけ
確認することも可能です。


JSONフォーマット rspec -fj

JSONフォーマット。
テスト結果を二次加工したり、
結果をトリガーに別の処理をしたりするのに使えそうですね。

rspec -fj > result.json

result.json

{"messages":["Run options: include {:focus=>true}","\nAll examples were filtered out; ignoring {:focus=>true}"],"examples":[{"description":"hige1 1","full_description":"Hige hige1 1","status":"passed","file_path":"./spec/hige_spec.rb","line_number":6},{"description":"hige1 2","full_description":"Hige hige1 2","status":"passed","file_path":"./spec/hige_spec.rb","line_number":10},{"description":"hige2","full_description":"Hige hige2","status":"passed","file_path":"./spec/hige_spec.rb","line_number":14},{"description":"hoge1 1","full_description":"Hoge hoge1 1","status":"passed","file_path":"./spec/hoge_spec.rb","line_number":6},{"description":"hoge1 2","full_description":"Hoge hoge1 2","status":"passed","file_path":"./spec/hoge_spec.rb","line_number":10},{"description":"hoge2","full_description":"Hoge hoge2","status":"passed","file_path":"./spec/hoge_spec.rb","line_number":14}],"summary":{"duration":0.0,"example_count":6,"failure_count":0,"pending_count":0},"summary_line":"6 examples, 0 failures"}

上記のJSONRubyでparseしてみる

# encoding: utf-8
require 'json'
require 'pp'

str = '{"messages":["Run options: include {:focus=>true}","\nAll examples were filtered out; ignoring {:focus=>true}"],"examples":[{"description":"hige1 1","full_description":"Hige hige1 1","status":"passed","file_path":"./spec/hige_spec.rb","line_number":6},{"description":"hige1 2","full_description":"Hige hige1 2","status":"passed","file_path":"./spec/hige_spec.rb","line_number":10},{"description":"hige2","full_description":"Hige hige2","status":"passed","file_path":"./spec/hige_spec.rb","line_number":14},{"description":"hoge1 1","full_description":"Hoge hoge1 1","status":"passed","file_path":"./spec/hoge_spec.rb","line_number":6},{"description":"hoge1 2","full_description":"Hoge hoge1 2","status":"passed","file_path":"./spec/hoge_spec.rb","line_number":10},{"description":"hoge2","full_description":"Hoge hoge2","status":"passed","file_path":"./spec/hoge_spec.rb","line_number":14}],"summary":{"duration":0.0,"example_count":6,"failure_count":0,"pending_count":0},"summary_line":"6 examples, 0 failures"}'

rspec_result = JSON.parse str
pp rspec_result

出力

{"messages"=>
  ["Run options: include {:focus=>true}",
   "\nAll examples were filtered out; ignoring {:focus=>true}"],
 "examples"=>
  [{"description"=>"hige1 1",
    "full_description"=>"Hige hige1 1",
    "status"=>"passed",
    "file_path"=>"./spec/hige_spec.rb",
    "line_number"=>6},
   {"description"=>"hige1 2",
    "full_description"=>"Hige hige1 2",
    "status"=>"passed",
    "file_path"=>"./spec/hige_spec.rb",
    "line_number"=>10},
   {"description"=>"hige2",
    "full_description"=>"Hige hige2",
    "status"=>"passed",
    "file_path"=>"./spec/hige_spec.rb",
    "line_number"=>14},
   {"description"=>"hoge1 1",
    "full_description"=>"Hoge hoge1 1",
    "status"=>"passed",
    "file_path"=>"./spec/hoge_spec.rb",
    "line_number"=>6},
   {"description"=>"hoge1 2",
    "full_description"=>"Hoge hoge1 2",
    "status"=>"passed",
    "file_path"=>"./spec/hoge_spec.rb",
    "line_number"=>10},
   {"description"=>"hoge2",
    "full_description"=>"Hoge hoge2",
    "status"=>"passed",
    "file_path"=>"./spec/hoge_spec.rb",
    "line_number"=>14}],
 "summary"=>
  {"duration"=>0.0,
   "example_count"=>6,
   "failure_count"=>0,
   "pending_count"=>0},
 "summary_line"=>"6 examples, 0 failures"}