Skip to content

Core\Test\Summary::passed

$summary->passed(): int

Summary::passed() returns how many tests of the class passed on their first run.

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.

A test that failed first and then passed on a retry is not counted here. flaky() counts it. A test with #[TestWith] rows counts once for each row.

The examples below show a count of passed tests, a check that every test passed, and a line for a status page.

Return value

Returns int. The number of passed tests. A flaky test is not counted here.

Examples

A count of passed tests

<?nvs
// Shows an `#[AfterClass]` method that prints how many tests of the class passed.
use Core\Str;
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class Username
{
public static function clean(string $name): string
{
return Str::lower(Str::trim($name));
}
}
class UsernameTest
{
#[Test]
public function itRemovesSpaces(): void
{
Test::assertSame(Username::clean(' ada '), 'ada');
}
#[Test]
public function itUsesLowerCase(): void
{
Test::assertSame(Username::clean('Linus'), 'linus');
}
// `passed()` is `2` here, one for each test.
#[AfterClass]
public static function printCount(Summary $summary): void
{
echo $summary->class(), ': ', $summary->passed(), " passed\n";
}
}
// This prints `ada linus`.
echo Username::clean(' ada '), ' ', Username::clean('Linus'), "\n";
Output
ada linus

Check that every test passed

<?nvs
// Shows an `#[AfterClass]` method that checks whether every test of the class
// passed. A class where no test ran is not counted as a success.
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class AllPassed
{
// At least one test must pass, and no test may fail or pass only on a retry.
public static function check(int $passed, int $failed, int $flaky): bool
{
return $passed > 0 && $failed == 0 && $flaky == 0;
}
}
class PostcodeTest
{
#[Test]
public function itAcceptsFiveDigits(): void
{
Test::assertSame(Core\Str::length('10115'), 5);
}
#[AfterClass]
public static function printVerdict(Summary $summary): void
{
bool $ok = AllPassed::check($summary->passed(), $summary->failed(), $summary->flaky());
echo $summary->class(), $ok ? ": all passed\n" : ": not all passed\n";
}
}
// This prints `false`, because no test passed.
echo AllPassed::check(0, 0, 0) ? 'true' : 'false', "\n";
Output
false

A line for a status page

<?nvs
// Shows an `#[AfterClass]` method that adds one line to a status page. The line
// shows how many tests passed out of all tests that ran.
use Core\IO;
use Core\Path;
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class StatusLine
{
// A flaky test also ran, so it is part of the total.
public static function of(string $class, int $passed, int $failed, int $flaky): string
{
int $ran = $passed + $failed + $flaky;
return $class . ': ' . $passed . ' of ' . $ran . " passed\n";
}
}
class CheckoutTest
{
#[Test]
public function itNeedsAnAddress(): void
{
Test::assertTrue(Core\Str::isEmpty(''));
}
#[AfterClass]
public static function updateStatusPage(Summary $summary): void
{
string $line = StatusLine::of($summary->class(), $summary->passed(), $summary->failed(), $summary->flaky());
IO::append(Path::fromWorkingDirectory('status.txt'), $line);
}
}
// This prints `CheckoutTest: 9 of 10 passed`.
echo StatusLine::of('CheckoutTest', 9, 1, 0);
Output
CheckoutTest: 9 of 10 passed