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

tests-setup-teardown

一个宏,用于在每个测试用例上添加 setupteardown

1个不稳定版本

0.0.1 2023年1月30日

#10#teardown

MIT/Apache

8KB
112

这是什么?

此宏允许您为测试添加 setupteardown。冗长的设置会分散您的注意力并使每个测试的空间变得杂乱,而理想情况下应该用于关注测试逻辑本身。

示例

以前需要做

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

现在您可以这样做

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

依赖关系

~1.5MB
~36K SLoC