PHPUnit_Selenium代码覆盖范围有效吗? [英] Does PHPUnit_Selenium Code Coverage Work?
问题描述
在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:
-
将PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php复制到Web服务器的文档根目录中.
Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
在Web服务器的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("yep it's me");
验证了它们正在被调用.
... 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
/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文件的输出:
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.
这似乎是硒的已知问题. Google"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屋!