#teardown #setup #testing #add #macro #case #test-cases

macro tests-setup-teardown

A macro to add setup and teardown on each test case

1 unstable release

0.0.1 Jan 30, 2023

#9 in #teardown

MIT/Apache

8KB
112 lines

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);
    }
}

Dependencies

~1.5MB
~37K SLoC