A simple test suite for PHP 5.3+, partially inspired by QUnit.
- Simple to write tests and get output – start writing tests fast
- Short, straightforward syntax
- Can be run from the CLI – no web server required
- Fancy colored output in terminal
<?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.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.php
file for more, or try running it with php example.php
-
FUnit::test($name, \Closure $test)
Add a test with the name $name and an anonymous function $test. $test would contain various assertions, likeFUnit::ok()
-
FUnit::ok($a, $msg = null)
Assert that $a is truthy. Optional $msg describes the test -
FUnit::equal($a, $b, $msg = null)
Assert that $a == $b. Optional $msg describes the test -
FUnit::not_equal($a, $b, $msg = null)
Assert that $a != $b. Optional $msg describes the test -
FUnit::strict_equal($a, $b, $msg = null)
Assert that $a === $b. Optional $msg describes the test -
FUnit::not_strict_equal($a, $b, $msg = null)
Assert that $a !== $b. Optional $msg describes the test -
FUnit::has($needle, $haystack, $msg = null)
Assert that an array or object ($haystack
) has a key or property ($needle
) -
FUnit::fail($msg = null, [$expected = null])
Force a failed assertion. If$expected === true
, it's marked as an expected failure -
FUnit::expect_fail($msg = null)
Assets an expected failure. Equivalent toFUnit::fail('msg', true)
-
FUnit::setup(\Closure $setup)
Register a function to run at the start of each test. SeeFUnit::fixture()
-
FUnit::teardown(\Closure $setup)
Register a function to run at the end of each test. SeeFUnit::fixture()
andFUnit::reset_fixtures()
-
FUnit::fixture($key, [$val])
Retrieve or register a fixture. Use this in FUnit::setup() to assign fixtures to keys, and retrieve those fixtures in your tests -
FUnit::reset_fixtures()
Clears out all fixtures in the FUnit::$fixtures array. This doesn't guarantee clean shutdown/close -
FUnit::run($report = true)
Runs the registered tests. Iffalse
is passed, the report output is suppressed
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');
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();
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';
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
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');
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