Home

Awesome

FUnit: The testing microframework for PHP

FUnit is a testing microframework for PHP 5.3+, partially inspired by QUnit. FUnit was created by Ed Finkler for Fictive Kin.

If you can code PHP, you can write tests with FUnit.

Features

Example

<?php
require __DIR__ . '/FUnit.php';
use \FUnit as fu;  // note the alias to "fu" for terseness

fu::test("this is a test", function() {
	fu::ok(1, "the integer '1' is okay");
	fu::ok(0, "the integer '0' is not okay"); // this will fail!
});

$exit_code = fu::run();
exit($exit_code);

Will output:

> php example_standalone.php
Running test 'this is a test...'
RESULTS:
--------------------------------------------
TEST: this is a test (1/2):
 * PASS ok() the integer '1' is okay
 * FAIL ok() the integer '0' is not okay

ERRORS/EXCEPTIONS: 0
TOTAL ASSERTIONS: 1 pass, 1 fail, 0 expected fail, 2 total
TESTS: 1 run, 0 pass, 1 total

See the example_standalone.php file for more, or try running it with php example_standalone.php

Alternately, if you load standalone_example.php in a web browser, it will output a very simple HTML version of the text report. If you're running PHP 5.4 or above, you can use the dev server to view it like so:

php -S 0.0.0.0:8000 example_standalone.php

And then open http://0.0.0.0:8000 in a web browser.

Methods

Test Suite Building Methods

Assertions

Utility Methods

Configuration Methods

Report formats

By default, FUnit outputs a colorful text output, formatted for the terminal. You can also output reports in xunit-style xml.

The report format is the third parameter of FUnit::run():

Example:

// Outputs a colored text report. This is the default format.
FUnit::run(true, null, 'text');

// Outputs xUnit-style xml
FUnit::run(true, null, 'xunit');

Browser output

The standard text report format will output as very simple HTML if the test file is loaded up through a web server. You can test this with the dev server if you're running PHP 5.4+:

php -S 0.0.0.0:8000 test_file.php

And then open http://0.0.0.0:8000 in a web browser.

CLI Test Runner Utility

FUnit was designed to not require a separate test runner tool, but it does come with one at bin/fu (or vendor/bin/fu if you've installed via Composer). fu allows you to run tests in a single file, a group of files in a directory, and filter what tests are run.

Examples:

Note: When fu loads multiple test files, it requires each one. That means all the code within each is executed. Calls to FUnit::run() are suppressed, but non-FUnit code (like exit() calls or require statements) could cause issues.

Installation

Install with Composer

If you're using Composer to manage dependencies, you can add FUnit with it.

{
	"require": {
		"funkatron/funit": "dev-master"
	}
}

Note that FUnit has not yet reached 1.0! That means BC may break!

If you install via Composer, you can use the auto-generated autoloader to load FUnit, like so:

<?php
require "vendor/autoload.php"
use \FUnit as fu;

fu::test("this is a test", function() {
    fu::ok(1, "the integer '1' is okay");
    fu::ok(0, "the integer '0' is not okay"); // this will fail!
});

fu::run();

Install source from GitHub

To install the source code:

git clone git://github.com/funkatron/FUnit.git

And include it in your scripts:

require_once '/path/to/FUnit/FUnit.php';

Install source from zip/tarball

Alternatively, you can fetch a tarball or zipball:

$ curl https://github.com/funkatron/FUnit/tarball/master | tar xzv
(or)
$ wget https://github.com/funkatron/FUnit/tarball/master -O - | tar xzv

Using a Class Loader

If you're using a class loader (e.g., Symfony Class Loader) for PSR-0-style class loading:

$loader->registerNamespace('FUnit', 'path/to/vendor/FUnit');

Upgrading

If you're using a version older than 0.5, the namespace/class name changed to follow PSR-0 autoloader standards. The base class is now \FUnit, not \FUnit\fu. You can still call all your methods with fu::XXX() by aliasing the namespace like so:

use \FUnit as fu