Skip to content

Commit e418bd4

Browse files
mvoriseklocalheinz
andcommitted
Consistently include test setup and teardown in duration measurement
Co-authored-by: Andreas Möller <[email protected]> Co-authored-by: Michael Voříšek <[email protected]>
1 parent d94c5ad commit e418bd4

File tree

24 files changed

+125
-74
lines changed

24 files changed

+125
-74
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
For a full diff see [`2.8.0...main`][2.8.0...main].
1010

11+
### Changed
12+
13+
- Consistently included test setup and teardown in duration measurement ([#380]), by [@localheinz] and [@mvorisek]
14+
1115
### Fixed
1216

1317
- Required at least `phpunit/phpunit:^7.5.0` ([#448]), by [@localheinz]

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ When using `phpunit/phpunit:^7.5.0`, `phpunit/phpunit:^8.5.19`, or `phpunit/phpu
376376

377377
The hooks event system supports eleven hook methods that `phpunit/phpunit` invokes during the execution of tests.
378378

379-
When the extension uses the hooks event system, it uses the [`PHPUnit\Runner\AfterSuccessfulTestHook`](https://github.com/sebastianbergmann/phpunit/blob/8.5.19/src/Runner/Hook/AfterSuccessfulTestHook.php#L12-L15), which receives the [duration of invoking `PHPUnit\Framework\TestCase::runBare()` and more](https://github.com/sebastianbergmann/phpunit/blob/8.5.19/src/Framework/TestResult.php#L671-L754).
379+
When the extension uses the hooks event system, it uses the [`PHPUnit\Runner\AfterTestHook`](https://github.com/sebastianbergmann/phpunit/blob/7.5.0/src/Runner/Hook/AfterTestHook.php#L12-L21), which receives the [duration of invoking `PHPUnit\Framework\TestCase::runBare()` and more](https://github.com/sebastianbergmann/phpunit/blob/8.5.19/src/Framework/TestResult.php#L671-L754).
380380

381381
When `phpunit/phpunit` invokes `PHPUnit\Framework\TestCase::runBare()`, it will invoke the following methods before the first test method in the class:
382382

@@ -405,7 +405,28 @@ When using `phpunit/phpunit:^10.0.0`, the extension uses the new event system of
405405

406406
The new event system supports a wide range of events that `phpunit/phpunit` emits during the execution of tests.
407407

408-
When the extension uses the new event system, it uses and subscribes to the [`PHPUnit\Event\Test\Prepared`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Event/Events/Test/Lifecycle/Prepared.php#L22-L50) and [`PHPUnit\Event\Test\Passed`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Event/Events/Test/Outcome/Passed.php#L22-L50) events and measures the [duration between the points in time when `phpunit/phpunit` emits the former and the latter](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L614-L666).
408+
When the extension uses the new event system, it uses and subscribes to the [`PHPUnit\Event\Test\PreparationStarted`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Event/Events/Test/Lifecycle/PreparationStarted.php#L22-L50) and [`PHPUnit\Event\Test\Finished`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Event/Events/Test/Lifecycle/Finished.php#L22-L57) events and measures the duration between the points in time when `phpunit/phpunit` emits the former and the latter.
409+
410+
When `phpunit/phpunit` invokes `PHPUnit\Framework\TestCase::runBare()`, it will invoke the following methods before the first test method in the class:
411+
412+
- [`PHPUnit\Framework\TestCase::setUpBeforeClass()` and methods annotated with `@beforeClass`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L602-L604)
413+
414+
When `phpunit/phpunit` invokes `PHPUnit\Framework\TestCase::runBare()`, it will invoke the following methods before every test method in the class:
415+
416+
- [`PHPUnit\Framework\TestCase::setUp()` and methods annotated with `@before`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L611)
417+
- [`PHPUnit\Framework\TestCase::assertPreConditions()`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L612)
418+
419+
When `phpunit/phpunit` invokes `PHPUnit\Framework\TestCase::runBare()`, it will invoke the following methods after every test method in the class:
420+
421+
- [`PHPUnit\Framework\TestCase::assertPostConditions()`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L622)
422+
- [`PHPUnit\Framework\TestCase::tearDown()` and methods annotated with `@after`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L680)
423+
424+
When phpunit/phpunit invokes `PHPUnit\Framework\TestCase::runBare()`, it will invoke the following methods after the last test method in the class:
425+
426+
- [`PHPUnit\Framework\TestCase::tearDownAfterClass()` and methods annotated with `@afterClass`](https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L683)
427+
428+
> [!NOTE]
429+
> Because of this behavior, the measured test durations can and will vary depending on the order in which `phpunit/phpunit` executes tests.
409430
410431
## Changelog
411432

composer-require-checker.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"int",
88
"null",
99
"PHPUnit\\Event\\Code\\Test",
10-
"PHPUnit\\Event\\Test\\Passed",
11-
"PHPUnit\\Event\\Test\\PassedSubscriber",
12-
"PHPUnit\\Event\\Test\\Prepared",
13-
"PHPUnit\\Event\\Test\\PreparedSubscriber",
10+
"PHPUnit\\Event\\Test\\Finished",
11+
"PHPUnit\\Event\\Test\\FinishedSubscriber",
12+
"PHPUnit\\Event\\Test\\PreparationStarted",
13+
"PHPUnit\\Event\\Test\\PreparationStartedSubscriber",
1414
"PHPUnit\\Event\\TestRunner\\ExecutionFinished",
1515
"PHPUnit\\Event\\TestRunner\\ExecutionFinishedSubscriber",
1616
"PHPUnit\\Metadata\\Annotation\\Parser\\Registry",

psalm-baseline.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
<code>($durationInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds) / 1_000</code>
2222
</InvalidOperand>
2323
</file>
24-
<file src="src/Subscriber/Test/PassedSubscriber.php">
24+
<file src="src/Subscriber/Test/FinishedSubscriber.php">
2525
<UndefinedClass>
26-
<code>Event\Test\PassedSubscriber</code>
26+
<code>Event\Test\FinishedSubscriber</code>
2727
</UndefinedClass>
2828
<UnusedClass>
29-
<code>PassedSubscriber</code>
29+
<code>FinishedSubscriber</code>
3030
</UnusedClass>
3131
<UnusedParam>
3232
<code>$collector</code>
@@ -35,12 +35,12 @@
3535
<code>$timeKeeper</code>
3636
</UnusedParam>
3737
</file>
38-
<file src="src/Subscriber/Test/PreparedSubscriber.php">
38+
<file src="src/Subscriber/Test/PreparationStartedSubscriber.php">
3939
<UndefinedClass>
40-
<code>Event\Test\PreparedSubscriber</code>
40+
<code>Event\Test\PreparationStartedSubscriber</code>
4141
</UndefinedClass>
4242
<UnusedClass>
43-
<code>PreparedSubscriber</code>
43+
<code>PreparationStartedSubscriber</code>
4444
</UnusedClass>
4545
<UnusedParam>
4646
<code>$event</code>

src/Extension.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
final class Extension implements
3434
Runner\AfterLastTestHook,
3535
Runner\AfterSuccessfulTestHook,
36+
Runner\AfterTestHook,
3637
Runner\BeforeFirstTestHook
3738
{
3839
private int $suites = 0;
@@ -68,9 +69,21 @@ public function executeBeforeFirstTest(): void
6869
++$this->suites;
6970
}
7071

72+
/**
73+
* @see https://github.com/sebastianbergmann/phpunit/pull/3392#issuecomment-1868311482
74+
* @see https://github.com/sebastianbergmann/phpunit/blob/7.5.0/src/TextUI/TestRunner.php#L227-L239
75+
* @see https://github.com/sebastianbergmann/phpunit/pull/3762
76+
*/
7177
public function executeAfterSuccessfulTest(
7278
string $test,
7379
float $time
80+
): void {
81+
// intentionally left blank
82+
}
83+
84+
public function executeAfterTest(
85+
string $test,
86+
float $time
7487
): void {
7588
$seconds = (int) \floor($time);
7689
$nanoseconds = (int) (($time - $seconds) * 1_000_000_000);
@@ -206,8 +219,8 @@ public function bootstrap(
206219
);
207220

208221
$facade->registerSubscribers(
209-
new Subscriber\Test\PreparedSubscriber($timeKeeper),
210-
new Subscriber\Test\PassedSubscriber(
222+
new Subscriber\Test\PreparationStartedSubscriber($timeKeeper),
223+
new Subscriber\Test\FinishedSubscriber(
211224
$maximumDuration,
212225
$timeKeeper,
213226
$collector,

src/Subscriber/Test/PassedSubscriber.php renamed to src/Subscriber/Test/FinishedSubscriber.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* @internal
2929
*/
30-
final class PassedSubscriber implements Event\Test\PassedSubscriber
30+
final class FinishedSubscriber implements Event\Test\FinishedSubscriber
3131
{
3232
private Collector\Collector $collector;
3333
private TimeKeeper $timeKeeper;
@@ -44,9 +44,10 @@ public function __construct(
4444
}
4545

4646
/**
47-
* @see https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L664-L666
47+
* @see https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestRunner.php#L198
48+
* @see https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestRunner.php#L238
4849
*/
49-
public function notify(Event\Test\Passed $event): void
50+
public function notify(Event\Test\Finished $event): void
5051
{
5152
$phaseIdentifier = PhaseIdentifier::fromString($event->test()->id());
5253

src/Subscriber/Test/PreparedSubscriber.php renamed to src/Subscriber/Test/PreparationStartedSubscriber.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* @internal
2323
*/
24-
final class PreparedSubscriber implements Event\Test\PreparedSubscriber
24+
final class PreparationStartedSubscriber implements Event\Test\PreparationStartedSubscriber
2525
{
2626
private TimeKeeper $timeKeeper;
2727

@@ -31,9 +31,9 @@ public function __construct(TimeKeeper $timeKeeper)
3131
}
3232

3333
/**
34-
* @see https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L614-L616
34+
* @see https://github.com/sebastianbergmann/phpunit/blob/10.0.0/src/Framework/TestCase.php#L585-L587
3535
*/
36-
public function notify(Event\Test\Prepared $event): void
36+
public function notify(Event\Test\PreparationStarted $event): void
3737
{
3838
$time = $event->telemetryInfo()->time();
3939

test/EndToEnd/Version10/TestCase/Combination/test.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Configuration: %s/EndToEnd/Version10/TestCase/Combination/phpunit.xml
2424

2525
Detected 3 tests that took longer than expected.
2626

27-
1. 0.4%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
28-
2. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0
29-
3. 0.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\Combination\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
27+
1. 1.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
28+
2. 1.0%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0
29+
3. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\Combination\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
3030

3131
Time: %s, Memory: %s
3232

test/EndToEnd/Version10/TestCase/WithAfterAnnotation/test.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ Configuration: %s/EndToEnd/Version10/TestCase/WithAfterAnnotation/phpunit.xml
2222

2323
... 3 / 3 (100%)
2424

25-
Detected 2 tests that took longer than expected.
25+
Detected 3 tests that took longer than expected.
2626

27-
1. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
28-
2. 0.2%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0
27+
1. 0.4%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
28+
2. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0
29+
3. 0.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAnnotation\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
2930

3031
Time: %s, Memory: %s
3132

test/EndToEnd/Version10/TestCase/WithAfterAttribute/test.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ Configuration: %s/EndToEnd/Version10/TestCase/WithAfterAttribute/phpunit.xml
2222

2323
... 3 / 3 (100%)
2424

25-
Detected 2 tests that took longer than expected.
25+
Detected 3 tests that took longer than expected.
2626

27-
1. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAttribute\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
28-
2. 0.2%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAttribute\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0
27+
1. 0.4%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAttribute\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
28+
2. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAttribute\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0
29+
3. 0.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version10\TestCase\WithAfterAttribute\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
2930

3031
Time: %s, Memory: %s
3132

0 commit comments

Comments
 (0)