Skip to content

PHPUnit 12 badly evaluates time for very first test #6162

@niconoe-

Description

@niconoe-
Q A
PHPUnit version 12.0.x
PHP version 8.3.x
Installation Method Composer

Summary

When running PHPUnit 12, the very first test is reported to have a time-consumption really higher than with PHPUnit 11 on JUnit and cache-files reports. I bet this might be caused by an unwanted including of tests preparations before the very first test plays, but I can't find any location in source code so I wasn't able to provide a PR about it.

Current behavior

The "time" value on JUnit report is currently not the actual execution time for the very first test only.

On PHPUnit 12.0.10 :

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="/data/www/phpunit.xml" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.452475">
    <testsuite name="myproject" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.452475">
      <testsuite name="MyProject\Tests\FooBarTest" file="/data/www/src/Tests/FooBarTest.php" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.452475">
        <testsuite name="MyProject\Tests\FooBarTest::testTrue" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.452475">
          <testcase name="testTrue with data set &quot;test-case #1&quot;" file="/data/www/src/Tests/FooBarTest.php" line="17" class="MyProject\Tests\FooBarTest" classname="MyProject.Tests.FooBarTest" assertions="1" time="0.445116"/>
          <testcase name="testTrue with data set &quot;test-case #2&quot;" file="/data/www/src/Tests/FooBarTest.php" line="17" class="MyProject\Tests\FooBarTest" classname="MyProject.Tests.FooBarTest" assertions="1" time="0.003829"/>
          <testcase name="testTrue with data set &quot;test-case #3&quot;" file="/data/www/src/Tests/FooBarTest.php" line="17" class="MyProject\Tests\FooBarTest" classname="MyProject.Tests.FooBarTest" assertions="1" time="0.003530"/>
        </testsuite>
      </testsuite>
    </testsuite>
  </testsuite>
</testsuites>

On PHPUnit 11.5.15 :

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="/data/www/phpunit.xml" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.074995">
    <testsuite name="myproject" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.074995">
      <testsuite name="MyProject\Tests\FooBarTest" file="/data/www/src/Tests/FooBarTest.php" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.074995">
        <testsuite name="MyProject\Tests\FooBarTest::testTrue" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.074995">
          <testcase name="testTrue with data set &quot;test-case #1&quot;" file="/data/www/src/Tests/FooBarTest.php" line="17" class="MyProject\Tests\FooBarTest" classname="MyProject.Tests.FooBarTest" assertions="1" time="0.066512"/>
          <testcase name="testTrue with data set &quot;test-case #2&quot;" file="/data/www/src/Tests/FooBarTest.php" line="17" class="MyProject\Tests\FooBarTest" classname="MyProject.Tests.FooBarTest" assertions="1" time="0.004285"/>
          <testcase name="testTrue with data set &quot;test-case #3&quot;" file="/data/www/src/Tests/FooBarTest.php" line="17" class="MyProject\Tests\FooBarTest" classname="MyProject.Tests.FooBarTest" assertions="1" time="0.004197"/>
        </testsuite>
      </testsuite>
    </testsuite>
  </testsuite>
</testsuites>

How to reproduce

Simply create a very simple test case and compare the execution time with actual time, or with PHPUnit 11.

PHP Test file used for given stats:

<?php declare(strict_types=1);

namespace MyProject\Tests;

final class FooBarTest extends \PHPUnit\Framework\TestCase
{
    public static function provideTest(): \Generator
    {
        yield 'test-case #1' => ['a'];
        yield 'test-case #2' => ['b'];
        yield 'test-case #3' => ['c'];
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('provideTest')]
    public function testTrue(): void
    {
        self::assertTrue(true);
    }
}

Expected behavior

The "time" value on JUnit report should reflects the actual execution time of the test and not include the data preparation, like for PHPUnit 11

Thank you very much 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature/logging/junitIssues related to logging test results in JUnit XML formattype/bugSomething is brokenversion/12Something affects PHPUnit 12

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions