Writing Tests
A Provium test file is a sequence of test(...) calls. The harness runs them
in declaration order, gives each one a fresh t context, and records pass /
fail / skip. This page covers the patterns for getting the most out of that
loop.
The exhaustive reference is on test framework and meta tags.
Anatomy of a test file #
-- tests/file_handles.test.lua
provium. = "10s" -- file-default per-test timeout
test
test
Two things to know up front:
- Each
test()body runs in its own scope. Theprovium:vm("v", …)calls in the two tests above each create their own VM in their test scope; they're independent and do not share state. The VMs are silently shut down at test end. To share a VM across tests, declare it at file scope (top-levellocal vm = provium:vm("v", "peios"):boot()) and look it up by name (provium:vm("v")) or capture the userdata as a Lua local. - Each test gets a fresh
tcontext.t.nameandt.metaare per-test; assertions and skips are scoped to the running test.
For the full scoping rules (lookup fallthrough, shadow detection, fixtures), see labs and scope.
Naming tests #
Test names must be unique within a file. The harness raises at registration time on a duplicate:
test: duplicate name `boots` in this file
Pick names that read like the assertion, not the implementation.
"boots and runs uname" is better than "test_boot_uname", the renderer
prefixes them with status (PASS, FAIL) and indents under the file path, so
the name reads as a sentence.
Assertions #
The t context exposes:
| Assertion | Use when |
|---|---|
t:assert(cond, msg?) | You're checking any boolean condition. |
t:assert_eq(a, b, msg?) | Two values must be equal. The error message includes both values. |
t:assert_neq(a, b, msg?) | Two values must be unequal. |
t:assert_contains(haystack, needle, msg?) | A string must appear inside another string. Both args must be strings. |
t:assert_raises(fn, msg?) | Calling fn must raise; returns the error value. |
t:fail(msg?) | You've decided the test failed for reasons that don't fit an assertion. |
Every assertion that fires raises (with error(..., 2) so the call site, not
the assertion implementation, is in the message). The harness catches the
raise and records the test as Failed.
Even when you wrap a body in pcall and swallow the error, the harness still
detects the failure, an assertion marks the test failed before it raises, and
that mark is sticky:
test
Use t:assert_raises(fn) if you want the inverse, "this should raise":
test
Skipping #
Three ways to skip:
Inline (t:skip(reason)) #
test
t:skip raises an internal sentinel that the harness treats as Skipped.
Useful when the skip condition can only be evaluated at runtime.
Declarative ({skip = …}) #
test
test
The body never runs. Cleaner than inline when the test is permanently disabled or pending an unrelated change.
File-scope (todo("reason")) #
todo
test
test
Every registered test is reported Skipped with the given reason. Use this when an entire test file is non-applicable temporarily, do not delete the tests, mark the file pending.
Logging diagnostic data #
t:log(msg) appends to a per-test log array. The harness includes these
messages in the file outcome and the TestPassed / TestFailed events,
useful for diagnostic context when something goes wrong:
test
Avoid print in tests, print writes to stdout and gets interleaved with the
harness's own output, while t:log is structured and tied to the specific
test.
Per-test metadata #
The optional second arg to test(...) is a metadata table. Provium inspects a
handful of well-known keys:
| Key | Purpose |
|---|---|
slow = true | Skip unless --include-slow is passed. |
skip = … | Declarative skip. |
tags = {...} | Tag-based filtering (--tag, --no-tag). |
timeout = "30s" | Per-test wall-clock timeout. |
spec = "PSD-…" | Spec linkage for provium-coverage. |
Anything else passes through to event consumers. See meta tags for the full reference.
test
Polling with wait_until #
wait_until(predicate, opts?) calls predicate repeatedly until it returns
truthy. Use it for guest-side conditions that don't have a stream interface:
test
For things that produce a stream (logs, console output, captured stdout),
prefer :expect on the stream over wait_until, it gets event-driven
semantics and much tighter feedback.
File-default timeouts #
Set provium.timeout at file scope to put a default on every test:
provium. = "30s"
test -- 30s
test -- per-test wins; 5m
Per-test meta.timeout always wins over the file default. When a per-test
timeout fires, the watchdog tears down the entire file's lab, there's no
finer-grained cancellation in v1. See
time and timeouts for
the scope-limitation note.
Reset-between-tests #
Set provium.reset_between_tests = true at file scope to take a baseline
snapshot after the file's top-level chunk and restore it between every test:
provium. = true
local vm = provium::
vm: -- snapshot baseline includes this
test
test
Mutually exclusive with file-scope open streams. Opening a tail_file,
console:read, bridge:capture, and so on at top-level errors at chunk load
with the offending stream's creation site named. Move stream opens into
test() bodies.
Common patterns #
One-test-per-VM #
When tests are independent and a fresh VM per test is acceptable, opt into reset-between-tests:
provium. = true
test
One-VM-many-tests, ordered #
When tests build on each other, leave reset_between_tests off and let state
accumulate:
local vm = provium::
test
test
Order matters here. If you delete the first test, the second will fail; that's intentional.
Fixture-backed setup #
When the setup is expensive (install packages, fetch data, build a config),
put it in a *.fixture.lua file and call provium:vm_fixture("name"):
-- tests/fixtures/corpus.fixture.lua
local vm = provium::
vm::
return vm:
-- tests/uses-corpus.test.lua
test
The fixture is built once, cached on disk, and restored per test that asks for it. See fixtures and dependencies for the cache lifecycle.
See also #
- Test framework reference: every method
on
tand the helperswait_untilandtodo. - Meta tags reference: every well-known meta key.
- Labs and scope:
lab:claim,lab:barrier,reset_between_tests.