Rspec-filtering

提供:Dev Guides
移動先:案内検索

RSpec-フィルタリング

RSpecフィルタリングはRSpecメタデータに基づいているため、このセクションを読む前にRSpecメタデータのセクションを読むことをお勧めします。

スペックファイルがあり、2つのタイプのテスト(例)が含まれていると想像してください:ポジティブ機能テストとネガティブ(エラー)テスト。 このように定義しましょう-

RSpec.describe "An Example Group with positive and negative Examples" do
   context 'when testing Ruby\'s build-in math library' do

      it 'can do normal numeric operations' do
         expect(1 + 1).to eq(2)
      end

      it 'generates an error when expected' do
         expect{1/0}.to raise_error(ZeroDivisionError)
      end

   end
end

さて、上記のテキストを「filter_spec.rb」というファイルとして保存し、このコマンドで実行します-

rspec filter_spec.rb

次のような出力が表示されます-

..
Finished in 0.003 seconds (files took 0.11201 seconds to load)
2 examples, 0 failures

ここで、このファイルの肯定的なテストのみを再実行したい場合はどうでしょうか? または、負のテストのみですか? これは、RSpec Filtersで簡単に行えます。 上記のコードをこれに変更します-

RSpec.describe "An Example Group with positive and negative Examples" do
   context 'when testing Ruby\'s build-in math library' do

      it 'can do normal numeric operations', positive: true do
         expect(1 + 1).to eq(2)
      end

      it 'generates an error when expected', negative: true do
         expect{1/0}.to raise_error(ZeroDivisionError)
      end

   end
end

変更をfilter_spec.rbに保存し、このわずかに異なるコマンドを実行します-

rspec --tag positive filter_spec.rb

これで、次のような出力が表示されます-

Run options: include {:positive=>true}
.
Finished in 0.001 seconds (files took 0.11401 seconds to load)
1 example, 0 failures

--tag positiveを指定することで、RSpecに、ポジティブメタデータ変数が定義されたサンプルのみを実行するように指示しています。 このようなコマンドを実行することで、負のテストでも同じことができます-

rspec --tag negative filter_spec.rb

これらは単なる例であり、任意の名前でフィルターを指定できることに注意してください。

RSpecフォーマッター

フォーマッタにより、RSpecはさまざまな方法でテストからの出力を表示できます。 このコードを含む新しいRSpecファイルを作成しましょう-

RSpec.describe "A spec file to demonstrate how RSpec Formatters work" do
   context 'when running some tests' do

      it 'the test usually calls the expect() method at least once' do
         expect(1 + 1).to eq(2)
      end

   end
end

さて、これをformatter_spec.rbというファイルに保存し、このRSpecコマンドを実行します-

rspec formatter_spec.rb

次のような出力が表示されるはずです-

.
Finished in 0.002 seconds (files took 0.11401 seconds to load)
1 example, 0 failures

今、同じコマンドを実行しますが、今回はこのようにフォーマッタを指定します-

rspec --format progress formatter_spec.rb

今回は同じ出力が表示されるはずです-

.
Finished in 0.002 seconds (files took 0.11401 seconds to load)
1 example, 0 failures

その理由は、「進行中」フォーマッターがデフォルトのフォーマッターだからです。 次に別のフォーマッタを試して、このコマンドを実行してみてください-

rspec --format doc formatter_spec.rb

今、あなたはこの出力が表示されるはずです-

A spec file to demonstrate how RSpec Formatters work
   when running some tests
      the test usually calls the expect() method at least once
Finished in 0.002 seconds (files took 0.11401 seconds to load)
1 example, 0 failures

ご覧のとおり、出力は「doc」フォーマッタとは大きく異なります。 このフォーマッタは、ドキュメントのようなスタイルで出力を提示します。 テストに失敗したときに、これらのオプションがどのように見えるか疑問に思われるかもしれません(例)。 formatter_spec.rb のコードを次のように変更してみましょう-

RSpec.describe "A spec file to demonstrate how RSpec Formatters work" do
   context 'when running some tests' do

      it 'the test usually calls the expect() method at least once' do
         expect(1 + 1).to eq(1)
      end

   end
end

期待値* expect(1 + 1).to eq(1)*は失敗します。 変更を保存し、上記のコマンドを再実行します-

*rspec --format progress formatter_spec.rb* を覚えておいてください。「プログレス」フォーマッターがデフォルトであるため、実行できるのは *rspec formatter_spec.rb* です。 この出力が表示されるはずです-
F
Failures:
1) A spec file to demonstrate how RSpec Formatters work when running some tests
the test usually calls the expect() method at least once
   Failure/Error: expect(1 + 1).to eq(1)

      expected: 1
         got: 2

      (compared using ==)
   # ./formatter_spec.rb:4:in `block (3 levels) in <top (required)>'

Finished in 0.016 seconds (files took 0.11201 seconds to load)
1 example, 1 failure
Failed examples:

rspec ./formatter_spec.rb:3 # A spec file to demonstrate how RSpec
   Formatters work when running some tests the test usually calls
   the expect() method at least once

さて、ドキュメントフォーマッタを試して、このコマンドを実行してください-

rspec --format doc formatter_spec.rb

さて、失敗したテストでは、この出力が表示されるはずです-

A spec file to demonstrate how RSpec Formatters work
   when running some tests
      the test usually calls the expect() method at least once (FAILED - 1)

Failures:

1) A spec file to demonstrate how RSpec Formatters work when running some
   tests the test usually calls the expect() method at least once
   Failure/Error: expect(1 + 1).to eq(1)

   expected: 1
        got: 2

   (compared using ==)
   # ./formatter_spec.rb:4:in `block (3 levels) in <top (required)>'

Finished in 0.015 seconds (files took 0.11401 seconds to load)
1 example, 1 failure

失敗した例

rspec ./formatter_spec.rb:3#いくつかのテストを実行するときにRSpec Formatterがどのように機能するかを示すための仕様ファイル。テストは通常​​、expect()メソッドを少なくとも1回呼び出します。

RSpec Formatterは、テスト結果の表示方法を変更する機能を提供します。独自のカスタムFormatterを作成することも可能ですが、それはより高度なトピックです。