Skip to content

Commit

Permalink
Merge pull request #38 from ergebnis/feature/maximum-duration
Browse files Browse the repository at this point in the history
Enhancement: Include maximum duration for each slow test in report
  • Loading branch information
ergebnis-bot committed Jan 25, 2021
2 parents 6aed224 + 75e370c commit 55bbaca
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
* Used `TimeKeeper` instead of `SlowTestCollector` in `Subscriber\TestPreparedSubscriber` ([#25]), by [@localheinz]
* Used `TimeKeeper` and `Collector\Collector` instead of `SlowTestCollector` in `Subscriber\TestPassedSubscriber` ([#26]), by [@localheinz]
* Composed maximum duration into `SlowTest` ([#37]), by [@localheinz]
* Rendered maximum duration in report created by `DefaultReporter` ([#38]), by [@localheinz]

### Removed

Expand All @@ -53,5 +54,6 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
[#34]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/34
[#36]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/36
[#37]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/37
[#38]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/38

[@localheinz]: https://github.com/localheinz
36 changes: 27 additions & 9 deletions src/Reporter/DefaultReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,15 @@ private function header(SlowTest ...$slowTests): string
{
$count = \count($slowTests);

$formattedMaximumDuration = $this->durationFormatter->format($this->maximumDuration);

if (1 === $count) {
return <<<TXT
Detected {$count} test that took longer than {$formattedMaximumDuration}.
Detected {$count} test that took longer than expected.
TXT;
}

return <<<TXT
Detected {$count} tests that took longer than {$formattedMaximumDuration}.
Detected {$count} tests that took longer than expected.
TXT;
}
Expand All @@ -109,14 +107,34 @@ private function list(SlowTest ...$slowTests): string
/** @var SlowTest $slowestTest */
$slowestTest = \reset($slowTestsToReport);

$longestMaximumDuration = \array_reduce(
$slowTestsToReport,
static function (Event\Telemetry\Duration $maximumDuration, SlowTest $slowTest): Event\Telemetry\Duration {
if ($maximumDuration->isLessThan($slowTest->maximumDuration())) {
return $slowTest->maximumDuration();
}

return $maximumDuration;
},
$this->maximumDuration
);

$durationFormatter = $this->durationFormatter;

$width = \strlen($durationFormatter->format($slowestTest->duration()));
$durationWidth = \strlen($durationFormatter->format($slowestTest->duration()));
$maximumDurationWidth = \strlen($durationFormatter->format($longestMaximumDuration));

$items = \array_map(static function (SlowTest $slowTest) use ($durationFormatter, $width): string {
$label = \str_pad(
$items = \array_map(static function (SlowTest $slowTest) use ($durationFormatter, $durationWidth, $maximumDurationWidth): string {
$formattedDuration = \str_pad(
$durationFormatter->format($slowTest->duration()),
$width,
$durationWidth,
' ',
\STR_PAD_LEFT
);

$formattedMaximumDuration = \str_pad(
$durationFormatter->format($slowTest->maximumDuration()),
$maximumDurationWidth,
' ',
\STR_PAD_LEFT
);
Expand All @@ -130,7 +148,7 @@ private function list(SlowTest ...$slowTests): string
);

return <<<TXT
{$label}: {$testName}
{$formattedDuration} ({$formattedMaximumDuration}): {$testName}
TXT;
}, $slowTestsToReport);

Expand Down
71 changes: 43 additions & 28 deletions test/Unit/Reporter/DefaultReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM
7,
890_123_456
),
$maximumDuration
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
3,
500_000_000
)
),
];

Expand All @@ -116,9 +119,9 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM
$report = $reporter->report(...$slowTests);

$expected = <<<'TXT'
Detected 1 test that took longer than 100 ms.
Detected 1 test that took longer than expected.
7,890 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
7,890 ms (3,500 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
TXT;

self::assertSame($expected, $report);
Expand All @@ -144,7 +147,10 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM
7,
890_123_456
),
$maximumDuration
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
3,
500_000_000
)
),
SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Expand Down Expand Up @@ -209,13 +215,13 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM
$report = $reporter->report(...$slowTests);

$expected = <<<'TXT'
Detected 5 tests that took longer than 100 ms.
Detected 5 tests that took longer than expected.
12,345 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
1,234 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::quz
123 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::baz with dataset "string"
12,345 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms (3,500 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
1,234 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::quz
123 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::baz with dataset "string"
TXT;

self::assertSame($expected, $report);
Expand All @@ -239,7 +245,10 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsEqualToTheMaxim
7,
890_123_456
),
$maximumDuration
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
3,
500_000_000
)
),
SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Expand Down Expand Up @@ -304,13 +313,13 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsEqualToTheMaxim
$report = $reporter->report(...$slowTests);

$expected = <<<'TXT'
Detected 5 tests that took longer than 100 ms.
Detected 5 tests that took longer than expected.
12,345 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
1,234 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::quz
123 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::baz with dataset "string"
12,345 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms (3,500 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
1,234 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::quz
123 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::baz with dataset "string"
TXT;

self::assertSame($expected, $report);
Expand All @@ -334,7 +343,10 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsOneMoreThanTheM
7,
890_123_456
),
$maximumDuration
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
3,
500_000_000
)
),
SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Expand Down Expand Up @@ -399,12 +411,12 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsOneMoreThanTheM
$report = $reporter->report(...$slowTests);

$expected = <<<'TXT'
Detected 5 tests that took longer than 100 ms.
Detected 5 tests that took longer than expected.
12,345 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
1,234 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::quz
12,345 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms (3,500 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
1,234 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::quz
There is one additional slow test that is not listed here.
TXT;
Expand All @@ -430,7 +442,10 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsGreaterThanTheM
7,
890_123_456
),
$maximumDuration
Event\Telemetry\Duration::fromSecondsAndNanoseconds(
3,
500_000_000
)
),
SlowTest::fromTestDurationAndMaximumDuration(
new Event\Code\Test(
Expand Down Expand Up @@ -495,11 +510,11 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsGreaterThanTheM
$report = $reporter->report(...$slowTests);

$expected = <<<'TXT'
Detected 5 tests that took longer than 100 ms.
Detected 5 tests that took longer than expected.
12,345 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms: Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
12,345 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::bar
7,890 ms (3,500 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::foo with data set #123
3,456 ms ( 100 ms): Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::qux
There are 2 additional slow tests that are not listed here.
TXT;
Expand Down

0 comments on commit 55bbaca

Please sign in to comment.