test

Lightweight unit testing and assertion framework with diff-style failure messages.

v1.0.0Tooling & CLIbundledimport "test";
Overview Installation Documentation Examples

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
Packagetest
Version1.0.0 (follows the compiler)
Entry filetest.hphp
Sourcepackages/lib/test ↗

Documentation

API Reference

Suite Creation

TestSuite Class

Assertions

All assertions accept an optional $message parameter and throw Exception with diff-style output on failure:

Error Handling

All validation and assertion errors throw new Exception("test: ...") with actionable details:

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 it
hphp install test
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_test.hphp.