Skip to main content

Crate tpt_fixture

Crate tpt_fixture 

Source
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

ScopeInitialisedShared acrossTeardown
testevery test— (fresh each call)end of that test
moduleoncethe test binaryshutdown()
suiteoncethe whole test binaryshutdown()

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§

FixtureInit
The result of normalising a fixture’s init return value.
TestScopeGuard
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§

IntoFixture
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 T for scope.
shutdown
Tear down every module/suite-scope fixture.

Attribute Macros§

tpt_fixture