Attribute Macro module

Source
#[module]
Expand description

Help each function with #[test_with::runtime_*] in the module can register to run Also you can set up a mock instance for all of the test in the module

 // example/run-test.rs

 test_with::runner!(module1, module2);
 #[test_with::module]
 mod module1 {
     #[test_with::runtime_env(PWD)]
     fn test_works() {
         assert!(true);
     }
 }

 #[test_with::module]
 mod module2 {
     #[test_with::runtime_env(PWD)]
     fn test_works() {
         assert!(true);
     }
 }

You can set up mock with a public struct named TestEnv inside the module, or a public type named TestEnv inside the module. And the type or struct should have a Default trait for initialize the mock instance.

use std::ops::Drop;
use std::process::{Child, Command};

test_with::runner!(net);

#[test_with::module]
mod net {
    pub struct TestEnv {
        p: Child,
    }

    impl Default for TestEnv {
        fn default() -> TestEnv {
            let p = Command::new("python")
                .args(["-m", "http.server"])
                .spawn()
                .expect("failed to execute child");
            let mut count = 0;
            while count < 3 {
                if libtest_with::reqwest::blocking::get("http://127.0.0.1:8000").is_ok() {
                    break;
                }
                std::thread::sleep(std::time::Duration::from_secs(1));
                count += 1;
            }
            TestEnv { p }
        }
    }

    impl Drop for TestEnv {
        fn drop(&mut self) {
            self.p.kill().expect("fail to kill python http.server");
        }
    }

    #[test_with::runtime_http(127.0.0.1:8000)]
    fn test_with_environment() {
        assert!(true);
    }
}

or you can write mock struct in other place and just pass by type.

use std::ops::Drop;
use std::process::{Child, Command};

test_with::runner!(net);

pub struct Moc {
    p: Child,
}

impl Default for Moc {
    fn default() -> Moc {
        let p = Command::new("python")
            .args(["-m", "http.server"])
            .spawn()
            .expect("failed to execute child");
        let mut count = 0;
        while count < 3 {
            if libtest_with::reqwest::blocking::get("http://127.0.0.1:8000").is_ok() {
                break;
            }
            std::thread::sleep(std::time::Duration::from_secs(1));
            count += 1;
        }
        Moc { p }
    }
}

impl Drop for Moc {
    fn drop(&mut self) {
        self.p.kill().expect("fail to kill python http.server");
    }
}

#[test_with::module]
mod net {
    pub type TestEnv = super::Moc;

    #[test_with::runtime_http(127.0.0.1:8000)]
    fn test_with_environment() {
        assert!(true);
    }
}