1- //! An experimental replacement for libtest-mimic
1+ //! An experimental replacement for
2+ //! [libtest-mimic](https://docs.rs/libtest-mimic/latest/libtest_mimic/)
3+ //!
4+ //! Write your own tests that look and behave like built-in tests!
5+ //!
6+ //! This is a simple and small test harness that mimics the original `libtest`
7+ //! (used by `cargo test`/`rustc --test`). That means: all output looks pretty
8+ //! much like `cargo test` and most CLI arguments are understood and used. With
9+ //! that plumbing work out of the way, your test runner can focus on the actual
10+ //! testing.
11+ //!
12+ //! For a small real world example, see [`examples/mimic-tidy.rs`][1].
13+ //!
14+ //! [1]: https://github.com/assert-rs/libtest2/blob/main/crates/libtest2-mimic/examples/mimic-tidy.rs
215//!
316//! # Usage
417//!
1629//!
1730//! ```no_run
1831//! libtest2_mimic::Harness::with_env()
32+ //! .discover(
33+ //! Trial::test("succeeding_test", move || Ok(())),
34+ //! Trial::test("failing_test", move || Err("Woops".into())),
35+ //! )
1936//! .main();
2037//! ```
38+ //! Instead of returning `Ok` or `Err` directly, you want to actually perform
39+ //! your tests, of course. See [`Trial::test`] for more information on how to
40+ //! define a test. You can of course list all your tests manually. But in many
41+ //! cases it is useful to generate one test per file in a directory, for
42+ //! example.
43+ //!
44+ //! You can then run `cargo test --test mytest` to run it. To see the CLI
45+ //! arguments supported by this crate, run `cargo test --test mytest -- -h`.
2146//!
47+ //! # Known limitations and differences to the official test harness
48+ //!
49+ //! `libtest2-mimic` aims to be fully compatible with stable, non-deprecated parts of `libtest`
50+ //! but there are differences for now.
51+ //!
52+ //! Some of the notable differences:
53+ //!
54+ //! - Output capture and `--no-capture`: simply not supported. The official
55+ //! `libtest` uses internal `std` functions to temporarily redirect output.
56+ //! `libtest-mimic` cannot use those, see also [libtest2#12](https://github.com/assert-rs/libtest2/issues/12)
57+ //! - `--format=json` (unstable): our schema is part of an experiment to see what should be
58+ //! stabilized for `libtest`, see also [libtest2#42](https://github.com/assert-rs/libtest2/issues/42)
2259
2360#![ cfg_attr( docsrs, feature( doc_auto_cfg) ) ]
2461//#![warn(clippy::print_stderr)]
@@ -30,23 +67,27 @@ pub struct Harness {
3067}
3168
3269impl Harness {
70+ /// Read the process's CLI arguments
71+ pub fn with_env ( ) -> Self {
72+ let raw = std:: env:: args_os ( ) ;
73+ Self :: with_args ( raw)
74+ }
75+
76+ /// Manually specify CLI arguments
3377 pub fn with_args ( args : impl IntoIterator < Item = impl Into < std:: ffi:: OsString > > ) -> Self {
3478 Self {
3579 raw : args. into_iter ( ) . map ( |a| a. into ( ) ) . collect ( ) ,
3680 cases : Vec :: new ( ) ,
3781 }
3882 }
3983
40- pub fn with_env ( ) -> Self {
41- let raw = std:: env:: args_os ( ) ;
42- Self :: with_args ( raw)
43- }
44-
84+ /// Enumerate all test [`Trial`]s
4585 pub fn discover ( mut self , cases : impl IntoIterator < Item = Trial > ) -> Self {
4686 self . cases . extend ( cases) ;
4787 self
4888 }
4989
90+ /// Perform the tests and exit
5091 pub fn main ( self ) -> ! {
5192 match self . run ( ) {
5293 Ok ( true ) => std:: process:: exit ( 0 ) ,
@@ -79,6 +120,7 @@ impl Harness {
79120 }
80121}
81122
123+ /// A test case to be run
82124pub struct Trial {
83125 name : String ,
84126 #[ allow( clippy:: type_complexity) ]
@@ -149,10 +191,18 @@ pub struct RunContext<'t> {
149191}
150192
151193impl < ' t > RunContext < ' t > {
194+ /// Request this test to be ignored
195+ ///
196+ /// May be overridden by the CLI
197+ ///
198+ /// **Note:** prefer [`RunContext::ignore_for`]
152199 pub fn ignore ( & self ) -> Result < ( ) , RunError > {
153200 self . inner . ignore ( ) . map_err ( |e| RunError { inner : e } )
154201 }
155202
203+ /// Request this test to be ignored
204+ ///
205+ /// May be overridden by the CLI
156206 pub fn ignore_for ( & self , reason : impl std:: fmt:: Display ) -> Result < ( ) , RunError > {
157207 self . inner
158208 . ignore_for ( reason)
0 commit comments