Skip to content

Core\Test\Summary::failedTests

$summary->failedTests(): array<string>

Summary::failedTests() returns the names of the tests of the class that failed, as an array of strings. When no test failed, the array is empty.

Each name is the name of the test method, the same way the test report prints it. A test with #[TestWith] rows has the row number after the name, such as itRoundsThePrice#1.

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.

The examples below show the failed tests one per line, a list for a chat message, and a file that the next run can read.

Return value

Returns array<string>. One name per failed test, as the report prints it. The list is empty when no test failed.

Examples

The failed tests one per line

<?nvs
// Shows an `#[AfterClass]` method that prints the name of each failed test on
// its own line.
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class FailedList
{
public static function lines(string $class, array<string> $tests): string
{
string $text = '';
foreach ($tests as string $test) {
$text = $text . 'failed: ' . $class . '::' . $test . "\n";
}
return $text;
}
}
class PasswordTest
{
#[Test]
public function itNeedsTwelveCharacters(): void
{
Test::assertTrue(Core\Str::length('correct horse') >= 12);
}
// `failedTests()` is empty when every test passed, so this prints nothing.
#[AfterClass]
public static function printFailed(Summary $summary): void
{
echo FailedList::lines($summary->class(), $summary->failedTests());
}
}
// This prints two lines, one for each failed test.
echo FailedList::lines('PasswordTest', ['itNeedsTwelveCharacters', 'itNeedsANumber']);
Output
failed: PasswordTest::itNeedsTwelveCharacters
failed: PasswordTest::itNeedsANumber

A list for a chat message

<?nvs
// Shows an `#[AfterClass]` method that joins the failed tests into one line. A
// team can post this line in a chat after a test run at night.
use Core\Arr;
use Core\Str;
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class ChatLine
{
public static function of(string $class, array<string> $tests): ?string
{
if (Arr::isEmpty($tests)) {
return null;
}
return $class . ' failed: ' . Str::join($tests, ', ');
}
}
class OrderTest
{
#[Test]
public function itAddsTheShipping(): void
{
Test::assertSame(1250 + 490, 1740);
}
#[AfterClass]
public static function prepareTheMessage(Summary $summary): void
{
?string $line = ChatLine::of($summary->class(), $summary->failedTests());
if ($line != null) {
echo $line, "\n";
}
}
}
// This prints `OrderTest failed: itAddsTheShipping, itAddsTheTax`.
echo ChatLine::of('OrderTest', ['itAddsTheShipping', 'itAddsTheTax']) ?? '', "\n";
Output
OrderTest failed: itAddsTheShipping, itAddsTheTax

A file the next run can read

<?nvs
// Shows an `#[AfterClass]` method that writes the failed tests to a file, one
// name per line. After a fix, you can read the file and check these tests first.
use Core\IO;
use Core\Path;
use Core\Str;
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
use Core\Test\TestWith;
class Rerun
{
public static function fileText(array<string> $tests): string
{
return Str::join($tests, "\n") . "\n";
}
}
class PackingFee
{
// The fee is 50 cents for each item.
public static function of(int $items): int
{
return $items * 50;
}
}
class PackingFeeTest
{
// A failed row has its row number in the name, such as `itChargesPerItem#1`.
#[TestWith(items: 3, fee: 150)]
#[TestWith(items: 0, fee: 0)]
#[Test]
public function itChargesPerItem(int $items, int $fee): void
{
Test::assertSame(PackingFee::of($items), $fee);
}
#[AfterClass]
public static function writeRerunFile(Summary $summary): void
{
IO::write(Path::fromWorkingDirectory('failed-tests.txt'), Rerun::fileText($summary->failedTests()));
}
}
// This prints the file for two failed tests.
echo Rerun::fileText(['itChargesPerItem#1', 'itIsFreeForEmptyOrders']);
Output
itChargesPerItem#1
itIsFreeForEmptyOrders