Skip to content

Latest commit

 

History

History
79 lines (60 loc) · 1.83 KB

cli-writing-tests.md

File metadata and controls

79 lines (60 loc) · 1.83 KB

Writing CLI Tests

You can write tests that can be run by the CLI by using the QuickCheck\Test functions. This class is only meant to be used to set up tests that will be run by the CLI. To run tests programmatically use the QuickCheck\Property API.

Test::forAll

static Test::forAll ( array $generators, callable $predicate [, int $numTests = 100 [, int $maxSize = 200 ]] ) : void

A single test per file can be defined with QuickCheck\Test::forAll.

use QuickCheck\Generator as Gen;
use QuickCheck\Test;

Test::forAll(
    [Gen::asciiStrings()],
    function($s) {
        return !is_numeric($s);
    },
    1000
);

Test::check

static Test::check ([ string $name = null ]) : Check

You can write multiple tests per file by using the QuickCheck\Test::check function. It returns a QuickCheck\Check instance.

QuickCheck\Check

The Check instance is a simple builder that allows you to fluently configure the test:

Check::times ( int $n ) : Check
Check::maxSize ( int $n ) : Check
Check::numTests ( int $n ) : Check
Check::forAll ( array $generators, callable $predicate ) : void

Note that Check::forAll will register the test in the suite and thus returns void.

Examples

use QuickCheck\Generator as Gen;
use QuickCheck\Test;

Test::check('is commutative')
    ->forAll(
        [Gen::ints(), Gen::ints()],
        function($a, $b) {
            return $a + $b === $b + $a;
        });

Test::check('has zero as identity')
    ->forAll(
        [Gen::ints()],
        function($x) {
            return $x + 0 === $x;
        });

Test::check('is associative')
    ->times(1000)
    ->maxSize(1337)
    ->forAll(
        [Gen::ints(), Gen::ints(), Gen::ints()],
        function($a, $b, $c) {
            return ($a + $b) + $c == $a + ($b + $c);
        });