Does PHPUnit_Selenium Code Coverage Work?

2019-01-26 21:16发布

问题:

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

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

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

  2. 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.

  3. 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.

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

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"

... 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());
    }
}

?>

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

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>

The test itself passes.

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

Any ideas?

回答1:

It is definitely working. I have set up selenium tests in symfony measuring the coverage according to the documentation.

The biggest issue I had was, that the coverage data had the wrong path to the files in it, and therefore could not align the sources with the coverage data. This is because I executed tests from a different place, as the server kept the files in. Therefore I tweaked the append.php to rewrite the path to the place where my source files are.



回答2:

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

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

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.

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

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