Thanks Driven Life

日々是感謝

parallel_rspec (parallel_tests) が turnip の feature を認識してくれないと嘆いてる皆へ

turnip-parallel_tests を使ってみてください

Story

ということなので、parallel_rspec で turnip によるテストコードを並列実行したい!と思ってやってみると

$ git clone https://github.com/gongo/turnip_formatter.git
$ cd turnip_formatter/
$ echo "gem 'parallel_tests'" >> Gemfile
$ bundle install --path vendor/bundle
$ cd example/
$ bundle exec parallel_rspec -n 2 spec
2 processes for 0 specs, ~ 0 specs per process



Took 0.008955 seconds

0 spec という悲しい文字が。なんでや。 ちなみに省略しないで feature を指定するといける

$ bundle exec parallel_rspec -n 2 spec/features/{battle3,songs}.feature
2 processes for 2 specs, ~ 1 specs per process
.*

Pending:
  A feature with multiline strings This is a feature with multiline strings a monster sings the following song -> the song should have 2 lines
    # No such step(0): 'a monster sings the following song'
    # ./spec/features/songs.feature:13

Finished in 0.00255 seconds
2 examples, 0 failures, 1 pending
...

Finished in 0.00727 seconds
3 examples, 0 failures

5 examples, 0 failures, 1 pending

Took 1.421022 seconds

Why

なんで feature ファイルを見てくれないのかと言うと parallel_tests/test/runner.rb に秘密があります。

このコードでは、テストディレクトリ以下にある /#{Regexp.escape test_suffix}$/ に マッチするファイル名をもつテストファイルだけ実行する、みたいな感じになっています。 この test_suffix ってのが何かというと、parallel_tests/rspec/runner.rb の通り _spec.rb です。

つまり turnip でお決まりの .feature がテスト対象にならないから、 0 specs なんていう悲しい結果になったわけです。 あと、parallel_tests には --pattern regexp っていうオプションがあって、これ使えば --pattern .feature みたいなイメージで feature ファイル実行してくれるんじゃないの?って思ってた時期が俺にもありました。 先程の test/runner.rb 見てると、--pattern オプションは test_suffix で選抜されたファイル一覧にしか効果を発揮しないので そもそも feature ファイルは足切りされてしまっているわけです。悲しい。

まあそんなわけで、 zsh とかだと

$ bundle exec parallel_rspec spec/**/*.feature

とかやれば実行してくれるんですが、正直めんどい。というわけで

gongo/turnip-parallel_tests · GitHub

です。

What

コード見ていただければわかるんですが、単に _spec.rb.feature をテスト対象にしているだけです。楽い。

補足

turnip_formatterparallel_tests を組み合わせた時のレポートの出し方なんですが、 始めはどうにかして一つにまとめられないかなーと思ったんですが、めんどくさかったので .rspec_parallel とかにでも

-f RSpecTurnipFormatter -o report<%= ENV['TEST_ENV_NUMBER'] %>.html

とか書くといいと思います。-n 3 で実行してたら report.html report2.html report3.html が生成されます