Skip to main content

test

Attribute Macro test 

Source
#[test]
Expand description

Attribute macro for database integration tests with schema isolation.

Creates an isolated PostgreSQL schema per test, applies SQL fixtures, passes a connected Pool to the test function, and drops the schema after the test completes (even on panic).

§Usage

#[bsql::test]
async fn test_basic(pool: bsql::Pool) {
    pool.raw_execute("SELECT 1").await.unwrap();
}

#[bsql::test(fixtures("schema", "seed"))]
async fn test_with_fixtures(pool: bsql::Pool) {
    let user = bsql::query!("SELECT name FROM users WHERE id = $id: i32")
        .fetch_one(&pool).await.unwrap();
    assert_eq!(user.name, "Alice");
}

§Fixtures

Fixture names are resolved to SQL files at compile time from:

  • {CARGO_MANIFEST_DIR}/fixtures/{name}.sql
  • {CARGO_MANIFEST_DIR}/tests/fixtures/{name}.sql

Fixtures are applied in order after the isolated schema is created.

§Environment

Requires BSQL_DATABASE_URL or DATABASE_URL to be set at runtime.