Core\Test\Summary::failed
$summary->failed(): intSummary::failed() returns how many tests of the class failed. When every test passed, the result is 0.
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. That method also runs when a test failed. You cannot create a Summary yourself.
A test with retries: counts as failed only when its last run failed too. A test that ended the program with exit() also counts as failed.
The examples below show a warning when a test failed, a status file for a build server, and the subject line of a report email.
Return value
Returns int. The number of failed tests.
Examples
A warning when a test failed
<?nvs// Shows an `#[AfterClass]` method that prints a warning when a test of the class// failed. When every test passed, it prints nothing.
use Core\Test;use Core\Test\AfterClass;use Core\Test\Summary;
class Warning{ public static function forClass(string $class, int $failed): ?string { if ($failed == 0) { return null; } return 'warning: ' . $failed . ' test(s) failed in ' . $class; }}
class OpeningHoursTest{ #[Test] public function itIsOpenAtNoon(): void { int $hour = 12; Test::assertTrue($hour >= 9 && $hour < 18); }
#[AfterClass] public static function warn(Summary $summary): void { ?string $warning = Warning::forClass($summary->class(), $summary->failed()); if ($warning != null) { echo $warning, "\n"; } }}
// This prints `warning: 2 test(s) failed in OpeningHoursTest`.echo Warning::forClass('OpeningHoursTest', 2) ?? '', "\n";warning: 2 test(s) failed in OpeningHoursTestA status file for a build server
<?nvs// Shows an `#[AfterClass]` method that writes `red` or `green` to a status file.// A build server can read this file and show a colored light.
use Core\IO;use Core\Path;use Core\Test;use Core\Test\AfterClass;use Core\Test\Summary;
class Light{ public static function color(int $failed): string { return $failed > 0 ? 'red' : 'green'; }}
class InventoryTest{ #[Test] public function itNeverGoesBelowZero(): void { int $stock = 2; int $ordered = 3; Test::assertSame($ordered > $stock ? 0 : $stock - $ordered, 0); }
#[AfterClass] public static function writeStatus(Summary $summary): void { IO::write(Path::fromWorkingDirectory('build-light.txt'), Light::color($summary->failed())); }}
// This prints `red green`.echo Light::color(1), ' ', Light::color(0), "\n";red greenThe subject of a report email
<?nvs// Shows an `#[AfterClass]` method that builds the subject line of a report email.// The subject starts with `FAILED` when a test failed, so it is easy to spot.
use Core\IO;use Core\Path;use Core\Test;use Core\Test\AfterClass;use Core\Test\Summary;
class Subject{ public static function of(string $class, int $passed, int $failed): string { if ($failed > 0) { return 'FAILED: ' . $failed . ' of ' . ($passed + $failed) . ' tests in ' . $class; } return 'OK: ' . $passed . ' tests in ' . $class; }}
class NewsletterTest{ #[Test] public function itHasAnUnsubscribeLink(): void { Test::assertTrue(Core\Str::contains("<a href=\"/unsubscribe\">", 'unsubscribe')); }
// A mail job can send the file later. #[AfterClass] public static function writeSubject(Summary $summary): void { string $subject = Subject::of($summary->class(), $summary->passed(), $summary->failed()); IO::write(Path::fromWorkingDirectory('report-subject.txt'), $subject); }}
// This prints `FAILED: 1 of 4 tests in NewsletterTest`.echo Subject::of('NewsletterTest', 3, 1), "\n";FAILED: 1 of 4 tests in NewsletterTest