safe_tests

Attribute Macro safe_tests 

Source
#[safe_tests]
Expand description

Some tests need extra guard to be executed safely. It fixes some flakes that are caused by tests that are executed in parallel and are using the same resources.

Make sure to add #[safe_test] attribute to each test that needs to be executed safely.

This macro will inject static mutex variable, so that tests are executed safely without impacting each other. Mutex variable name can be provided as a macro param. If no param is provided, default name TEST_MUTEX will be used.

Example:

#[safe_tests]
pub(crate) mod test {
    const TEST_ENV_VAR: &str = "TEST_ENV_VAR";

    #[safe_test]
    #[test]
    fn it_works1() {
        std::env::set_var(TEST_ENV_VAR, "test1");
        let test_value = std::env::var(TEST_ENV_VAR).unwrap();
        assert_eq!(test_value, "test1");
    }
    #[safe_test]
    #[test]
    fn it_works2() {
        std::env::set_var(TEST_ENV_VAR, "test2");
        let test_value = std::env::var(TEST_ENV_VAR).unwrap();
        assert_eq!(test_value, "test2");
    }
}