Expand description
Session- and module-scoped test fixtures with async init and teardown.
tpt-fixture fills the beforeAll / afterAll gap that rstest issue
#119 leaves open: it lets an init function run once per test scope and
share its resource (as a thread-safe Arc<T>) across every
test in that scope, tearing it down when the scope ends.
Use the companion tpt_fixture attribute macro to
declare fixtures and to inject them into tests:
use std::sync::Arc;
use tpt_fixture::tpt_fixture;
#[tpt_fixture(scope = "suite")]
async fn db() -> Database {
Database::connect().await
}
#[tpt_fixture]
#[tokio::test]
async fn reads_rows(db: Arc<Database>) {
assert!(db.row_count() > 0);
}§Scopes
| Scope | Initialised | Shared across | Teardown |
|---|---|---|---|
test | every test | — (fresh each call) | end of that test |
module | once | the test binary | shutdown() |
suite | once | the whole test binary | shutdown() |
module and suite are both process-lifetime singletons (Rust has no runtime
module identity), initialised exactly once via a global OnceLock. They differ
only in intent; both are cleaned up by shutdown (call it from a teardown
test, or rely on the per-process model of cargo-nextest).
§Async init
Async init functions are awaited with block_on. By default this uses a tiny
single-threaded executor; enable the tokio feature to drive them on a real
tokio current-thread runtime (needed for async I/O, timers, etc.).
Structs§
- Fixture
Init - The result of normalising a fixture’s init return value.
- Test
Scope Guard - RAII guard that runs all
test-scope teardowns when the test ends — even on panic. Inserted by the#[tpt_fixture]macro at the top of each test.
Enums§
- Scope
- The lifetime of a fixture.
Traits§
- Into
Fixture - Normalises a fixture init return value into a
FixtureInit.
Functions§
- block_
on - Drive a future to completion.
- end_
test_ scope - Run all
test-scope teardowns queued on the current thread. - fixture_
access - Resolve (or create) a fixture resource of type
Tforscope. - shutdown
- Tear down every
module/suite-scope fixture.