Home

Awesome

Nette Tester

Downloads this Month Tests Latest Stable Version License

 <!---->

Introduction

Nette Tester is a productive and enjoyable unit testing framework. It's used by the Nette Framework and is capable of testing any PHP code.

Documentation is available on the Nette Tester website. Read the blog for new information.

 <!---->

Support Tester

Do you like Nette Tester? Are you looking forward to the new features?

Buy me a coffee

Thank you!

 <!---->

Installation

The recommended way to install Nette Tester is through Composer:

composer require nette/tester --dev

Alternatively, you can download the tester.phar file.

Nette Tester 2.5 is compatible with PHP 8.0 to 8.4. Collecting and processing code coverage information depends on Xdebug or PCOV extension, or PHPDBG SAPI.

 <!---->

Writing Tests

Imagine that we are testing this simple class:

class Greeting
{
	function say($name)
	{
		if (!$name) {
			throw new InvalidArgumentException('Invalid name.');
		}
		return "Hello $name";
	}
}

So we create test file named greeting.test.phpt:

require 'src/bootstrap.php';

use Tester\Assert;

$h = new Greeting;

// use an assertion function to test say()
Assert::same('Hello John', $h->say('John'));

Thats' all!

Now we run tests from command-line using the tester command:

> tester
 _____ ___  ___ _____ ___  ___
|_   _/ __)( __/_   _/ __)| _ )
  |_| \___ /___) |_| \___ |_|_\  v2.5

PHP 8.2.0 | php -n | 8 threads
.
OK (1 tests, 0 skipped, 0.0 seconds)

Nette Tester prints dot for successful test, F for failed test and S when the test has been skipped.

 <!---->

Assertions

This table shows all assertions (class Assert means Tester\Assert):

Testing exceptions:

Assert::exception(function () {
	$h = new Greeting;
	$h->say(null);
}, InvalidArgumentException::class, 'Invalid name.');

Testing PHP errors, warnings or notices:

Assert::error(function () {
	$h = new Greeting;
	echo $h->abc;
}, E_NOTICE, 'Undefined property: Greeting::$abc');

Testing private access methods:

$h = new Greeting;
Assert::with($h, function () {
	// normalize() is internal private method.
	Assert::same('Hello David', $this->normalize('Hello david')); // $this is Greeting
});

 <!---->

Tips and features

Running unit tests manually is annoying, so let Nette Tester to watch your folder with code and automatically re-run tests whenever code is changed:

tester -w /my/source/codes

Running tests in parallel is very much faster and Nette Tester uses 8 threads as default. If you wish to run the tests in series use:

tester -j 1

How do you find code that is not yet tested? Use Code-Coverage Analysis. This feature requires you have installed Xdebug or PCOV extension, or you are using PHPDBG SAPI. This will generate nice HTML report in coverage.html.

tester . -c php.ini --coverage coverage.html --coverage-src /my/source/codes

We can load Nette Tester using Composer's autoloader. In this case it is important to setup Nette Tester environment:

require 'vendor/autoload.php';

Tester\Environment::setup();

We can also test HTML pages. Let the template engine generate HTML code or download existing page to $html variable. We will check whether the page contains form fields for username and password. The syntax is the same as the CSS selectors:

$dom = Tester\DomQuery::fromHtml($html);

Assert::true($dom->has('input[name="username"]'));
Assert::true($dom->has('input[name="password"]'));

For more inspiration see how Nette Tester tests itself.

 <!---->

Running tests

The command-line test runner can be invoked through the tester command (or php tester.php). Take a look at the command-line options:

> tester

Usage:
    tester [options] [<test file> | <directory>]...

Options:
    -p <path>                    Specify PHP interpreter to run (default: php).
    -c <path>                    Look for php.ini file (or look in directory) <path>.
    -C                           Use system-wide php.ini.
    -l | --log <path>            Write log to file <path>.
    -d <key=value>...            Define INI entry 'key' with value 'val'.
    -s                           Show information about skipped tests.
    --stop-on-fail               Stop execution upon the first failure.
    -j <num>                     Run <num> jobs in parallel (default: 8).
    -o <console|console-lines|tap|junit|none>
                                 Specify output format.
    -w | --watch <path>          Watch directory.
    -i | --info                  Show tests environment info and exit.
    --setup <path>               Script for runner setup.
    --temp <path>                Path to temporary directory. Default by sys_get_temp_dir().
    --colors [1|0]               Enable or disable colors.
    --coverage <path>            Generate code coverage report to file.
    --coverage-src <path>        Path to source code.
    -h | --help                  This help.