PHPUnit_Selenium 代码覆盖是否有效? [英] Does PHPUnit_Selenium Code Coverage Work?

查看:23
本文介绍了PHPUnit_Selenium 代码覆盖是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHPUnit 文档中,它说可以获取代码覆盖率数据:

In the PHPUnit docs, it says that it's possible to get code coverage data:

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

PHPUnit_Extensions_SeleniumTestCase can collect code coverage information for tests run through Selenium:

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

  1. Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.

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

In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.

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

In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use protected $coverageScriptUrl = 'http://host/phpunit_coverage.php'; to configure the URL for the phpunit_coverage.php script.

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

I haven't been able to get this to output any coverage information. I am able to get code coverage info through normal unit tests.

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

For my app running at http://localhost/ts2_templates/ I've copied phpunit_coverage.php to http://localhost/phpunit_coverage.php.

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

I've added the following to 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(是的,是我"); 调用的.

... and verified they are being called with a die("yep it's me");.

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

Finally, I added the following to my test case:

<?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 生成,代码覆盖率:

Here's the command for running the test with code coverage, generated by 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 文件的输出:

Heres the output of the coverage XML file:

<?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 语句.

I have verified there are no exit or die statements anywhere in the code.

有什么想法吗?

推荐答案

我也遇到了一些问题.以下帖子在Samuel Goldstein 的 YII 论坛帮助了我:

I also had some issues getting things to work. The following post in the YII forum by Samuel Goldstein helped me out:

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

I ended up moving the prepend.php and append.php into my project's document root.

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

I also found that the temporary file location made a difference - I originally was trying to save them to /tmp/ and PHP was silently failing. When I changed $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] to myroot/protected/runtime/tmp and did a chmod 777 on that directory, it started working.

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

One thing that might frustrate you a bit is that code run through Ajax does not get flagged as being covered.

这似乎是 Selenium 的一个已知问题.谷歌github sebastianbergmann phpunit-selenium 问题"并追踪 已关闭问题 #22更多信息.

This appears to be a known problem with Selenium. Google "github sebastianbergmann phpunit-selenium issues" and track down closed issue #22 for more information.

这篇关于PHPUnit_Selenium 代码覆盖是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆