A lib help you run test with condition
Run test case when the environment variable is set. A solution for this issue of rust-lang.
```rust
mod tests {
// PWD environment variable exists
#[test_with::env(PWD)]
fn test_works() {
assert!(true);
}
// NOTHING environment variable does not exist
#[test_with::env(NOTHING)]
fn test_ignored() {
panic!("should be ignored")
}
} ```
Result of cargo test
```text
running 2 tests
test tests::testignored ... ignored
test tests::testworks ... ok
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s ```
If the test depends on more than one environment variables,
you can write it with multiple variables, #[test_with::env(VAR1, VAR2)]
.
Run test case when the file or folder exist. This is good for testing with database config.
If you want to check the folder exist or not, please use path
.
```rust
mod tests {
// hostname exists
#[test_with::file(/etc/hostname)]
fn test_works() {
assert!(true);
}
// nothing file does not exist
#[test_with::file(/etc/nothing)]
fn test_ignored() {
panic!("should be ignored")
}
// etc exists
#[test_with::path(/etc)]
fn test_works_for_path() {
assert!(true);
}
} ```
If the test depends on more than one file or path,
you can write it with multiple file/path,
#[test_with::file(/file1, /file2)]
or #[test_with::path(/folder, /file)]
.
Run test case when the http/https service available. This is good for integration testing.
```rust
mod tests {
// https service exists
#[test_with::https(www.rust-lang.org)]
fn test_works() {
assert!(true);
}
// There is no not.exist.com
#[test_with::https(not.exist.com)]
fn test_ignored() {
panic!("should be ignored")
}
} ```
If the test depends on more than one service,
you can write it with multiple service,
#[test_with::http(service1, service2)]
or #[test_with::http2(service1, service2)]
.
Run integration test case when the remote tcp socket is listening.
```
mod tests { #[testwith::tcp(8.8.8.8:53)] fn testworks() { assert!(true); }
#[test_with::tcp(193.194.195.196)]
fn test_ignored() {
panic!("should be ignored")
}
} ```
Run integration test case when the remote server online. Please note the user running test case should have capability to open socket.
```
mod tests {
// localhost is online
#[test_with::icmp(127.0.0.1)]
fn test_works() {
assert!(true);
}
// 193.194.195.196 is offline
#[test_with::icmp(193.194.195.196)]
fn test_ignored() {
panic!("should be ignored")
}
} ```