Core\Test\Outcome::failures
Outcome::failures() returns the failure messages of this run of the test, as an array of strings. The messages are the same ones the test report prints. When the run passed, the array is empty.
You get an Outcome as the parameter of a method with #[AfterEachTest]. That method runs after each test, also when the test failed. You cannot create an Outcome yourself.
A test stops at its first failed assertion, and that assertion gives one message. An error the test throws and does not catch also gives one message, which is the message of the error.
The examples below show the failures written to a log file, a count of the messages, and a short line for a chat message.
Return value
Returns array<string>. One message per failure, as the report prints it. The list is empty when the run passed.
Examples
Write the failures to a log file
<?nvs// Shows a hook that writes the failure messages of each failed test to a log// file. Each line has the test name and one message.
use Core\IO;use Core\Path;use Core\Test;use Core\Test\AfterEachTest;use Core\Test\Outcome;
class FailureLog{ public static function lines(string $test, array<string> $failures): string { string $text = ''; foreach ($failures as string $failure) { $text = $text . $test . ': ' . $failure . "\n"; } return $text; }}
class Shipping{ // The prices are in cents. An order of 50 euros or more ships for free. public static function cost(int $order): int { return $order >= 5000 ? 0 : 490; }}
class ShippingTest{ #[Test] public function itIsFreeAboveFiftyEuros(): void { Test::assertSame(Shipping::cost(5000), 0); }
// `failures()` is empty for a passed test, so nothing is written. #[AfterEachTest] public function writeFailures(Outcome $outcome): void { string $text = FailureLog::lines($outcome->name(), $outcome->failures()); if ($text != '') { IO::append(Path::fromWorkingDirectory('failures.log'), $text); } }}
// This prints `itIsFreeAboveFiftyEuros: shipping is 490`.echo FailureLog::lines('itIsFreeAboveFiftyEuros', ['shipping is 490']);itIsFreeAboveFiftyEuros: shipping is 490Count the failure messages
<?nvs// Shows a hook that prints how many failure messages a run has. A passed run has// none, so the hook prints `0` for it.
use Core\Arr;use Core\Test;use Core\Test\AfterEachTest;use Core\Test\Outcome;
class Count{ public static function line(string $test, array<string> $failures): string { return $test . ': ' . Arr::count($failures) . ' failure(s)'; }}
class TaxTest{ #[Test] public function itUsesTheReducedRateForBooks(): void { var $rates = ['book' => 10, 'tea' => 20]; Test::assertSame($rates['book'], 10); }
#[AfterEachTest] public function printCount(Outcome $outcome): void { echo Count::line($outcome->name(), $outcome->failures()), "\n"; }}
// This prints `itUsesTheReducedRateForBooks: 0 failure(s)`.echo Count::line('itUsesTheReducedRateForBooks', []), "\n";itUsesTheReducedRateForBooks: 0 failure(s)A short line for a chat message
<?nvs// Shows a hook that makes one short line from the first failure message. A team// can post this line in a chat when a test fails at night.
use Core\Arr;use Core\Str;use Core\Test;use Core\Test\AfterEachTest;use Core\Test\Outcome;
class ChatLine{ // A long message is cut after 40 characters, and `...` is added. public static function of(string $test, array<string> $failures): ?string { // A passed run has no failure, so there is no message to post. string $first = Arr::first($failures) ?? ''; if ($first == '') { return null; } if (Str::length($first) > 40) { $first = Str::slice($first, 0, 40) . '...'; } return 'Test ' . $test . ' failed: ' . $first; }}
class SearchTest{ #[Test] public function itFindsTheProductByName(): void { array<string> $found = Arr::filter(['green tea', 'black tea', 'cake'], fn(string $name): bool => Str::contains($name, 'tea')); Test::assertCount($found, 2); }
#[AfterEachTest] public function prepareTheMessage(Outcome $outcome): void { ?string $line = ChatLine::of($outcome->name(), $outcome->failures()); if ($line != null) { echo $line, "\n"; } }}
// This prints the line with a long message cut short.echo ChatLine::of('itFindsTheProductByName', ["Core\\Test::assertCount failed: the array has 1 element, not 2"]) ?? '', "\n";Test itFindsTheProductByName failed: Core\Test::assertCount failed: the array...