A lib help you run test with conditions, else the test will be ignored.
This crate provide a workable solution for this issue of rust-lang.
Currently, the condition is checked on build-time not runtime and not perfect,
because of this issue of rust-lang.
To avoid known issue at this moment,
please clean before running test.
bash
cargo clean; SOME_VAR=true cargo test
If you forget to add #[test]
flag on the test case, #[test_with]
macro will add it for you.
Before Rust version 1.61
, the ignore message does not show, such that the feature ign-msg
can be used,
and the name of ignored test case will be rewritten, such that you can easier to know why the test is ignored.
Run test case when the environment variable is set.
```rust // PWD environment variable exists
fn test_works() { assert!(true); }
// NOTHING environment variable does not exist
fn test_ignored() {
panic!("should be ignored")
}
or run all test cases for test module when the environment variable is set.
rust
mod tests {
#[test]
fn test_works() {
assert!(true);
}
} ```
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 // hostname exists
fn test_works() { assert!(true); }
// nothing file does not exist
fn test_ignored() { panic!("should be ignored") }
// etc exists
fn testworksfor_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 // https service exists
fn test_works() { assert!(true); }
// There is no 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.
```rust
fn test_works() { assert!(true); }
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.
```rust // localhost is online
fn test_works() { assert!(true); }
// 193.194.195.196 is offline
fn test_ignored() { panic!("should be ignored") } ```
Run integration test case when the user is specific user or in specific group ```rust
fn test_ignored() { panic!("should be ignored") }
fn test_ignored2() { panic!("should be ignored") }
fn test_ignored3() { panic!("should be ignored") } ```
Run integration test case when the memory/swap is enough ```rust
fn testignoredbycpucore() { panic!("should be ignored") }
fn testignoredbyphysicalcpu_core() { panic!("should be ignored") }
fn testignoredby_mem() { panic!("should be ignored") }
fn testignoredby_swap() { panic!("should be ignored") } ```