Core\Test\Summary::flaky
$summary->flaky(): intSummary::flaky() returns how many tests of the class failed first and then passed on a retry. Such a test is called flaky.
A test runs again after a failure only when its #[Test] attribute has retries:. A flaky test is not counted in passed() or in failed().
You get a Summary as the parameter of a public static method with #[AfterClass]. The test runner creates it after the last test of the class. You cannot create a Summary yourself.
In plain words: a flaky test passes only sometimes. It often depends on time, on the network or on the order of the tests. It is worth fixing before it starts to fail.
The examples below show a count of flaky tests, a warning, and a limit for the number of flaky tests.
Return value
Returns int. The number of flaky tests.
Examples
A count of flaky tests
<?nvs// Shows an `#[AfterClass]` method that prints how many tests passed only on a// retry. The test below may run up to 3 times.
use Core\Test;use Core\Test\AfterClass;use Core\Test\Summary;
class Counts{ public static function line(string $class, int $passed, int $flaky): string { return $class . ': ' . $passed . ' passed, ' . $flaky . ' flaky'; }}
class WeatherTest{ #[Test(retries: 2, because: 'the weather service is sometimes slow')] public function itReadsTheTemperature(): void { Test::assertSame(Core\Str::trim(' 21 '), '21'); }
// `flaky()` counts a test that failed first and passed on a retry. #[AfterClass] public static function printCounts(Summary $summary): void { echo Counts::line($summary->class(), $summary->passed(), $summary->flaky()), "\n"; }}
// This prints `WeatherTest: 4 passed, 1 flaky`.echo Counts::line('WeatherTest', 4, 1), "\n";WeatherTest: 4 passed, 1 flakyA warning about flaky tests
<?nvs// Shows an `#[AfterClass]` method that prints a warning when a test of the class// was flaky. The run still passes, so without a warning nobody would notice.
use Core\Test;use Core\Test\AfterClass;use Core\Test\Summary;
class FlakyWarning{ public static function of(string $class, int $flaky): ?string { if ($flaky == 0) { return null; } return $class . ': ' . $flaky . ' test(s) passed only on a retry. Please fix them.'; }}
class CacheTest{ #[Test(retries: 1, because: 'the cache entry may not be written yet')] public function itReturnsTheStoredValue(): void { Test::assertSame('cart:42', 'cart:42'); }
#[AfterClass] public static function warn(Summary $summary): void { ?string $warning = FlakyWarning::of($summary->class(), $summary->flaky()); if ($warning != null) { echo $warning, "\n"; } }}
// This prints `CacheTest: 1 test(s) passed only on a retry. Please fix them.`.echo FlakyWarning::of('CacheTest', 1) ?? '', "\n";CacheTest: 1 test(s) passed only on a retry. Please fix them.A limit for flaky tests
<?nvs// Shows an `#[AfterClass]` method that allows at most 2 flaky tests per class.// Above the limit, it throws an error, and the test run fails.
use Core\Test;use Core\Test\AfterClass;use Core\Test\Summary;
class FlakyLimit{ public const int MAX = 2;
public static function message(string $class, int $flaky): ?string { if ($flaky <= FlakyLimit::MAX) { return null; } return $class . ' has ' . $flaky . ' flaky tests, the limit is ' . FlakyLimit::MAX; }}
class UploadTest{ #[Test(retries: 2, because: 'the test storage is shared')] public function itStoresThePicture(): void { Test::assertSame(Core\Str::endsWith('cat.jpg', '.jpg'), true); }
#[AfterClass] public static function checkTheLimit(Summary $summary): void { ?string $message = FlakyLimit::message($summary->class(), $summary->flaky()); if ($message != null) { throw new RuntimeError($message); } }}
// This prints `UploadTest has 3 flaky tests, the limit is 2`.echo FlakyLimit::message('UploadTest', 3) ?? '', "\n";UploadTest has 3 flaky tests, the limit is 2