Core\Test\Outcome::hasPassed
$outcome->hasPassed(): boolOutcome::hasPassed() checks whether this run of the test passed. The result is true when it passed, and false when it failed.
You get an Outcome as the parameter of a method with #[AfterEachTest]. That method runs after each test, also when the test failed. You cannot create an Outcome yourself.
A common use is to clean up after a test that passed, and to keep files of a test that failed. You can then open those files and see what went wrong.
Good to know: a test with retries: runs again after a failed run. The result is about one run, so the first run can be false and the second true.
The examples below show a file kept only for a failed test, a status line for each test, and a report line for a build server.
Return value
Returns bool. true when the run passed, and false when it failed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Test::answerCli sets the input that each prompt reads in a test. Core\Test\Outcome->hasPassed() checks whether a test passed. The old names were scriptAnswers and passed. |
Examples
Keep the file of a failed test
<?nvs// Shows a hook that deletes the export file of a passed test and keeps the file// of a failed test. You can then open the file and see what went wrong.
use Core\IO;use Core\Path;use Core\Test;use Core\Test\AfterEachTest;use Core\Test\Outcome;
class Export{ public static function csv(array<string> $names): string { return "name\n" . Core\Str::join($names, "\n") . "\n"; }}
class ExportTest{ public string $file;
public function constructor() { $this->file = Path::fromWorkingDirectory('customers-export.csv'); }
#[Test] public function itWritesOneLinePerCustomer(): void { IO::write($this->file, Export::csv(['Ada', 'Linus'])); Test::assertSame(IO::readText($this->file), "name\nAda\nLinus\n"); }
// `hasPassed()` is `true` when the test passed. #[AfterEachTest] public function cleanUp(Outcome $outcome): void { if ($outcome->hasPassed()) { IO::remove($this->file); } }}
// This prints the CSV text that the test writes to the file.echo Export::csv(['Ada', 'Linus']);nameAdaLinusA status line for each test
<?nvs// Shows a hook that prints one status line for each test. The line starts with// `PASS` or `FAIL`, so a script can find the failed tests.
use Core\Test;use Core\Test\AfterEachTest;use Core\Test\Outcome;
class Status{ public static function line(bool $passed, string $test): string { return ($passed ? 'PASS ' : 'FAIL ') . $test; }}
class Voucher{ // The prices are in cents. A voucher never makes the price lower than 0. public static function apply(int $price, int $voucher): int { if ($voucher > $price) { return 0; } return $price - $voucher; }}
class VoucherTest{ #[Test] public function itNeverGoesBelowZero(): void { Test::assertSame(Voucher::apply(300, 500), 0); }
#[AfterEachTest] public function printStatus(Outcome $outcome): void { echo Status::line($outcome->hasPassed(), $outcome->name()), "\n"; }}
// This prints `FAIL itNeverGoesBelowZero`.echo Status::line(false, 'itNeverGoesBelowZero'), "\n";FAIL itNeverGoesBelowZeroA report line for a build server
<?nvs// Shows a hook that writes a JUnit XML line for each test. Many build servers// read this format and show the failed tests in red.
use Core\IO;use Core\Path;use Core\Test;use Core\Test\AfterEachTest;use Core\Test\Outcome;
class JUnit{ // A failed test gets a `<failure/>` element inside its `<testcase>`. public static function testCase(string $class, string $test, bool $passed): string { string $open = "<testcase classname=\"" . $class . "\" name=\"" . $test . "\""; if ($passed) { return $open . "/>\n"; } return $open . "><failure/></testcase>\n"; }}
class LoginTest{ #[Test] public function itAcceptsTheRightPassword(): void { Test::assertTrue(Core\Str::length('correct horse') > 8); }
#[AfterEachTest] public function report(Outcome $outcome): void { string $line = JUnit::testCase($outcome->class(), $outcome->name(), $outcome->hasPassed()); IO::append(Path::fromWorkingDirectory('junit-cases.xml'), $line); }}
// This prints the line of a failed test.echo JUnit::testCase('LoginTest', 'itAcceptsTheRightPassword', false);<testcase classname="LoginTest" name="itAcceptsTheRightPassword"><failure/></testcase>