2012-02-10 103 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