2012-06-30 53 views
3

In the PHPUnit docs,它说,它可能得到的代码覆盖率数据:PHPUnit_Selenium代码覆盖率是否工作?

  1. 复制的PHPUnit /扩展/ SeleniumTestCase/phpunit_coverage.php:

    PHPUnit_Extensions_SeleniumTestCase的可以收集代码覆盖率信息通过测试运行硒进入您的网络服务器的文档根目录。

  2. 在你的web服务器的php.ini配置文件,配置的PHPUnit /扩展/ SeleniumTestCase/prepend.php和PHPUnit的/扩展/ SeleniumTestCase/append.php分别中的auto_prepend_file和auto_append_file所。

  3. 在你的测试用例类,它扩展了PHPUnit_Extensions_SeleniumTestCase,使用 保护$ coverageScriptUrl = 'HTTP://host/phpunit_coverage.php'; 配置phpunit_coverage.php脚本的URL。

我一直没能得到这个输出的任何覆盖信息。我可以通过正常的单元测试获得代码覆盖率信息。

对于在http://localhost/ts2_templates/运行我的应用程序,我复制到phpunit_coverage.phphttp://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> 

T他测试自己通过。

我已经验证有没有退出或代码的任何地方死语句。

任何想法?

回答

1

这绝对是工作。根据文档,我已经在symfony中设置了测试覆盖率的硒测试。

我最大的问题是,将覆盖数据有错误路径文件在里面,因此不能对准覆盖数据来源。这是因为我执行的测试,从不同的地方,因为服务器保留在文件中。因此,我调整了append.php的路径改写到我的源文件的地方。

1

我也有一些问题,把事情的工作。以下由塞缪尔戈尔德斯坦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硒问题”和追踪closed issue #22欲了解更多信息。