Overview
Lightweight, zero-dependency unit testing and assertion framework for HolyPHP with clear diff-style failure messages.
bundled — works offline
zero dependencies
assert
unit
Quick start
import "test";
$suite = test_suite("Math");
$suite->test("addition works", function () {
assert_eq(2 + 2, 4);
assert_neq(2 + 2, 5);
});
$suite->test("division by zero throws", function () {
assert_throws(function () {
throw new Exception("division by zero");
}, "division by zero");
});
$suite->assertAllPassed();
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install test
This adds the library to hphp.yaml, installs it into .hphp/packages/test/ and pins the exact version in hphp.lock:
require:
test: "^1.0"
Use it
import "test";
test is bundled with the compiler, so import "test"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install test # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install test
Documentation
API Reference
Suite Creation
test_suite(string $name, bool $verbose = true): TestSuite Creates and returns a new TestSuite. Throws if $name is empty or whitespace-only.
TestSuite Class
new TestSuite(string $name, bool $verbose = true) Initializes a suite. When $verbose is true, each test prints [PASS] or [FAIL] as it runs.
$suite->test(string $name, $fn): void Runs $fn(). Records a pass if no exception is thrown, or a fail with the exception message otherwise.
$suite->passedCount(): int — Number of passed tests.
$suite->failedCount(): int — Number of failed tests.
$suite->totalCount(): int — Total number of tests run.
$suite->failures(): array — Array of ["name" => string, "error" => string] failure records.
$suite->summary(): bool — Prints a formatted pass/fail summary and returns true if all tests passed.
$suite->assertAllPassed(): void — Calls summary() and throws an Exception if any test failed.
Assertions
All assertions accept an optional $message parameter and throw Exception with diff-style output on failure:
assert_eq($actual, $expected, string $message = ""): void Asserts $actual equals $expected (supports scalars, null, booleans, and arrays).
assert_neq($actual, $unexpected, string $message = ""): void Asserts $actual does not equal $unexpected.
assert_true($value, string $message = ""): void Asserts $value is strictly boolean true.
assert_false($value, string $message = ""): void Asserts $value is strictly boolean false.
assert_null($value, string $message = ""): void Asserts $value is null.
assert_not_null($value, string $message = ""): void Asserts $value is not null.
assert_contains($haystack, $needle, string $message = ""): void Asserts string $haystack contains substring $needle, or array $haystack contains element $needle.
assert_throws($fn, string $expectedSubstring = "", string $message = ""): string Asserts $fn() throws an Exception (and optionally that its message contains $expectedSubstring). Returns the caught exception message.
Error Handling
All validation and assertion errors throw new Exception("test: ...") with actionable details:
- Empty suite name:
test: suite name must be a non-empty string
- Empty test name:
test: test case name must be a non-empty string
- Invalid haystack in
assert_contains: test: assert_contains expects haystack to be a string or array, got ...
- Failed assertions include diff-style
expected: / actual: lines, e.g.:
test: assertion failed (assert_eq): expected: 4 actual: 5
Examples
01Quick start
Bundled with the compiler — import "test"; works offline in any file, or add it to a project with hphp install test.
import "test";
$suite = test_suite("Math");
$suite->test("addition works", function () {
assert_eq(2 + 2, 4);
assert_neq(2 + 2, 5);
});
$suite->test("division by zero throws", function () {
assert_throws(function () {
throw new Exception("division by zero");
}, "division by zero");
});
$suite->assertAllPassed();
Try ithphp install test
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_test.hphp.