tests-setup-teardown

Crates.iotests-setup-teardown
lib.rstests-setup-teardown
version0.0.1
sourcesrc
created_at2023-01-30 08:16:57.778347
updated_at2023-01-30 08:16:57.778347
descriptionA macro to add `setup` and `teardown` on each test case
homepagehttps://github.com/xphoniex/tests-setup-teardown-rs
repositoryhttps://github.com/xphoniex/tests-setup-teardown-rs
max_upload_size
id771532
size7,392
Dave (xphoniex)

documentation

README

What is this?

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.

Example

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);
    }
}
Commit count: 1

cargo fmt