Skip to content

Core\Test\Outcome::attempt

$outcome->attempt(): int

Outcome::attempt() returns which run of the test this was. The first run is 1.

A test with retries: in its #[Test] attribute runs again when it fails. Each new run adds one, so the second run is 2. A test without retries: always has 1.

You get an Outcome as the parameter of a method with #[AfterEachTest]. The method runs after every run, also after a run that failed. You cannot create an Outcome yourself.

Good to know: a file you name after the test is written again on each run. Put the attempt number in the name to keep the file of every run.

The examples below show a log file for each run, a warning for a test that needed a retry, and a CSV file with one row for each run.

Return value

Returns int. 1 for the first run. Each retry from retries: adds one.

Examples

A log file for each run

<?nvs
// Shows a hook that keeps the log of each failed run. The attempt number is part
// of the file name, so a retry does not write over the log of the first run.
use Core\IO;
use Core\Path;
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class RunLog
{
public static function fileName(string $test, int $attempt): string
{
return $test . '-attempt-' . $attempt . '.log';
}
}
class PaymentTest
{
public string $log;
public function constructor()
{
$this->log = Path::fromWorkingDirectory('payment-test.log');
}
// A payment service on the network can be slow, so this test may run 3 times.
#[Test(retries: 2, because: 'the payment sandbox is sometimes slow')]
public function itChargesTheCard(): void
{
IO::write($this->log, "charge 1990 cents\n");
Test::assertSame(1990, 1990);
}
// `attempt()` is `1` for the first run and `2` for the first retry.
#[AfterEachTest]
public function keepTheLog(Outcome $outcome): void
{
if ($outcome->hasPassed()) {
IO::remove($this->log);
} else {
IO::move($this->log, Path::fromWorkingDirectory(RunLog::fileName($outcome->name(), $outcome->attempt())));
}
}
}
// This prints `itChargesTheCard-attempt-2.log`.
echo RunLog::fileName('itChargesTheCard', 2), "\n";
Output
itChargesTheCard-attempt-2.log

A warning for a test that needed a retry

<?nvs
// Shows a hook that prints a warning when a test passed only after a retry. Such
// a test is called flaky, and it is worth fixing before it fails.
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class Warning
{
// A passed run with an attempt number above `1` needed a retry.
public static function forRun(string $test, int $attempt, bool $passed): ?string
{
if ($passed && $attempt > 1) {
return $test . ' passed on attempt ' . $attempt;
}
return null;
}
}
class MailTest
{
#[Test(retries: 1, because: 'the local mail server starts slowly')]
public function itSendsTheReceipt(): void
{
Test::assertSame('receipt sent', 'receipt sent');
}
#[AfterEachTest]
public function warnAboutRetries(Outcome $outcome): void
{
?string $warning = Warning::forRun($outcome->name(), $outcome->attempt(), $outcome->hasPassed());
if ($warning != null) {
echo 'warning: ', $warning, "\n";
}
}
}
// This prints `itSendsTheReceipt passed on attempt 2`.
echo Warning::forRun('itSendsTheReceipt', 2, true) ?? 'no warning', "\n";
Output
itSendsTheReceipt passed on attempt 2

A csv row for each run

<?nvs
// Shows a hook that adds one CSV row for each run of each test. A spreadsheet of
// these rows shows which tests need retries most often.
use Core\IO;
use Core\Path;
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class RunRow
{
public static function csv(string $class, string $test, int $attempt, bool $passed): string
{
return $class . ',' . $test . ',' . $attempt . ',' . ($passed ? 'passed' : 'failed') . "\n";
}
}
class StockTest
{
#[Test(retries: 2, because: 'the warehouse service is shared with other teams')]
public function itReservesTheItems(): void
{
Test::assertSame(12 - 3, 9);
}
#[AfterEachTest]
public function addRow(Outcome $outcome): void
{
string $row = RunRow::csv($outcome->class(), $outcome->name(), $outcome->attempt(), $outcome->hasPassed());
IO::append(Path::fromWorkingDirectory('test-runs.csv'), $row);
}
}
// This prints `StockTest,itReservesTheItems,2,passed`.
echo RunRow::csv('StockTest', 'itReservesTheItems', 2, true);
Output
StockTest,itReservesTheItems,2,passed