Crates.io | tests-setup-teardown |
lib.rs | tests-setup-teardown |
version | 0.0.1 |
source | src |
created_at | 2023-01-30 08:16:57.778347 |
updated_at | 2023-01-30 08:16:57.778347 |
description | A macro to add `setup` and `teardown` on each test case |
homepage | https://github.com/xphoniex/tests-setup-teardown-rs |
repository | https://github.com/xphoniex/tests-setup-teardown-rs |
max_upload_size | |
id | 771532 |
size | 7,392 |
This macro allows you to add setup
and teardown
for tests. Longs setups distract you and clutters the space on each test, which ideally should be used to focus on the test logic itself.
Instead of having to do:
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cat() {
let db = reserve_path_for_db()
.make_tables_for_all_animals()
.unwrap();
let animals = populate_animals_in(&db)
.then_add_breeds()
.then_listen()
.unwrap();
// now test logic
assert_eq!(animals.is("sphynx", "cat"), true);
}
#[test]
fn test_dog() {
let db = reserve_path_for_db()
.make_tables_for_all_animals()
.unwrap();
let animals = populate_animals_in(&db)
.then_add_breeds()
.then_listen()
.unwrap();
// now test logic
assert_eq!(animals.is("bulldog", "dog"), true);
}
}
You can now do:
#[cfg(test)]
mod tests {
use super::*;
use tests_setup_teardown::{setup, setup_fn};
#[setup_fn(visible = animals)]
fn setup() {
let db = reserve_path_for_db()
.make_tables_for_all_animals()
.unwrap();
let animals = populate_animals_in(&db)
.then_add_breeds()
.then_listen()
.unwrap();
}
#[test]
#[setup(visible = animals)]
fn test_cat() {
// now test logic
assert_eq!(animals.is("sphynx", "cat"), true);
}
#[test]
#[setup(visible = animals)]
fn test_dog() {
// now test logic
assert_eq!(animals.is("bulldog", "dog"), true);
}
}