Expand description
An experimental replacement for libtest-mimic
Write your own tests that look and behave like built-in tests!
This is a simple and small test harness that mimics the original libtest
(used by cargo test
/rustc --test
). That means: all output looks pretty
much like cargo test
and most CLI arguments are understood and used. With
that plumbing work out of the way, your test runner can focus on the actual
testing.
For a small real world example, see examples/mimic-tidy.rs
.
§Usage
To use this, you most likely want to add a manual [[test]]
section to
Cargo.toml
and set harness = false
. For example:
[[test]]
name = "mytest"
path = "tests/mytest.rs"
harness = false
And in tests/mytest.rs
you would call Harness::main
in the main
function:
Harness::with_env()
.discover([
Trial::test("succeeding_test", move |_| Ok(())),
Trial::test("failing_test", move |_| Err(RunError::fail("Woops"))),
])
.main();
Instead of returning Ok
or Err
directly, you want to actually perform
your tests, of course. See Trial::test
for more information on how to
define a test. You can of course list all your tests manually. But in many
cases it is useful to generate one test per file in a directory, for
example.
You can then run cargo test --test mytest
to run it. To see the CLI
arguments supported by this crate, run cargo test --test mytest -- -h
.
§Known limitations and differences to the official test harness
libtest2-mimic
aims to be fully compatible with stable, non-deprecated parts of libtest
but there are differences for now.
Some of the notable differences:
- Output capture and
--no-capture
: simply not supported. The officiallibtest
uses internalstd
functions to temporarily redirect output.libtest-mimic
cannot use those, see also libtest2#12 --format=json
(unstable): our schema is part of an experiment to see what should be stabilized forlibtest
, see also libtest2#42
Structs§
- Harness
- RunContext
- RunError
- Trial
- A test case to be run