Skip to content

nvs test --mutate

nvs test --mutate checks whether your tests notice when a line of your program is wrong.

Novis runs your tests once. Then it changes your program in one place, such as >= to >. This changed copy is a mutant. The tests that ran that line run again. If a test fails, the mutant is killed. If every test passes, the mutant survived. That means no test checks the result of that line.

The report lists the survived mutants by method. The mutation score is the percentage of mutants that made a test fail. It counts only the mutants on lines that a test ran.

--mutate=Shop\Cart makes mutants only in that class. --since main makes mutants only on the lines you edited since the git branch main. A class or method with #[NoMutate] gets no mutants.

In plain words: you hide a small mistake in your code and see if your tests find it.

Good to know: every test must pass before the mutants run. A survived mutant does not make the command fail.

The examples below show a mutant that survives, the test that kills it, code that no test calls, and a class with #[NoMutate].

Examples

A test that misses the boundary

<?nvs
// Shows a mutant that survives because no test checks the boundary.
// Orders of 50.00 or more ship free. The tests use 20.00 and 80.00 only.
// So when `>=` changes to `>`, every test still passes.
// proof: test --mutate
use Core\Test;
class Shipping
{
// Prices are in cents.
public static function total(int $order): int
{
if ($order >= 5000) {
return $order;
}
return $order + 495;
}
}
final class ShippingTest
{
#[Test]
public function aSmallOrderPaysForShipping(): void
{
Test::assertSame(Shipping::total(2000), 2495);
}
#[Test]
public function aLargeOrderShipsFree(): void
{
Test::assertSame(Shipping::total(8000), 8000);
}
}
Output
ShippingTest
✓ aSmallOrderPaysForShipping
✓ aLargeOrderShipsFree
0 failed, 2 passed, 0 skipped, 0 flaky
5 of 5 lines run (100.0%)
3 mutants: each one runs the tests that reach its line
compiles: 1, for the 3 mutants that ran tests
mutants: 2 killed, 1 survived, 0 timed out, 0 not reached
mutation score: 66.7% (2 of 3 reached mutants made a test fail or time out)
survived in Shipping::total (1):
docs/examples/tools/cli/nvs-test-mutate/01-a-test-that-misses-the-boundary.nvs:13 `>=` to `>` mutant = "Shipping::total#1: `>=` to `>`"

A boundary test kills every mutant

<?nvs
// Shows how one more test kills the mutant that survived before.
// The new test uses an order of exactly 50.00. The mutation score is now 100%.
// proof: test --mutate
use Core\Test;
class Shipping
{
// Prices are in cents.
public static function total(int $order): int
{
if ($order >= 5000) {
return $order;
}
return $order + 495;
}
}
final class ShippingTest
{
#[Test]
public function aSmallOrderPaysForShipping(): void
{
Test::assertSame(Shipping::total(2000), 2495);
}
#[Test]
public function aLargeOrderShipsFree(): void
{
Test::assertSame(Shipping::total(8000), 8000);
}
// This test fails if `>=` changes to `>`.
#[Test]
public function anOrderOfExactlyFiftyShipsFree(): void
{
Test::assertSame(Shipping::total(5000), 5000);
}
}
Output
ShippingTest
✓ aSmallOrderPaysForShipping
✓ aLargeOrderShipsFree
✓ anOrderOfExactlyFiftyShipsFree
0 failed, 3 passed, 0 skipped, 0 flaky
6 of 6 lines run (100.0%)
3 mutants: each one runs the tests that reach its line
compiles: 1, for the 3 mutants that ran tests
mutants: 3 killed, 0 survived, 0 timed out, 0 not reached
mutation score: 100.0% (3 of 3 reached mutants made a test fail or time out)

Code no test calls

<?nvs
// Shows the mutant in a function that no test calls.
// The tests call `isStrong` only, so the mutant in `isTooShort` is `not reached`.
// It does not lower the score. Add a test for `isTooShort` to check it.
// proof: test --mutate
use Core\Str;
use Core\Test;
class Password
{
public static function isStrong(string $password): bool
{
return Str::length($password) >= 12;
}
public static function isTooShort(string $password): bool
{
return Str::length($password) < 8;
}
}
final class PasswordTest
{
#[Test]
public function twelveCharactersAreStrong(): void
{
Test::assertSame(Password::isStrong('abcdefghijkl'), true);
}
#[Test]
public function elevenCharactersAreNotStrong(): void
{
Test::assertSame(Password::isStrong('abcdefghijk'), false);
}
}
Output
PasswordTest
✓ twelveCharactersAreStrong
✓ elevenCharactersAreNotStrong
0 failed, 2 passed, 0 skipped, 0 flaky
3 of 4 lines run (75.0%)
2 mutants: each one runs the tests that reach its line
compiles: 1, for the 1 mutant that ran tests
mutants: 1 killed, 0 survived, 0 timed out, 1 not reached
mutation score: 100.0% (1 of 1 reached mutants made a test fail or time out)

No mutate on a view

<?nvs
// Shows `#[NoMutate]`, which keeps a class out of a mutation run.
// `PriceLabel` gets no mutants. The report counts only the mutants in the `Discount` class.
// proof: test --mutate
use Core\Test;
use Core\Test\NoMutate;
class Discount
{
// Prices are in cents. An order of 100.00 or more gets 10.00 off.
public static function apply(int $order): int
{
if ($order >= 10000) {
return $order - 1000;
}
return $order;
}
}
// `because:` is the reason, for a person who reads the code. It must not be empty.
#[NoMutate(because: 'only the text of a label, which a person checks on the page')]
class PriceLabel
{
public static function show(int $cents): string
{
if ($cents >= 10000) {
return 'big order';
}
return 'order';
}
}
final class DiscountTest
{
#[Test]
public function aSmallOrderPaysTheFullPrice(): void
{
Test::assertSame(Discount::apply(9999), 9999);
}
#[Test]
public function anOrderOfExactlyOneHundredGetsTheDiscount(): void
{
Test::assertSame(Discount::apply(10000), 9000);
}
#[Test]
public function theLabelNamesTheOrder(): void
{
Test::assertSame(PriceLabel::show(500), 'order');
}
}
Output
DiscountTest
✓ aSmallOrderPaysTheFullPrice
✓ anOrderOfExactlyOneHundredGetsTheDiscount
✓ theLabelNamesTheOrder
0 failed, 3 passed, 0 skipped, 0 flaky
8 of 9 lines run (88.9%)
3 mutants: each one runs the tests that reach its line
compiles: 1, for the 3 mutants that ran tests
mutants: 3 killed, 0 survived, 0 timed out, 0 not reached
mutation score: 100.0% (3 of 3 reached mutants made a test fail or time out)

Changelog

VersionChange
0.0.5nvs test --mutate=Shop\Cart makes mutants only in the code you name, and #[NoMutate] keeps a class or a method out. --since main makes mutants only on the lines you edited, and a run compiles the program once. Survivors you checked go in mutants.toml, and the report lists the others by method.