Skip to content

nvs test

nvs test <paths> runs tests. A .nvs file is a program, and the command runs every method that has the #[Test] attribute. A directory with .nvs files runs as one program. Any other path is a .nvst case file, or a directory of such files. A case is one program with its expected output.

nvs test CartTest.nvs:42 runs the test that contains line 42. --filter <text> runs the tests whose name contains the text. --tag and --skip-tag select tests by their #[Test(tags: [...])]. --since main runs the tests that, in an earlier run, ran code you changed or deleted since the git branch main. When it cannot tell which tests a change affects, it runs every test. nvs test --help lists every flag, such as --fail-fast, --rerun-failed and --watch.

The exit status is not zero when a test failed. A skipped test is not a failure.

Good to know: --update writes the new text into each failed Core\Test::assertMatchesInline snapshot in the test's source file. Those tests are still reported as failed, so run the tests again.

Examples

Test a price rule

<?nvs
// Shows a file of tests for a price rule. `nvs test 01-test-a-price-rule.nvs` runs
// every method that has `#[Test]` and prints one line for each test.
// `nvs run` runs only the code outside the classes, so it prints `4500`.
use Core\Test;
class Coupon
{
public static function apply(int $price, int $amountOff): int
{
if ($amountOff > $price) {
return 0;
}
return $price - $amountOff;
}
}
final class CouponTest
{
#[Test]
public function takesTheAmountOffThePrice(): void
{
Test::assertSame(Coupon::apply(5000, 500), 4500);
}
// A coupon worth more than the price gives a price of 0, not a negative price.
#[Test]
public function neverGoesBelowZero(): void
{
Test::assertSame(Coupon::apply(300, 500), 0);
}
}
echo Coupon::apply(5000, 500), "\n";
Output
4500

Changelog

VersionChange
0.0.5nvs test CartTest.nvs:42 runs the test that contains line 42. nvs test --since main runs only the tests that reach code you changed since the git branch main.
0.0.6With [debug] store = true, nvs test records the result, the duration and the failure message of each test. nvs test and nvs run keep the last 64 KB they printed in development. The debug viewer shows both on the run's page.
0.0.6nvs test --since also runs the tests that reach a deleted or renamed file or method. When it cannot tell which tests a change affects, it runs every test.