Spectacular gives your Rust tests three layers of hooks that stack in a predictable order:
| Layer | Runs once per... | Runs per test |
|---|---|---|
| Suite | binary (before) |
test (before_each / after_each) |
| Group | group (before / after) |
test (before_each / after_each) |
| Test | -- | the test body |
Installation
[]
= "0.1"
Quick Start
RSpec-style DSL
use spec;
spec!
Attribute style
use ;
Hooks
Group hooks
Group hooks run within a single test module. before runs once before the first test; after runs once after the last test. before_each and after_each run around every test.
use spec;
use ;
static READY: AtomicBool = new;
spec!
Suite hooks (3-layer)
Suite hooks run across all opted-in groups. Place suite! as a sibling of your test groups, then opt in with suite; (DSL) or #[test_suite(suite)] (attribute style):
use ;
use ;
static DB_READY: AtomicBool = new;
suite!
spec!
Groups without suite; skip the suite layer entirely -- no runtime cost, no coupling.
Hook Execution Order
For each test in a group that opts into suite hooks:
suite::before (Once -- first test in binary triggers it)
group::before (Once -- first test in group triggers it)
suite::before_each
group::before_each
TEST
group::after_each
suite::after_each
group::after (countdown -- last test in group triggers it)
After-hooks are protected by catch_unwind, so cleanup runs even if a test panics.
Attribute Style Reference
| Attribute | Description |
|---|---|
#[test_suite] |
Marks a module as a test group |
#[test_suite(suite)] |
Same, with suite hook opt-in |
#[test_case] |
Marks a function as a test |
#[before] |
Once-per-group setup (max one per module) |
#[after] |
Once-per-group teardown (max one per module) |
#[before_each] |
Per-test setup (max one per module) |
#[after_each] |
Per-test teardown (max one per module) |
License
MIT -- see LICENSE for details.