libtest_mimic/
lib.rs

1//! Write your own tests and benchmarks that look and behave like built-in tests!
2//!
3//! This is a simple and small test harness that mimics the original `libtest`
4//! (used by `cargo test`/`rustc --test`). That means: all output looks pretty
5//! much like `cargo test` and most CLI arguments are understood and used. With
6//! that plumbing work out of the way, your test runner can focus on the actual
7//! testing.
8//!
9//! For a small real world example, see [`examples/tidy.rs`][1].
10//!
11//! [1]: https://github.com/LukasKalbertodt/libtest-mimic/blob/master/examples/tidy.rs
12//!
13//! # Usage
14//!
15//! To use this, you most likely want to add a manual `[[test]]` section to
16//! `Cargo.toml` and set `harness = false`. For example:
17//!
18//! ```toml
19//! [[test]]
20//! name = "mytest"
21//! path = "tests/mytest.rs"
22//! harness = false
23//! ```
24//!
25//! And in `tests/mytest.rs` you would call [`run`] in the `main` function:
26//!
27//! ```no_run
28//! use libtest_mimic::{Arguments, Trial};
29//!
30//!
31//! // Parse command line arguments
32//! let args = Arguments::from_args();
33//!
34//! // Create a list of tests and/or benchmarks (in this case: two dummy tests).
35//! let tests = vec![
36//!     Trial::test("succeeding_test", move || Ok(())),
37//!     Trial::test("failing_test", move || Err("Woops".into())),
38//! ];
39//!
40//! // Run all tests and exit the application appropriatly.
41//! libtest_mimic::run(&args, tests).exit();
42//! ```
43//!
44//! Instead of returning `Ok` or `Err` directly, you want to actually perform
45//! your tests, of course. See [`Trial::test`] for more information on how to
46//! define a test. You can of course list all your tests manually. But in many
47//! cases it is useful to generate one test per file in a directory, for
48//! example.
49//!
50//! You can then run `cargo test --test mytest` to run it. To see the CLI
51//! arguments supported by this crate, run `cargo test --test mytest -- -h`.
52//!
53//!
54//! # Known limitations and differences to the official test harness
55//!
56//! `libtest-mimic` works on a best-effort basis: it tries to be as close to
57//! `libtest` as possible, but there are differences for a variety of reasons.
58//! For example, some rarely used features might not be implemented, some
59//! features are extremely difficult to implement, and removing minor,
60//! unimportant differences is just not worth the hassle.
61//!
62//! Some of the notable differences:
63//!
64//! - Output capture and `--nocapture`: simply not supported. The official
65//!   `libtest` uses internal `std` functions to temporarily redirect output.
66//!   `libtest-mimic` cannot use those. See [this issue][capture] for more
67//!   information.
68//! - `--format=junit`
69//! - Also see [#13](https://github.com/LukasKalbertodt/libtest-mimic/issues/13)
70//!
71//! [capture]: https://github.com/LukasKalbertodt/libtest-mimic/issues/9
72
73#![forbid(unsafe_code)]
74
75use std::{
76    borrow::Cow,
77    fmt,
78    process::{self, ExitCode},
79    sync::{mpsc, Mutex},
80    thread,
81    time::Instant,
82};
83
84mod args;
85mod printer;
86
87use printer::Printer;
88
89pub use crate::args::{Arguments, ColorSetting, FormatSetting};
90
91
92
93/// A single test or benchmark.
94///
95/// The original `libtest` often calls benchmarks "tests", which is a bit
96/// confusing. So in this library, it is called "trial".
97///
98/// A trial is created via [`Trial::test`] or [`Trial::bench`]. The trial's
99/// `name` is printed and used for filtering. The `runner` is called when the
100/// test/benchmark is executed to determine its outcome. If `runner` panics,
101/// the trial is considered "failed". If you need the behavior of
102/// `#[should_panic]` you need to catch the panic yourself. You likely want to
103/// compare the panic payload to an expected value anyway.
104pub struct Trial {
105    runner: Box<dyn FnOnce(bool) -> Outcome + Send>,
106    info: TestInfo,
107}
108
109impl Trial {
110    /// Creates a (non-benchmark) test with the given name and runner.
111    ///
112    /// The runner returning `Ok(())` is interpreted as the test passing. If the
113    /// runner returns `Err(_)`, the test is considered failed.
114    pub fn test<R>(name: impl Into<String>, runner: R) -> Self
115    where
116        R: FnOnce() -> Result<(), Failed> + Send + 'static,
117    {
118        Self {
119            runner: Box::new(move |_test_mode| match runner() {
120                Ok(()) => Outcome::Passed,
121                Err(failed) => Outcome::Failed(failed),
122            }),
123            info: TestInfo {
124                name: name.into(),
125                kind: String::new(),
126                is_ignored: false,
127                is_bench: false,
128            },
129        }
130    }
131
132    /// Creates a benchmark with the given name and runner.
133    ///
134    /// If the runner's parameter `test_mode` is `true`, the runner function
135    /// should run all code just once, without measuring, just to make sure it
136    /// does not panic. If the parameter is `false`, it should perform the
137    /// actual benchmark. If `test_mode` is `true` you may return `Ok(None)`,
138    /// but if it's `false`, you have to return a `Measurement`, or else the
139    /// benchmark is considered a failure.
140    ///
141    /// `test_mode` is `true` if neither `--bench` nor `--test` are set, and
142    /// `false` when `--bench` is set. If `--test` is set, benchmarks are not
143    /// ran at all, and both flags cannot be set at the same time.
144    pub fn bench<R>(name: impl Into<String>, runner: R) -> Self
145    where
146        R: FnOnce(bool) -> Result<Option<Measurement>, Failed> + Send + 'static,
147    {
148        Self {
149            runner: Box::new(move |test_mode| match runner(test_mode) {
150                Err(failed) => Outcome::Failed(failed),
151                Ok(_) if test_mode => Outcome::Passed,
152                Ok(Some(measurement)) => Outcome::Measured(measurement),
153                Ok(None)
154                    => Outcome::Failed("bench runner returned `Ok(None)` in bench mode".into()),
155            }),
156            info: TestInfo {
157                name: name.into(),
158                kind: String::new(),
159                is_ignored: false,
160                is_bench: true,
161            },
162        }
163    }
164
165    /// Sets the "kind" of this test/benchmark. If this string is not
166    /// empty, it is printed in brackets before the test name (e.g.
167    /// `test [my-kind] test_name`). (Default: *empty*)
168    ///
169    /// This is the only extension to the original libtest.
170    pub fn with_kind(self, kind: impl Into<String>) -> Self {
171        Self {
172            info: TestInfo {
173                kind: kind.into(),
174                ..self.info
175            },
176            ..self
177        }
178    }
179
180    /// Sets whether or not this test is considered "ignored". (Default: `false`)
181    ///
182    /// With the built-in test suite, you can annotate `#[ignore]` on tests to
183    /// not execute them by default (for example because they take a long time
184    /// or require a special environment). If the `--ignored` flag is set,
185    /// ignored tests are executed, too.
186    pub fn with_ignored_flag(self, is_ignored: bool) -> Self {
187        Self {
188            info: TestInfo {
189                is_ignored,
190                ..self.info
191            },
192            ..self
193        }
194    }
195
196    /// Returns the name of this trial.
197    pub fn name(&self) -> &str {
198        &self.info.name
199    }
200
201    /// Returns the kind of this trial. If you have not set a kind, this is an
202    /// empty string.
203    pub fn kind(&self) -> &str {
204        &self.info.kind
205    }
206
207    /// Returns whether this trial has been marked as *ignored*.
208    pub fn has_ignored_flag(&self) -> bool {
209        self.info.is_ignored
210    }
211
212    /// Returns `true` iff this trial is a test (as opposed to a benchmark).
213    pub fn is_test(&self) -> bool {
214        !self.info.is_bench
215    }
216
217    /// Returns `true` iff this trial is a benchmark (as opposed to a test).
218    pub fn is_bench(&self) -> bool {
219        self.info.is_bench
220    }
221}
222
223impl fmt::Debug for Trial {
224    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225        struct OpaqueRunner;
226        impl fmt::Debug for OpaqueRunner {
227            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228                f.write_str("<runner>")
229            }
230        }
231
232        f.debug_struct("Test")
233            .field("runner", &OpaqueRunner)
234            .field("name", &self.info.name)
235            .field("kind", &self.info.kind)
236            .field("is_ignored", &self.info.is_ignored)
237            .field("is_bench", &self.info.is_bench)
238            .finish()
239    }
240}
241
242#[derive(Debug)]
243struct TestInfo {
244    name: String,
245    kind: String,
246    is_ignored: bool,
247    is_bench: bool,
248}
249
250impl TestInfo {
251    fn test_name_with_kind(&self) -> Cow<'_, str> {
252        if self.kind.is_empty() {
253            Cow::Borrowed(&self.name)
254        } else {
255            Cow::Owned(format!("[{}] {}", self.kind, self.name))
256        }
257    }
258}
259
260/// Output of a benchmark.
261#[derive(Debug, Clone, Copy, PartialEq, Eq)]
262pub struct Measurement {
263    /// Average time in ns.
264    pub avg: u64,
265
266    /// Variance in ns.
267    pub variance: u64,
268}
269
270/// Indicates that a test/benchmark has failed. Optionally carries a message.
271///
272/// You usually want to use the `From` impl of this type, which allows you to
273/// convert any `T: fmt::Display` (e.g. `String`, `&str`, ...) into `Failed`.
274#[derive(Debug, Clone)]
275pub struct Failed {
276    msg: Option<String>,
277}
278
279impl Failed {
280    /// Creates an instance without message.
281    pub fn without_message() -> Self {
282        Self { msg: None }
283    }
284
285    /// Returns the message of this instance.
286    pub fn message(&self) -> Option<&str> {
287        self.msg.as_deref()
288    }
289}
290
291impl<M: std::fmt::Display> From<M> for Failed {
292    fn from(msg: M) -> Self {
293        Self {
294            msg: Some(msg.to_string())
295        }
296    }
297}
298
299
300
301/// The outcome of performing a test/benchmark.
302#[derive(Debug, Clone)]
303enum Outcome {
304    /// The test passed.
305    Passed,
306
307    /// The test or benchmark failed.
308    Failed(Failed),
309
310    /// The test or benchmark was ignored.
311    Ignored,
312
313    /// The benchmark was successfully run.
314    Measured(Measurement),
315}
316
317/// Contains information about the entire test run. Is returned by[`run`].
318///
319/// This type is marked as `#[must_use]`. Usually, you just call
320/// [`exit()`][Conclusion::exit] on the result of `run` to exit the application
321/// with the correct exit code. But you can also store this value and inspect
322/// its data.
323#[derive(Clone, Debug, PartialEq, Eq)]
324#[must_use = "Call `exit()` or `exit_if_failed()` to set the correct return code"]
325pub struct Conclusion {
326    /// Number of tests and benchmarks that were filtered out (either by the
327    /// filter-in pattern or by `--skip` arguments).
328    pub num_filtered_out: u64,
329
330    /// Number of passed tests.
331    pub num_passed: u64,
332
333    /// Number of failed tests and benchmarks.
334    pub num_failed: u64,
335
336    /// Number of ignored tests and benchmarks.
337    pub num_ignored: u64,
338
339    /// Number of benchmarks that successfully ran.
340    pub num_measured: u64,
341}
342
343impl Conclusion {
344    /// Returns an exit code that can be returned from `main` to signal
345    /// success/failure to the calling process.
346    pub fn exit_code(&self) -> ExitCode {
347        if self.has_failed() {
348            ExitCode::from(101)
349        } else {
350            ExitCode::SUCCESS
351        }
352    }
353
354    /// Returns whether there have been any failures.
355    pub fn has_failed(&self) -> bool {
356        self.num_failed > 0
357    }
358
359    /// Exits the application with an appropriate error code (0 if all tests
360    /// have passed, 101 if there have been failures). This uses
361    /// [`process::exit`], meaning that destructors are not ran. Consider
362    /// using [`Self::exit_code`] instead for a proper program cleanup.
363    pub fn exit(&self) -> ! {
364        self.exit_if_failed();
365        process::exit(0);
366    }
367
368    /// Exits the application with error code 101 if there were any failures.
369    /// Otherwise, returns normally. This uses [`process::exit`], meaning that
370    /// destructors are not ran. Consider using [`Self::exit_code`] instead for
371    /// a proper program cleanup.
372    pub fn exit_if_failed(&self) {
373        if self.has_failed() {
374            process::exit(101)
375        }
376    }
377
378    fn empty() -> Self {
379        Self {
380            num_filtered_out: 0,
381            num_passed: 0,
382            num_failed: 0,
383            num_ignored: 0,
384            num_measured: 0,
385        }
386    }
387}
388
389impl Arguments {
390    /// Returns `true` if the given test should be ignored.
391    fn is_ignored(&self, test: &Trial) -> bool {
392        (test.info.is_ignored && !self.ignored && !self.include_ignored)
393            || (test.info.is_bench && self.test)
394            || (!test.info.is_bench && self.bench)
395    }
396
397    fn is_filtered_out(&self, test: &Trial) -> bool {
398        let test_name = test.name();
399        // Match against the full test name, including the kind. This upholds the invariant that if
400        // --list prints out:
401        //
402        // <some string>: test
403        //
404        // then "--exact <some string>" runs exactly that test.
405        let test_name_with_kind = test.info.test_name_with_kind();
406
407        // If a filter was specified, apply this
408        if let Some(filter) = &self.filter {
409            match self.exact {
410                // For exact matches, we want to match against either the test name (to maintain
411                // backwards compatibility with older versions of libtest-mimic), or the test kind
412                // (technically more correct with respect to matching against the output of --list.)
413                true if test_name != filter && &test_name_with_kind != filter => return true,
414                false if !test_name_with_kind.contains(filter) => return true,
415                _ => {}
416            };
417        }
418
419        // If any skip pattern were specified, test for all patterns.
420        for skip_filter in &self.skip {
421            match self.exact {
422                // For exact matches, we want to match against either the test name (to maintain
423                // backwards compatibility with older versions of libtest-mimic), or the test kind
424                // (technically more correct with respect to matching against the output of --list.)
425                true if test_name == skip_filter || &test_name_with_kind == skip_filter => {
426                    return true
427                }
428                false if test_name_with_kind.contains(skip_filter) => return true,
429                _ => {}
430            }
431        }
432
433        if self.ignored && !test.info.is_ignored {
434            return true;
435        }
436
437        false
438    }
439}
440
441/// Runs all given trials (tests & benchmarks).
442///
443/// This is the central function of this crate. It provides the framework for
444/// the testing harness. It does all the printing and house keeping.
445///
446/// The returned value contains a couple of useful information. See
447/// [`Conclusion`] for more information. If `--list` was specified, a list is
448/// printed and a dummy `Conclusion` is returned.
449pub fn run(args: &Arguments, mut tests: Vec<Trial>) -> Conclusion {
450    let start_instant = Instant::now();
451    let mut conclusion = Conclusion::empty();
452
453    // Apply filtering
454    if args.filter.is_some() || !args.skip.is_empty() || args.ignored {
455        let len_before = tests.len() as u64;
456        tests.retain(|test| !args.is_filtered_out(test));
457        conclusion.num_filtered_out = len_before - tests.len() as u64;
458    }
459    let tests = tests;
460
461    // Create printer which is used for all output.
462    let mut printer = printer::Printer::new(args, &tests);
463
464    // If `--list` is specified, just print the list and return.
465    if args.list {
466        printer.print_list(&tests, args.ignored);
467        return Conclusion::empty();
468    }
469
470    // Print number of tests
471    printer.print_title(tests.len() as u64);
472
473    let mut failed_tests = Vec::new();
474    let mut handle_outcome = |outcome: Outcome, test: TestInfo, printer: &mut Printer| {
475        printer.print_single_outcome(&test, &outcome);
476
477        // Handle outcome
478        match outcome {
479            Outcome::Passed => conclusion.num_passed += 1,
480            Outcome::Failed(failed) => {
481                failed_tests.push((test, failed.msg));
482                conclusion.num_failed += 1;
483            },
484            Outcome::Ignored => conclusion.num_ignored += 1,
485            Outcome::Measured(_) => conclusion.num_measured += 1,
486        }
487    };
488
489    // Execute all tests.
490    let test_mode = !args.bench;
491
492    let num_threads = platform_defaults_to_one_thread()
493        .then_some(1)
494        .or(args.test_threads)
495        .or_else(|| std::thread::available_parallelism().ok().map(Into::into))
496        .unwrap_or(1);
497
498    if num_threads == 1 {
499        // Run test sequentially in main thread
500        for test in tests {
501            // Print `test foo    ...`, run the test, then print the outcome in
502            // the same line.
503            printer.print_test(&test.info);
504            let outcome = if args.is_ignored(&test) {
505                Outcome::Ignored
506            } else {
507                run_single(test.runner, test_mode)
508            };
509            handle_outcome(outcome, test.info, &mut printer);
510        }
511    } else {
512        // Run test in thread pool.
513        let (sender, receiver) = mpsc::channel();
514
515        let num_tests = tests.len();
516        // TODO: this should use a mpmc channel, once that's stabilized in std.
517        let iter = Mutex::new(tests.into_iter());
518        thread::scope(|scope| {
519            // Start worker threads
520            for _ in 0..num_threads {
521                scope.spawn(|| {
522                    loop {
523                        // Get next test to process from the iterator.
524                        let Some(trial) = iter.lock().unwrap().next() else {
525                            break;
526                        };
527
528                        let payload = if args.is_ignored(&trial) {
529                            (Outcome::Ignored, trial.info)
530                        } else {
531                            let outcome = run_single(trial.runner, test_mode);
532                            (outcome, trial.info)
533                        };
534
535                        // It's fine to ignore the result of sending. If the
536                        // receiver has hung up, everything will wind down soon
537                        // anyway.
538                        let _ = sender.send(payload);
539                    }
540                });
541            }
542
543            // Print results of tests that already dinished
544            for (outcome, test_info) in receiver.iter().take(num_tests) {
545                // In multithreaded mode, we do only print the start of the line
546                // after the test ran, as otherwise it would lead to terribly
547                // interleaved output.
548                printer.print_test(&test_info);
549                handle_outcome(outcome, test_info, &mut printer);
550            }
551        });
552
553    }
554
555    // Print failures if there were any, and the final summary.
556    if !failed_tests.is_empty() {
557        printer.print_failures(&failed_tests);
558    }
559
560    printer.print_summary(&conclusion, start_instant.elapsed());
561
562    conclusion
563}
564
565/// Returns whether the current host platform should use a single thread by
566/// default rather than a thread pool by default. Some platforms, such as
567/// WebAssembly, don't have native support for threading at this time.
568fn platform_defaults_to_one_thread() -> bool {
569    cfg!(target_family = "wasm")
570}
571
572/// Runs the given runner, catching any panics and treating them as a failed test.
573fn run_single(runner: Box<dyn FnOnce(bool) -> Outcome + Send>, test_mode: bool) -> Outcome {
574    use std::panic::{catch_unwind, AssertUnwindSafe};
575
576    catch_unwind(AssertUnwindSafe(move || runner(test_mode))).unwrap_or_else(|e| {
577        // The `panic` information is just an `Any` object representing the
578        // value the panic was invoked with. For most panics (which use
579        // `panic!` like `println!`), this is either `&str` or `String`.
580        let payload = e.downcast_ref::<String>()
581            .map(|s| s.as_str())
582            .or(e.downcast_ref::<&str>().map(|s| *s));
583
584        let msg = match payload {
585            Some(payload) => format!("test panicked: {payload}"),
586            None => format!("test panicked"),
587        };
588        Outcome::Failed(msg.into())
589    })
590}