【问题标题】:Does PHPUnit_Selenium Code Coverage Work?PHPUnit_Selenium 代码覆盖是否有效?
【发布时间】:2012-07-01 12:01:36
【问题描述】:

In the PHPUnit docs,表示可以获取代码覆盖率数据:

PHPUnit_Extensions_SeleniumTestCase 可以为通过 Selenium 运行的测试收集代码覆盖率信息:

  1. 将 PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php 复制到您的网络服务器的文档根目录中。

  2. 在您的网络服务器的 php.ini 配置文件中,将 PHPUnit/Extensions/SeleniumTestCase/prepend.php 和 PHPUnit/Extensions/SeleniumTestCase/append.php 分别配置为 auto_prepend_file 和 auto_append_file。

  3. 在扩展 PHPUnit_Extensions_SeleniumTestCase 的测试用例类中,使用 protected $coverageScriptUrl = 'http://host/phpunit_coverage.php'; 配置 phpunit_coverage.php 脚本的 URL。

我无法让它输出任何覆盖信息。我可以通过正常的单元测试获得代码覆盖率信息。

对于在http://localhost/ts2_templates/ 运行的应用,我已将phpunit_coverage.php 复制到http://localhost/phpunit_coverage.php

我在 php.ini 中添加了以下内容:

auto_prepend_file = "/path/to/pear/share/pear/PHPUnit/Extensions/SeleniumTestCase/prepend.php"
auto_append_file = "/path/to/pear/share/pear/PHPUnit/Extensions/SeleniumTestCase/append.php"

...并验证了他们是用die("yep it's me"); 调用的。

最后,我在我的测试用例中添加了以下内容:

<?php

class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
    # added line below
    protected $coverageScriptUrl = 'http://localhost/phpunit_coverage.php';

    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost/ts2_templates');
    }

    public function testTitle()
    {
        $this->url('http://localhost/ts2_templates');
        $this->assertContains('test', $this->title());
    }
}

?>

这是运行测试的命令,由 PHPStorm 生成,代码覆盖率:

/Applications/MAMP/bin/php5.3/bin/php -dxdebug.coverage_enable=1 /private/var/folders/pp/0t4y41f95j5313qm_f8b42fw0000gn/T/ide-phpunit.php --coverage-clover /path/to/coverage/ts2_templates$WebTest.coverage --no-configuration WebTest /Users/Ian/php/ts2_templates/tests/WebTest.php

这是覆盖 XML 文件的输出:

<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1341015508">
    <project timestamp="1341015508">
        <metrics files="0" loc="0" ncloc="0" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
    </project>
</coverage>

测试本身通过了。

我已验证代码中的任何地方都没有 exit 或 die 语句。

有什么想法吗?

【问题讨论】:

    标签: selenium phpunit code-coverage


    【解决方案1】:

    我也遇到了一些问题。 Samuel Goldstein 的以下post in the YII forum 帮助了我:

    我最终将 prepend.php 和 append.php 移动到我的项目的文档根目录中。

    我还发现临时文件位置有所不同 - 我最初试图将它们保存到 /tmp/ 并且 PHP 默默地失败了。当我将 $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] 更改为 myroot/protected/runtime/tmp 并在该目录上执行 chmod 777 时,它开始工作了。

    可能让您有点沮丧的是,通过 Ajax 运行的代码不会被标记为被覆盖。

    这似乎是 Selenium 的一个已知问题。谷歌“github sebastianbergmann phpunit-selenium issues”并追踪closed issue #22以获取更多信息。

    【讨论】:

      【解决方案2】:

      它确实有效。我已经根据文档在 symfony 中设置了 selenium 测试来测量覆盖率。

      我遇到的最大问题是,覆盖数据的文件路径错误,因此无法将源与覆盖数据对齐。这是因为我从不同的地方执行测试,因为服务器将文件保存在其中。因此我调整了append.php 以重写源文件所在位置的路径。

      【讨论】:

        猜你喜欢
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 2015-09-24
        • 2014-09-20
        • 1970-01-01
        • 2022-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多