php-code-coverage icon indicating copy to clipboard operation
php-code-coverage copied to clipboard

Wrong coverage when a testcase is run multiple times

Open yannooo opened this issue 15 years ago • 5 comments

When a TestCase is run multiple times within a test suite, code coverage information is wrong. We are doing this in our project to test the same code under different configuration such as db, db+memcached, db+memcached+sphinx, master slave database. Both html and clover reports are affected by this bug.

As soon as a test is run multiple times, all the uncovered code is treated as a comment. This result as everything appears as 100% covered. This was not happening with phpunit 3.4

I've managed to create a minimal test case reproducing this issue. I've put the test case in this gist https://gist.github.com/609633

I have created a temporary fix by modifying the append function in CodeCoverage.php to suffix the test id by a uniq identifier so that coverage information from multiple runs of the same TestCase doesn't colide in the report.

public function append(array $data, $id = NULL, array $filterGroups = array('DEFAULT'))
{
    [.............]
    if (!empty($data)) {
        if ($id instanceof PHPUnit_Framework_TestCase) {
            $status           = $id->getStatus();
            $id               = get_class($id) . '::' . $id->getName() . '::' . uniqid('hack_');
            $this->tests[$id] = $status;
        }
[.....]
}

yannooo avatar Jan 19 '11 16:01 yannooo

Is there any chance you can test this with the master and/or report-refactoring branch(es) of PHP_CodeCoverage? If not, no worries. I will investigate the issue ASAP.

sebastianbergmann avatar Jan 19 '11 17:01 sebastianbergmann

BTW: A better way to implement what you want would be

abstract class FooTestCase extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
    }
}

class BarTest extends FooTestCase
{
}

class BazTest extends FooTestCase
{
}

BarTest and BazTest would then get their tests from FooTestCase but could, for instance, set up different database connections using their respective setUp() methods.

sebastianbergmann avatar Jan 19 '11 17:01 sebastianbergmann

I've tested the same testcase against master and the issue is still there. I've tried against the report-refactoring branch but I had an issue loading this file "File/Iterator/Autoload.php" which doesn't exist.

yannooo avatar Jan 25 '11 08:01 yannooo

It exists, it just is in another package. No worries, I'll look into it.

sebastianbergmann avatar Jan 25 '11 08:01 sebastianbergmann

Won't fix, please refactor your test case(s) like outlined in my earlier comment.

sebastianbergmann avatar Aug 28 '11 17:08 sebastianbergmann