Skip to content

Core\Test\Outcome::class

$outcome->class(): string

Outcome::class() returns the name of the test class that ran, with its namespace. For a class CartTest in the namespace Shop\Tests, the result is Shop\Tests\CartTest.

You get an Outcome as the parameter of a method with #[AfterEachTest]. The test runner creates it after each run of each test and passes it to that method. You cannot create an Outcome yourself, and your own class cannot extend it.

Use class() with name() to build a label for one test, such as a log file name or a line in a report.

The examples below show a label for each test, the class name without its namespace, and one log file for each test class.

Return value

Returns string. The class name, with its namespace.

Examples

A label for each test

<?nvs
// Shows a hook that prints a label for each failed test. The label is the class
// name and the test name, so you can search for it in your code.
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class Label
{
// This joins the class name and the test name with `::`.
public static function of(string $class, string $test): string
{
return $class . '::' . $test;
}
}
class Cart
{
public static function total(array<int> $prices): int
{
int $sum = 0;
foreach ($prices as int $price) {
$sum = $sum + $price;
}
return $sum;
}
}
class CartTest
{
#[Test]
public function itAddsThePrices(): void
{
Test::assertSame(Cart::total([450, 1200]), 1650);
}
// `class()` returns the name of this test class.
#[AfterEachTest]
public function printLabel(Outcome $outcome): void
{
if (!$outcome->hasPassed()) {
echo 'failed: ', Label::of($outcome->class(), $outcome->name()), "\n";
}
}
}
// This prints `CartTest::itAddsThePrices`.
echo Label::of('CartTest', 'itAddsThePrices'), "\n";
Output
CartTest::itAddsThePrices

The class name without its namespace

<?nvs
// Shows how to remove the namespace from the class name. `class()` returns the
// name with its namespace, such as `Shop\Tests\OrderTest`.
namespace Shop\Tests;
use Core\Arr;
use Core\Str;
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class ShortName
{
// The last part after a backslash is the class name alone.
public static function of(string $class): string
{
return Arr::last(Str::split($class, "\\")) ?? $class;
}
}
class OrderTest
{
#[Test]
public function itCountsTheItems(): void
{
Test::assertCount(['tea', 'cake', 'bread'], 3);
}
#[AfterEachTest]
public function printResult(Outcome $outcome): void
{
echo ShortName::of($outcome->class()), ' ', $outcome->name(), "\n";
}
}
// This prints `OrderTest`.
echo ShortName::of("Shop\\Tests\\OrderTest"), "\n";
Output
OrderTest

One log file for each test class

<?nvs
// Shows a hook that writes the result of each test to a log file. Each test
// class has its own file, so a build server can show one file per class.
namespace Shop\Tests;
use Core\IO;
use Core\Path;
use Core\Str;
use Core\Test;
use Core\Test\AfterEachTest;
use Core\Test\Outcome;
class TestLog
{
// A backslash is not allowed in a file name on every system, so it becomes `-`.
public static function fileFor(string $class): string
{
return 'logs/' . Str::replace($class, "\\", '-') . '.log';
}
}
class InvoiceTest
{
#[Test]
public function itAddsTheTax(): void
{
// The prices are in cents. The tax is 20%.
Test::assertSame(1000 + 200, 1200);
}
// `class()` returns `Shop\Tests\InvoiceTest` here.
#[AfterEachTest]
public function writeLog(Outcome $outcome): void
{
string $line = $outcome->name() . ' ' . ($outcome->hasPassed() ? 'passed' : 'failed') . "\n";
IO::append(Path::fromWorkingDirectory(TestLog::fileFor($outcome->class())), $line);
}
}
// This prints `logs/Shop-Tests-InvoiceTest.log`.
echo TestLog::fileFor("Shop\\Tests\\InvoiceTest"), "\n";
Output
logs/Shop-Tests-InvoiceTest.log