sequential_test/
lib.rs

1//! Allows for the creation of sequential tests.
2//! ```
3//! use sequential_test::sequential;
4//! #[cfg(test)]
5//! mod tests {
6//!     #[test]
7//!     #[sequential]
8//!     fn test1() {
9//!         // ...
10//!     }
11//!     #[test]
12//!     #[sequential]
13//!     fn test2() {
14//!         // ...
15//!     }
16//!     #[test]
17//!     #[parallel]
18//!     fn test3() {
19//!         // ...
20//!     }
21//! }
22//! ```
23//! - Tests with the [`macro@sequential`] attribute are guaranteed to be executed sequentially.
24//! - Tests with the [`macro@parallel`] attribute may run in parallel of each other but will not run
25//! at the same time as tests with the [`macro@sequential`] attribute.
26//! - Tests with neither attributes may run in parallel with any tests.
27//!
28//! Defining [`macro@sequential`] or [`macro@parallel`] attributes on non-tests or within scopes is
29//! considered UB.
30//!
31//! This library is both faster[^speed] and smaller than
32//! [`serial_test`](https://github.com/palfrey/serial_test) however offers less functionality.
33//!
34//! [^speed]: The current benchmark illustrate `sequential-test` covers the test set in an average
35//! of ~350ms while [`serial_test`](https://github.com/palfrey/serial_test) covers the test set in
36//! an average of ~550ms.
37
38pub use sequential_macro::*;
39
40#[cfg(test)]
41mod tests {
42    use super::{parallel, sequential};
43    use rand::{thread_rng, Rng};
44    use std::{thread, time::Duration};
45    // use serial_test::{serial,parallel};
46
47    const MAX_DURATION: Duration = Duration::from_millis(300);
48    const FAILURE_RATE: u8 = 0; // 0..100
49                                // Every test sleep for 0..MAX_DURATION then has a FAILURE_RATE % change to fail.
50    fn test() {
51        let mut rng = thread_rng();
52        let duration = rng.gen_range(Duration::ZERO..MAX_DURATION);
53        thread::sleep(duration);
54        assert!(rng.gen_range(0..100) >= FAILURE_RATE);
55    }
56    /// Sequential test
57    macro_rules! s {
58        ($($i:ident),*) => {
59            $(
60                #[test]
61                #[sequential]
62                fn $i() {
63                    test();
64                }
65            )*
66
67        };
68    }
69    /// Parallel test
70    macro_rules! p {
71        ($($i:ident),*) => {
72        $(
73            #[test]
74            #[parallel]
75            fn $i() {
76                test();
77            }
78        )*
79
80    };
81}
82    s!(
83        s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,
84        s21, s22, s23, s24, s25, s26, s27, s28, s29, s30, s31, s32, s33, s34, s35, s36, s37, s38,
85        s39, s40
86    );
87    p!(
88        p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20,
89        p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33, p34, p35, p36, p37, p38,
90        p39, p40
91    );
92}