Skip to main content

TestFixture

Trait TestFixture 

Source
pub trait TestFixture: Sized + Send {
    // Required method
    fn setup() -> Self;

    // Provided method
    fn teardown(&mut self) { ... }
}
Expand description

Trait for test fixtures that set up and tear down test data.

Implement this trait for resources that need initialization before tests and cleanup afterwards (databases, temp files, mock services, etc.).

§Example

use fastapi_core::testing::TestFixture;

struct DatabaseFixture {
    conn: DatabaseConnection,
    users_created: Vec<i64>,
}

impl TestFixture for DatabaseFixture {
    fn setup() -> Self {
        let conn = DatabaseConnection::test();
        DatabaseFixture { conn, users_created: vec![] }
    }

    fn teardown(&mut self) {
        // Delete any users we created
        for id in &self.users_created {
            self.conn.delete_user(*id);
        }
    }
}

Required Methods§

Source

fn setup() -> Self

Set up the fixture before the test.

Provided Methods§

Source

fn teardown(&mut self)

Tear down the fixture after the test.

This is called even if the test panics, ensuring cleanup happens.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§