2012-02-10 54 views
4

我有Selenium服務器在MAMP本地服務器上使用PHPUnit。Selenium不顯示失敗的數字行

當一個斷言失敗,失敗的號碼行不會顯示,而是我看到一個phpunit號碼行。

當我執行「phpunit only」測試時,我可以看到失敗斷言的數字行。

PHPUnit的唯一的測試

$ cd '/Applications/MAMP/htdocs/my-client/tests' && phpunit -c 'phpunit.xml' '/Applications/MAMP/htdocs/my-client/tests/controllers/homeTest.php' 
PHPUnit 3.6.10 by Sebastian Bergmann. 

Configuration read from /Applications/MAMP/htdocs/my-client/tests/phpunit.xml 

. 

Time: 0 seconds, Memory: 8.00Mb 

There was 1 failure: 

1) HomeTest::test_get_sections 
Failed asserting that Array (
    blah, blah, blah 
    ) 
) is identical to Array (blah, blah, blah2 
    ) 
). 

/Applications/MAMP/htdocs/my-client/tests/controllers/homeTest.php:56 
/Applications/MAMP/bin/php/php5.3.6/bin/phpunit:46 

FAILURES! 
Tests: 2, Assertions: 3, Failures: 1. 

PHPUnit的Selenium測試

$ cd '/Applications/MAMP/htdocs/my-client/tests' && phpunit -c 'phpunit.xml' 

'/Applications/MAMP/htdocs/my-client/tests/views/af_web_Test.php' 
PHPUnit 3.6.10 by Sebastian Bergmann. 

Configuration read from /Applications/MAMP/htdocs/my-client/tests/phpunit.xml 

E 

Time: 2 seconds, Memory: 8.75Mb 

There was 1 error: 

1) af_web_Test::test_crear_una_af_nueva_y_validar_el_valor_por_defecto_de_los_campos 
Current URL: http://localhost:8888/my-client/index.php/home/view 

Failed asserting that '' matches PCRE pattern "/0/". 

/Applications/MAMP/bin/php/php5.3.6/bin/phpunit:46 

FAILURES! 
Tests: 1, Assertions: 6, Errors: 1. 
+1

我建議你關閉顏色,這樣不顯示ANSI代碼(不使用'--colors'),見http://www.phpunit.de/manual/3.6/en/textui.h​​tml - 這裏不是你的問題,只是說,可能會幫助你調試。 – hakre 2012-02-10 12:11:05

回答

4

我有同樣的問題(雖然是一個LAMP服務器上),因爲我很依賴這些行號與沿截圖弄清楚我的測試中究竟出了什麼問題。這個錯誤,我認爲這是顯而易見的報道。請參閱https://github.com/sebastianbergmann/phpunit-selenium/issues/81以供參考。

作爲臨時解決方法我強制行號進入正在拋出的異常中的錯誤消息(因爲行號當然可以在跟蹤中找到)。作爲一個副作用,大多數異常被重新拋出,我只拋出PHPUnit_Framework_Error,但至少我得到了輸出中的行號。作爲一個臨時的解決方法,直到解決這個問題,這對我來說很有用。

要做到這一點,我向PHPUnit_Extensions_SeleniumTestCase的我自己SeleniumTestCase,並把這些功能吧:

非常稍加修改該功能的版本爲我所用:https://stackoverflow.com/a/6608751

protected function dumpStack(Exception $e) { 
    $stack = ''; 
    foreach ($e->getTrace() as $trace) { 
     if (isset($trace['file'])  && 
      isset($trace['line'])  && 
      isset($trace['class'])  && 
      isset($trace['function'])) 
     { 
      $stack .= PHP_EOL . 
       $trace['file']  . ':' . 
       $trace['line']  . ' ' . 
       $trace['class'] . '::' . 
       $trace['function']; 
     } 
    } 
    return $stack; 
} 

我覆蓋onNotSuccessfulTest from PHPUnit_Extensions_SeleniumTestCase

protected function onNotSuccessfulTest(Exception $e) { 
    try { 
     parent::onNotSuccessfulTest($e); 
    } 
    catch (PHPUnit_Framework_IncompleteTestError $e) { 
     // Don't do anything with the incomplete test exception 
     throw $e; 
    } 
    catch (PHPUnit_Framework_SkippedTestError $e) { 
     // Don't do anything with the skipped test exception 
     throw $e; 
    } 
    catch (Exception $e_parent) { 
     // Include line number for specific test file in error 
     $error_msg = chr(10).chr(10).$this->dumpStack($e); 

     throw new PHPUnit_Framework_Error($e_parent->getMessage().$error_msg, $e_parent->getCode(), $e_parent->getFile(), $e_parent->getLine(), $e_parent->getTrace()); 
    } 
} 

我不喜歡這個黑客,所以如果任何人有更好的解決方案,我會很高興聽到它!

更新: 我忘了最初提到這一點:當然,你必須讓測試延長自己的自定義SeleniumTestCase,而不是PHPUnit_Extensions_SeleniumTestCase的。

+0

驚人的「黑客」;-)謝謝! – rubdottocom 2012-04-26 17:04:14

相關問題