PHPUnit & Selenium - 更新到 3.6.10 版本后,似乎 markTestSkipped 和 markTestIncomplete 调用导致测试出错

标签 php selenium phpunit

在 PHPUnit 3.6.4 中,当我运行基于 PHPUnit_Extensions_SeleniumTestCase 的测试时,我使用

$this->markTestSkipped();

$this->markTestIncomplete();

我跳过了测试 (S) 或标记为不完整 (I)。

但是在将 PHPUnit 更新到 3.6.10(现在最新的)之后,这些函数似乎通过产生错误而不是跳过它而导致测试失败。

更多示例,请进行此测试:

class ExampleTest extends PHPUnit_Extensions_SeleniumTestCase
{
    public function testMyCase()
    {
        $this->markTestIncomplete();
    }
}  

如果您没有运行 Selenium 服务器,它仍然会运行测试并给出以下输出:

PHPUnit 3.6.10 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 6.25Mb

There was 1 error:

1) ExampleTest::testMyCase
RuntimeException: 

/usr/bin/phpunit:46

FAILURES!
Tests: 1, Assertions: 0, Errors: 1

如果您确实运行了 Selenium 服务器,您的结果会略有不同,但仍然会出现错误。这仅适用于 Selenium 测试,扩展 PHPUnit_Framework_TestCase 的测试似乎没问题。要确认这一点,请将您要扩展的类更改为 PHPUnit_Framework_TestCase:

class ExampleTest extends PHPUnit_Framework_TestCase
{
    public function testMyCase()
    {
        $this->markTestIncomplete();
    }
}  

你会得到这样的结果:

PHPUnit 3.6.10 by Sebastian Bergmann.

I

Time: 0 seconds, Memory: 5.25Mb

OK, but incomplete or skipped tests!
Tests: 1, Assertions: 0, Incomplete: 1.

所以我的问题是:这是 PHPUnit 3.6.10 中的错误吗?这是一个我不知道但我做错了什么的很酷的功能吗?

最佳答案

我遇到了同样的问题,看起来这个问题与 PHPUnit 的核心无关,而是与 PHPUnit Selenium 扩展有关。 A ticket has already been filed in their issue tracking system

我找到了引发错误的确切行,并在 Extensions/SeleniumTestcase.php on line 1215 中找到了它。 :

1213: // gain the screenshot path, lose the stack trace
1214: if ($this->captureScreenshotOnFailure) {
1215:    throw new PHPUnit_Framework_Error($buffer, $e->getCode(), $e->getFile(), $e->getLine(), $e->getTrace());
1216: }

如您所见,仅当 $this->captureScreenshotOnFailure 设置为 true 时才会发生此错误。因此,我当前使用的解决方法是在 Selenium 测试的 setUp() 方法以及标记为已跳过的每个测试中设置 $this->captureScreenshotOnFailure=true我在调用skip方法之前手动禁用屏幕截图:

public function setUp() {
    [...]
    $this->captureScreenshotOnFailure = true;
}

/**
 * @test
 */
public function mySkippedTest() {
    $this->captureScreenshotOnFailure = false; 
    $this->markTestSkipped(); 
    [...]
}

这对我有用,因为它正确地标记了跳过的 Selenium 测试,但仍然保留了所有其他测试的屏幕截图功能。

但是,如果您的项目中有大量跳过的测试,这可能会有点乏味,而且我不能保证没有任何其他副作用,因为我不太熟悉PHPUnit 的内部结构。在这种情况下,最好的解决方案可能是等待更新,直到 phpunit-selenium 的创建者修复它,因为他们似乎已经意识到了这个问题。

关于PHPUnit & Selenium - 更新到 3.6.10 版本后,似乎 markTestSkipped 和 markTestIncomplete 调用导致测试出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9467063/

相关文章:

php - 将每个数据库行插入不同的表行中?

python - selenium.common.exceptions.WebDriverException : Message: connection refused

javascript - Selenium:无法在 GWT 应用程序中找到元素

python - 使用selenium(python)抓取网页

php - Laravel 测试服务依赖注入(inject)错误

php - 我需要在准备好的语句中使用 htmlentities() 或 htmlspecialchars() 吗?

php - 在 codeigniter 中单击 ajax 上的标签 a 时无法加载 Controller 功能

php - UTF-8 编码在 PHP 中不起作用

php - 单元测试——基本目标?

php - 如何从 Windows 命令提示符在 Laravel 中运行 PHPUnit