Skip to content

RFC: Introduce setup, teardown, and fixture attributes for cargo test #117668

Open
@wiseaidev

Description

@wiseaidev

Problem

The current testing framework in Rust, as implemented by cargo test, lacks a convenient way to define and manage setup teardown, and fixtures objects for tests. While it is possible to implement such functionality using structs, implementing the Drop trait in case of teardown, and such, this approach can be cumbersome and lacks the expressiveness and convenience of more declarative fixture management.

The absence of a dedicated mechanism for defining fixtures and their lifecycles results in the following issues:

  1. Test authors often need to implement repetitive and error-prone setup teardown code and common structs instance for fixtures, making tests less maintainable.
  2. Complex test setups, teardowns, and resource management require a non-trivial amount of boilerplate code.
  3. There is no standard way to share setup/teardown, and fixtures across multiple tests or test modules, potentially leading to duplication of setup/teardown code and fixtures.

Proposed Solution

This RFC proposes the introduction of three new attributes #[setup], #[teardown], and #[fixture] which can be used in Rust test functions to indicate functions that should serve as setup/teardown functions, and fixture objects respectively. Test authors can define setup functions/teardown functions, and common objects with these attributes, and the testing framework will ensure that they are executed appropriately before and after test functions that use them. For instance:

use std::fs;
use tempfile::TempDir;

#[fixture]
fn temp_dir() -> TempDir {
    let temp_dir = TempDir::new().expect("Failed to create temporary directory");
    temp_dir
}

#[setup]
fn setup() {
   // Setup before tests
}

#[teardown]
fn teardown() {
   // Cleanup after tests 
}

#[test(setup, teardown)] // The usage of these functions is debatable
fn test_with_temp_dir(temp_dir) { // fixtures are passed to the test function as args
    // Test logic with temp_dir fixture arg
    // ...
}

With these attributes, test authors can set up necessary fixtures or resources in a clear and standardized manner before the test execution and ensure proper cleanup afterward. This feature will streamline the development of tests, particularly in cases where complex setup and teardown operations are required.

Notes

Introducing setup, teardown, and fixture attributes in Cargo Test will improve the test development experience by simplifying resource management, enhancing test predictability, and enabling developers to write more complex tests with ease. This RFC aligns with the goal of making Rust testing more ergonomic and developer-friendly.

P.S. Just stumbled upon rstest which provides fixtures, but lacks a setup/teardown mechanism.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-testing-devexRelevant to the testing devex team (testing DX), which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions