Skip to content

Core\Test\Outcome::name

$outcome->name(): string

Outcome::name() returns the name of the test method that ran. For a test method itAddsAnItem, the result is itAddsAnItem.

You get an Outcome as the parameter of a method with #[AfterEachTest]. The test runner creates it after each run of each test. You cannot create one yourself.

A test with #[TestWith] rows runs once per row. Each run has the row number after the name, the same way the test report prints it: itRoundsThePrice#0, itRoundsThePrice#1 and so on.

The examples below show a file named after the test, the name of a test with rows, and a file that lists the tests that ran.

Return value

Returns string. The method name. A test with #[TestWith] rows has #0, #1 and so on after the name, as the report prints it.

Examples

A file named after the test

<?nvs
// Shows a hook that keeps the page a failed test produced. The file is named
// after the test, so you can find the page of each test.
use Core\IO;
use Core\Path;
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class Page
{
public static function title(string $product): string
{
return '<h1>' . $product . '</h1>';
}
// Each test gets its own file, such as `pages/itShowsTheTitle.html`.
public static function fileFor(string $test): string
{
return 'pages/' . $test . '.html';
}
}
class ProductPageTest
{
public string $page = '';
#[Test]
public function itShowsTheTitle(): void
{
$this->page = Page::title('Green tea');
Test::assertSame($this->page, '<h1>Green tea</h1>');
}
// `name()` returns the name of the test method, here `itShowsTheTitle`.
#[AfterEachTest]
public function keepThePage(Outcome $outcome): void
{
if (!$outcome->hasPassed()) {
IO::write(Path::fromWorkingDirectory(Page::fileFor($outcome->name())), $this->page);
}
}
}
// This prints `pages/itShowsTheTitle.html`.
echo Page::fileFor('itShowsTheTitle'), "\n";
Output
pages/itShowsTheTitle.html

The name of a test with rows

<?nvs
// Shows the name of a test with `#[TestWith]` rows. Each row runs once, and its
// name ends with `#` and the row number, such as `itCountsTheCents#1`.
use Core\Str;
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
use Core\Test\TestWith;
class TestName
{
// This removes the row number, so all rows of one test have the same name.
public static function withoutRow(string $name): string
{
return Str::before($name, '#') ?? $name;
}
}
class Price
{
public static function inCents(int $euros, int $cents): int
{
return $euros * 100 + $cents;
}
}
class PriceTest
{
#[TestWith(euros: 2, cents: 50, want: 250)]
#[TestWith(euros: 0, cents: 99, want: 99)]
#[Test]
public function itCountsTheCents(int $euros, int $cents, int $want): void
{
Test::assertSame(Price::inCents($euros, $cents), $want);
}
#[AfterEachTest]
public function printTheTest(Outcome $outcome): void
{
echo TestName::withoutRow($outcome->name()), ' ', $outcome->hasPassed() ? 'passed' : 'failed', "\n";
}
}
// This prints `itCountsTheCents`.
echo TestName::withoutRow('itCountsTheCents#1'), "\n";
Output
itCountsTheCents

A file that lists the tests that ran

<?nvs
// Shows a hook that adds the name of each test to a file. After the run, the
// file lists every test that ran, one per line.
use Core\IO;
use Core\Path;
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class Slug
{
public static function of(string $title): string
{
return Core\Str::lower(Core\Str::replace($title, ' ', '-'));
}
}
class RunList
{
public static function line(string $class, string $test): string
{
return $class . ' ' . $test . "\n";
}
}
class SlugTest
{
#[Test]
public function itUsesLowerCase(): void
{
Test::assertSame(Slug::of('Tea'), 'tea');
}
#[Test]
public function itReplacesSpaces(): void
{
Test::assertSame(Slug::of('green tea'), 'green-tea');
}
// `append` adds one line to the end of the file for each test.
#[AfterEachTest]
public function addToTheList(Outcome $outcome): void
{
IO::append(Path::fromWorkingDirectory('tests-that-ran.txt'), RunList::line($outcome->class(), $outcome->name()));
}
}
// This prints `SlugTest itReplacesSpaces`.
echo RunList::line('SlugTest', 'itReplacesSpaces');
Output
SlugTest itReplacesSpaces