In the PHPUnit docs,它表示可以获取代码覆盖率数据:
PHPUnit_Extensions_SeleniumTestCase可以为通过Selenium运行的测试收集代码覆盖率信息:
将PHPUnit / Extensions / SeleniumTestCase / phpunit_coverage.php复制到Web服务器的文档根目录中。
在您的网络服务器的php.ini配置文件中,将PHPUnit / Extensions / SeleniumTestCase / prepend.php和PHPUnit / Extensions / SeleniumTestCase / append.php分别配置为auto_prepend_file和auto_append_file。
在扩展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
以下是coverage 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语句。
有任何想法吗?
最佳答案
我也遇到了一些问题,无法正常工作。 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运行的代码不会被标记为被覆盖。
这似乎是硒的已知问题。谷歌“ github sebastianbergmann phpunit-selenium问题”,并跟踪closed issue #22以获取更多信息。
关于selenium - PHPUnit_Selenium代码覆盖范围有效吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11270527/