2012-10-17 63 views
3

我正在使用Ruby 1.9.3和Rails 3.2.8。這使用rake 0.9.2.2。如何使用rake檢測失敗的單元測試

我想檢測一個失敗的單元測試,在Rails 2.3中,我們通過檢查退出代碼來做到這一點。這不再有效。

我嘗試:

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

failing_test.rb由一個單一的測試,斷言虛假的。我希望看到一個測試失敗,並沒有看到「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 

具體而言,我請參閱「命令失敗,狀態(1)」,但這似乎通過給吃掉了耙,因爲我也看到「OK」。請注意,如果我echo $?,我看到退出代碼是0(成功)。我試圖在持續集成和我們的發佈腳本的背景下工作,這兩種方式都假設退出代碼在發生錯誤時將不爲零,就像Rails 2.3中發生的那樣。

+0

注意,有在Rails的本身類似的bug八個月前,看到https://github.com/rails/rails/issues/4923然而,這是固定的,我驗證了承諾修復遺體在3.2.8中。 – ChrisInEdmonton

+0

無法用ruby 1.9.3-p194,rails 3.2.8,rake 0.9.2.2(以及shoulda-context 1.0.0)重現。你可以發佈你的'gem list'輸出,也許你的測試文件的正文? – Araxia

回答

3

我的失敗測試使用相同的rails/ruby​​/rake正在退出1與您的不同。也許你在你的代碼庫中有一些地方叫at_exit { exit 0 }trap('EXIT') { exit 0 }。這將修改耙子的退出碼。

如果你想要的,檢測故障,你可以嘗試使用閱讀的grep測試的輸出非Ruby的方式。 grep如果找到匹配則返回0,否則返回1。

正則表達式查找" 0 failures, 0 errors",如果發現則返回0,如果沒有則返回1

rake test:units TEST=test/unit/failing_test.rb RAILS_ENV=test | \ 
    tee >(xargs -0 echo {}) | \ 
    grep '\([[:space:]]0[[:space:]]\)failures,\1errors' && 
    echo OK 
+0

謝謝,lastcanal(和阿拉夏,上面)。這真的很有趣,明天我將不得不做一個小例子,看看究竟發生了什麼。我討厭我們的寶石或插件之一,因爲這些,但這正是標誌指向的地方。 +100代表在我有機會之前可能會過期,但如果準確,我會看看我能否在代表到期後授予代表。 – ChrisInEdmonton

+0

這確實是個問題,儘管at_exit塊沒有顯式返回任何東西。我修改了你的答案,以展示如何解決這個問題。 – ChrisInEdmonton

+0

是從寶石中調用的at_exit?瞭解哪些寶石會讓這件事變得糟糕透徹。 – gabeodess