问题:Selenium 不显示失败的数字行

我有 Selenium 服务器在 MAMP 本地服务器上与 PHPUnit 一起工作。

当断言失败时,失败的数字行没有显示,而是我看到一个 phpunit 数字行。

当我执行“仅 phpunit”测试时,我可以看到失败断言的数字行。

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.

解答

我遇到了同样的问题(尽管是在 LAMP 服务器上),因为我非常依赖这些行号以及屏幕截图来确定我的测试中到底出了什么问题。我认为这是错误的错误显然已被报告。请参阅https://github.com/sebastianbergmann/phpunit-selenium/issues/81以供参考。

作为 temporary 解决方法,我已将行号强制放入正在抛出的异常中的错误消息中(因为当然可以在跟踪中找到行号)。作为副作用,大多数异常都被重新抛出,我只抛出 PHPUnit_Framework_Error,但至少我得到了输出中的行号。作为解决此问题之前的临时解决方法,这对我有用。

为此,我使用自己的 SeleniumTestCase 扩展了 PHPUnit_Extensions_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;
}

我从 PHPUnit_Extensions_SeleniumTestCase 覆盖 onNotSuccessfulTest:

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());
     }
}

我不喜欢这个 hack,所以如果有人有更好的解决方案,我很高兴听到它!

更新:我最初忘记提及这一点:您当然必须让测试扩展您自己的自定义 SeleniumTestCase,而不是 PHPUnit_Extensions_SeleniumTestCase。

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐