Skip to content
CryoCryo home
Stdlibtest

fixture

import std::test::fixture; · source

Fixture

type trait Fixture {
    static setup() -> Result<This, TestError>;
}

A fixture is a value a test needs set up beforehand and torn down after — a temp directory, a seeded RNG, a probe allocator, a socket. The trait standardizes only the setup half.

Teardown is the value's own Drop impl. Because Cryo runs drop glue on every scope exit — including the early-return paths ? and return Result::Err(...) take — a fixture bound as a local is torn down on success and on assertion failure, with no per-test cleanup code.

![test]
function writes_a_file() -> Result<(), TestError> {
    mut tmp: TmpDir = TmpDir::setup()?;
    // ... the directory is removed however this function exits ...
    return Result::Ok(());
}

Define setup() once, in the Fixture impl. A second inherent setup() that called TmpDir::setup() would recurse into itself.

The one path Drop cannot cover is a hard process abort; there the OS reclaims everything.