3

Ruby 1.9.3 と Rails 3.2.8 を使用しています。これはレーキ 0.9.2.2 を使用します。

失敗した単体テストを検出したいのですが、Rails 2.3 では、終了コードをチェックすることでこれを行いました。これは機能しなくなりました。

私は試します:

rake test:units TEST=test/unit/failing_test.rb RAILS_ENV=test && echo "OK"

failing_test.rb は、false をアサートする単一のテストで構成されています。テストの失敗が表示され、「OK」が表示されないことを期待していますが、代わりに次のように表示されます。

Started
F
===============================================================================
Failure: <false> is not true.
test: failing test case should fail. (FailingTest)
test/unit/failing_test.rb:6:in `block (2 levels) in <class:FailingTest>'
shoulda-context (1.0.0) lib/shoulda/context/context.rb:398:in `call'
shoulda-context (1.0.0) lib/shoulda/context/context.rb:398:in `block in create_test_from_should_hash'
activesupport (3.2.8) lib/active_support/testing/setup_and_teardown.rb:72:in `block in run'
activesupport (3.2.8) lib/active_support/callbacks.rb:425:in `_run__3774233495015181374__setup__985181848351195933__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
activesupport (3.2.8) lib/active_support/testing/setup_and_teardown.rb:70:in `run'
===============================================================================


Finished in 0.002753 seconds.

1 tests, 1 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

363.24 tests/s, 363.24 assertions/s
rake aborted!
Command failed with status (1): [/Users/chris/.rvm/rubies/ruby-1.9.3-p194/b...]

Tasks: TOP => test:units
(See full trace by running task with --trace)
OK

具体的には「Command failed with status (1)」と表示されますが、これも「OK」と表示されるのでレーキに食い込まれそうです。echo $?実行すると、終了コードが 0 (成功) になることに注意してください。Rails 2.3で発生したように、エラーが発生した場合、終了コードがゼロ以外になると想定しています。

4

1 に答える 1

3

同じ rails/ruby/rake を使用した私の失敗したテストは、1あなたのものとは異なり終了しています。おそらく、あなたのコードベースのどこかにat_exit { exit 0 }呼び出しがあるか、どこかにあるでしょう。trap('EXIT') { exit 0 }これにより、rake の終了コードが変更されます。

障害を検出する Ruby 以外の方法が必要な場合は、 を使用してテストの出力を読み取ってみてくださいgrepgrep一致した場合は 0 を返し、一致しなかった場合は 1 を返します。

正規表現は を探し、見つかった場合" 0 failures, 0 errors"は返し、見つからなかった場合は返します。01

rake test:units TEST=test/unit/failing_test.rb RAILS_ENV=test | \
  tee >(xargs -0 echo {}) | \ 
  grep '\([[:space:]]0[[:space:]]\)failures,\1errors' && 
  echo OK
于 2012-10-29T22:53:32.293 に答える