-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Introduce setup
, teardown
, and fixture
attributes for cargo test
#117668
Comments
setup
and teardown
fixtures for cargo test
setup
, teardown
, and fixture
attributes for cargo test
FYI this is owned by libtest which is in the rust repo, and not cargo. We"ve also recently formed a testing-devex team to focus on improving the workflows around tests in a cross-team way. As this is a fairly fundamental change, I would recommend experimenting in a library first for these ideas (pytest-rs is where my experiments live) and then bringing that proposal forward on internals. However, this is unlikely to happen, directly. Last I talked to the libs team, we walked away with the idea that we"d work to make For myself, I see |
Thank you for your response, @epage. I appreciate your input, and I"ll definitely take a closer look at the repository you mentioned. Regarding the use of these attributes, I left a comment in the code, specifically, "// The usage of these functions is debatable." I haven"t fully explored the usage and implementation details yet, but I thought it was essential to introduce this idea in case it hasn"t been discussed before, especially concerning the simplification of unit tests. Here"s a more detailed suggestion I"d like to propose:
// module-based setup teardown.
#[setup]
fn setup_fn() {
// Setup before tests
}
#[teardown]
fn teardown_fn() {
// Cleanup after tests
}
#[test]
fn test_one() {
// Test logic
}
#[test]
fn test_two() {
// Test logic
}
#[test]
fn test_three() {
// Test logic
}
// test-based setup teardown.
#[setup]
fn setup_fn() {
// Setup before tests
}
#[teardown]
fn teardown_fn() {
// Cleanup after tests
}
#[test(setup_fn, teardown_fn)] // run setup/teardown for this test only
fn test_one() {
// Test logic
}
#[test]
fn test_two() {
// Test logic
}
#[test]
fn test_three() {
// Test logic
} In essence, this proposal aims to enhance the flexibility and clarity of how setup and teardown functions are employed in unit tests within your codebase. It encourages more precise control over when these functions are invoked, making the testing process more adaptable and robust. |
goofy ass ai written response 😭 |
We definitely need something like this for testing, so I really like where you are going with this, but I am not sure about this specifically. Different tests might have different setup requirements, but more importantly often the point of setup is to create some variables, such as a database connection, so I am not sure how useful a setup function is if it won"t be able to return anything. I feel like a setup function that cannot return a variable would be annoying, it"d leave me wondering what the point of it is if I cant use it for my normal setup functionality. |
At least, global setup and global teardown would be great ! |
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 theDrop
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:
setup
teardown
code and common structs instance for fixtures, making tests less maintainable.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: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
, andfixture
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.The text was updated successfully, but these errors were encountered: