test_casing/lib.rs
1//! Minimalistic testing framework for generating tests for a given set of test cases
2//! and decorating them to add retries, timeouts, sequential test processing etc. In other words,
3//! the framework implements:
4//!
5//! - Parameterized tests of reasonably low cardinality for the standard Rust test runner
6//! - Fully code-based, composable and extensible test decorators.
7//!
8//! # Overview
9//!
10//! ## Test cases
11//!
12//! [`test_casing`](macro@test_casing) attribute macro wraps a free-standing function
13//! with one or more arguments and transforms it into a collection of test cases.
14//! The arguments to the function are supplied by an iterator (more precisely,
15//! an expression implementing [`IntoIterator`]).
16//!
17//! For convenience, there is [`TestCases`], a lazy iterator wrapper that allows constructing
18//! test cases which cannot be constructed in compile time (e.g., ones requiring access to heap).
19//! [`TestCases`] can be instantiated using the [`cases!`] macro.
20//!
21//! Since a separate test wrapper is generated for each case, their number should be
22//! reasonably low (roughly speaking, no more than 20).
23//! Isolating each test case makes most sense if the cases involve some heavy lifting
24//! (spinning up a runtime, logging considerable amount of information, etc.).
25//!
26//! ## Test decorators
27//!
28//! [`decorate`] attribute macro can be placed on a test function to add generic functionality,
29//! such as retries, timeouts or running tests in a sequence.
30//!
31//! The [`decorators`] module defines some basic decorators and the
32//! [`DecorateTest`](decorators::DecorateTest) trait allowing to define custom decorators.
33//! Test decorators support async tests, tests returning `Result`s and test cases; see
34//! the module docs for more details.
35//!
36//! # Test cases structure
37//!
38//! The generated test cases are placed in a module with the same name as the target function
39//! near the function.
40//! This allows specifying the (potentially qualified) function name to restrict the test scope.
41//!
42//! If the [`nightly` crate feature](#nightly) is not enabled, names of particular test cases
43//! are not descriptive; they have the `case_NN` format, where `NN` is the 0-based case index.
44//! The values of arguments provided to the test are printed to the standard output
45//! at the test start. (The standard output is captured and thus may be not visible
46//! unless the `--nocapture` option is specified in the `cargo test` command.)
47//!
48//! If the `nightly` feature *is* enabled, the names are more descriptive, containing [`Debug`]
49//! presentation of all args together with their names. Here's an excerpt from the integration
50//! tests for this crate:
51//!
52//! ```text
53//! test number_can_be_converted_to_string::case_1 [number = 3, expected = "3"] ... ok
54//! test number_can_be_converted_to_string::case_2 [number = 5, expected = "5"] ... ok
55//! test numbers_are_large::case_0 [number = 2] ... ignored, testing that `#[ignore]` attr works
56//! test numbers_are_large::case_1 [number = 3] ... ignored, testing that `#[ignore]` attr works
57//! test string_conversion_fail::case_0 [bogus_str = "not a number"] - should panic ... ok
58//! test string_conversion_fail::case_1 [bogus_str = "-"] - should panic ... ok
59//! test string_conversion_fail::case_2 [bogus_str = ""] - should panic ... ok
60//! ```
61//!
62//! The names are fully considered when filtering tests, meaning that it's possible to run
63//! particular cases using a filter like `cargo test 'number = 5'`.
64//!
65//! # Alternatives and similar tools
66//!
67//! - The approach to test casing from this crate can be reproduced with some amount of copy-pasting
68//! by manually feeding necessary inputs to a common parametric testing function.
69//! Optionally, these tests may be collected in a module for better structuring.
70//! The main downside of this approach is the amount of copy-pasting.
71//! - Alternatively, multiple test cases may be run in a single `#[test]` (e.g., in a loop).
72//! This is fine for the large amount of small cases (e.g., mini-fuzzing), but may have downsides
73//! such as overflowing or overlapping logs and increased test runtimes.
74//! - The [`test-case`] crate uses a similar approach to test case structuring, but differs
75//! in how test case inputs are specified. Subjectively, the approach used by this crate
76//! is more extensible and easier to read.
77//! - [Property testing] / [`quickcheck`]-like frameworks provide much more exhaustive approach
78//! to parameterized testing, but they require significantly more setup effort.
79//! - [`rstest`] supports test casing and some of the test decorators (e.g., timeouts).
80//! - [`nextest`] is an alternative test runner that supports most of the test decorators
81//! defined in the [`decorators`] module. It does not use code-based decorator config and
82//! does not allow for custom decorator.
83//!
84//! [`test-case`]: https://docs.rs/test-case/
85//! [Property testing]: https://docs.rs/proptest/
86//! [`quickcheck`]: https://docs.rs/quickcheck/
87//! [`rstest`]: https://crates.io/crates/rstest
88//! [`nextest`]: https://nexte.st/
89//!
90//! # Crate features
91//!
92//! ## `nightly`
93//!
94//! *(Off by default)*
95//!
96//! Uses [custom test frameworks] APIs together with a generous spicing of hacks
97//! to include arguments in the names of the generated tests (see an excerpt above
98//! for an illustration). `test_casing` actually does not require a custom test runner,
99//! but rather hacks into the standard one; thus, the generated test cases can run alongside with
100//! ordinary / non-parameterized tests.
101//!
102//! Requires a nightly Rust toolchain and specifying `#![feature(test, custom_test_frameworks)]`
103//! in the using crate. Because `custom_test_frameworks` APIs may change between toolchain releases,
104//! the feature may break. See [the CI config] for the nightly toolchain version the crate
105//! is tested against.
106//!
107//! [custom test frameworks]: https://github.com/rust-lang/rust/issues/50297
108//! [the CI config]: https://github.com/slowli/test-casing/blob/main/.github/workflows/ci.yml
109
110#![cfg_attr(feature = "nightly", feature(custom_test_frameworks, test))]
111// Documentation settings
112#![doc(html_root_url = "https://docs.rs/test-casing/0.1.3")]
113// Linter settings
114#![warn(missing_debug_implementations, missing_docs, bare_trait_objects)]
115#![warn(clippy::all, clippy::pedantic)]
116#![allow(clippy::must_use_candidate, clippy::module_name_repetitions)]
117
118/// Wraps a tested function to add retries, timeouts etc.
119///
120/// # Inputs
121///
122/// This attribute must be placed on a test function (i.e., one decorated with `#[test]`,
123/// `#[tokio::test]`, etc.). The attribute must be invoked with a comma-separated list
124/// of one or more [test decorators](decorators::DecorateTest). Each decorator must
125/// be a constant expression (i.e., it should be usable as a definition of a `static` variable).
126///
127/// # Examples
128///
129/// ## Basic usage
130///
131/// ```no_run
132/// use test_casing::{decorate, decorators::Timeout};
133///
134/// #[test]
135/// #[decorate(Timeout::secs(1))]
136/// fn test_with_timeout() {
137/// // test logic
138/// }
139/// ```
140///
141/// ## Tests returning `Result`s
142///
143/// Decorators can be used on tests returning `Result`s, too:
144///
145/// ```no_run
146/// use test_casing::{decorate, decorators::{Retry, Timeout}};
147/// use std::error::Error;
148///
149/// #[test]
150/// #[decorate(Timeout::millis(200), Retry::times(2))]
151/// // ^ Decorators are applied in the order of their mention. In this case,
152/// // if the test times out, errors or panics, it will be retried up to 2 times.
153/// fn test_with_retries() -> Result<(), Box<dyn Error + Send>> {
154/// // test logic
155/// # Ok(())
156/// }
157/// ```
158///
159/// ## Multiple `decorate` attributes
160///
161/// Multiple `decorate` attributes are allowed. Thus, the test above is equivalent to
162///
163/// ```no_run
164/// # use test_casing::{decorate, decorators::{Retry, Timeout}};
165/// # use std::error::Error;
166/// #[test]
167/// #[decorate(Timeout::millis(200))]
168/// #[decorate(Retry::times(2))]
169/// fn test_with_retries() -> Result<(), Box<dyn Error + Send>> {
170/// // test logic
171/// # Ok(())
172/// }
173/// ```
174///
175/// ## Async tests
176///
177/// Decorators work on async tests as well, as long as the `decorate` macro is applied after
178/// the test macro:
179///
180/// ```no_run
181/// # use test_casing::{decorate, decorators::Retry};
182/// #[tokio::test]
183/// #[decorate(Retry::times(3))]
184/// async fn async_test() {
185/// // test logic
186/// }
187/// ```
188///
189/// ## Composability and reuse
190///
191/// Decorators can be extracted to a `const`ant or a `static` for readability, composability
192/// and/or reuse:
193///
194/// ```no_run
195/// # use test_casing::{decorate, decorators::*};
196/// # use std::time::Duration;
197/// const RETRY: RetryErrors<String> = Retry::times(2)
198/// .with_delay(Duration::from_secs(1))
199/// .on_error(|s| s.contains("oops"));
200///
201/// static SEQUENCE: Sequence = Sequence::new().abort_on_failure();
202///
203/// #[test]
204/// #[decorate(RETRY, &SEQUENCE)]
205/// fn test_with_error_retries() -> Result<(), String> {
206/// // test logic
207/// # Ok(())
208/// }
209///
210/// #[test]
211/// #[decorate(&SEQUENCE)]
212/// fn other_test() {
213/// // test logic
214/// }
215/// ```
216///
217/// ## Use with `test_casing`
218///
219/// When used together with the [`test_casing`](macro@test_casing) macro, the decorators will apply
220/// to each generated case.
221///
222/// ```
223/// use test_casing::{decorate, test_casing, decorators::Timeout};
224///
225/// #[test_casing(3, [3, 5, 42])]
226/// #[decorate(Timeout::secs(1))]
227/// fn parameterized_test_with_timeout(input: u64) {
228/// // test logic
229/// }
230/// ```
231pub use test_casing_macro::decorate;
232/// Flattens a parameterized test into a collection of test cases.
233///
234/// # Inputs
235///
236/// This attribute must be placed on a freestanding function with 1..8 arguments.
237/// The attribute must be invoked with 2 values:
238///
239/// 1. Number of test cases, a number literal
240/// 2. A *case iterator* expression evaluating to an implementation of [`IntoIterator`]
241/// with [`Debug`]gable, `'static` items.
242/// If the target function has a single argument, the iterator item type must equal to
243/// the argument type. Otherwise, the iterator must return a tuple in which each item
244/// corresponds to the argument with the same index.
245///
246/// A case iterator expression may reference the environment (e.g., it can be a name of a constant).
247/// It doesn't need to be a constant expression (e.g., it may allocate in heap). It should
248/// return at least the number of items specified as the first attribute argument, and can
249/// return more items; these additional items will not be tested.
250///
251/// [`Debug`]: core::fmt::Debug
252///
253/// # Mapping arguments
254///
255/// To support more idiomatic signatures for parameterized test functions, it is possible
256/// to *map* from the type returned by the case iterator. The only supported kind of mapping
257/// so far is taking a shared reference (i.e., `T` → `&T`). The mapping is enabled by placing
258/// the `#[map(ref)]` attribute on the corresponding argument. Optionally, the reference `&T`
259/// can be further mapped with a function / method (e.g., `&String` → `&str` with
260/// [`String::as_str()`]). This is specified as `#[map(ref = path::to::method)]`, a la
261/// `serde` transforms.
262///
263/// # Examples
264///
265/// ## Basic usage
266///
267/// `test_casing` macro accepts 2 args: number of cases and the iterator expression.
268/// The latter can be any valid Rust expression.
269///
270/// ```
271/// # use test_casing::test_casing;
272/// #[test_casing(5, 0..5)]
273/// // #[test] attribute is optional and is added automatically
274/// // provided that the test function is not `async`.
275/// fn number_is_small(number: i32) {
276/// assert!(number < 10);
277/// }
278/// ```
279///
280/// Functions returning `Result`s are supported as well.
281///
282/// ```
283/// # use test_casing::test_casing;
284/// use std::error::Error;
285///
286/// #[test_casing(3, ["0", "42", "-3"])]
287/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
288/// let number: i32 = s.parse()?;
289/// assert!(number.abs() < 100);
290/// Ok(())
291/// }
292/// ```
293///
294/// The function on which the `test_casing` attribute is placed can be accessed from other code
295/// (e.g., for more tests):
296///
297/// ```no_run
298/// # use test_casing::test_casing;
299/// # use std::error::Error;
300/// #[test_casing(3, ["0", "42", "-3"])]
301/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
302/// // snipped...
303/// # Ok(())
304/// }
305///
306/// #[test]
307/// fn parsing_number_error() {
308/// assert!(parsing_numbers("?").is_err());
309/// }
310/// ```
311///
312/// The number of test cases is asserted in a separate generated test. This ensures that no cases
313/// are skipped by "underreporting" this count (e.g., if the test cases expression is extended).
314///
315/// ## Case expressions
316///
317/// Case expressions can be extracted to a constant for reuse or better code structuring.
318///
319/// ```
320/// # use test_casing::{cases, test_casing, TestCases};
321/// const CASES: TestCases<(String, i32)> = cases! {
322/// [0, 42, -3].map(|i| (i.to_string(), i))
323/// };
324///
325/// #[test_casing(3, CASES)]
326/// fn parsing_numbers(s: String, expected: i32) {
327/// let parsed: i32 = s.parse().unwrap();
328/// assert_eq!(parsed, expected);
329/// }
330/// ```
331///
332/// This example also shows that semantics of args is up to the writer; some of the args may be
333/// expected values, etc.
334///
335/// ## Cartesian product
336///
337/// One of possible case expressions is a [`Product`]; it can be used to generate test cases
338/// as a Cartesian product of the expressions for separate args.
339///
340/// ```
341/// # use test_casing::{test_casing, Product};
342/// #[test_casing(6, Product((0_usize..3, ["foo", "bar"])))]
343/// fn numbers_and_strings(number: usize, s: &str) {
344/// assert!(s.len() <= number);
345/// }
346/// ```
347///
348/// ## Reference args
349///
350/// It is possible to go from a generated argument to its reference by adding
351/// a `#[map(ref)]` attribute on the argument. The attribute may optionally specify
352/// a path to the transform function from the reference to the desired type
353/// (similar to transform specifications in the [`serde`](https://docs.rs/serde/) attr).
354///
355/// ```
356/// # use test_casing::{cases, test_casing, TestCases};
357/// const CASES: TestCases<(String, i32)> = cases! {
358/// [0, 42, -3].map(|i| (i.to_string(), i))
359/// };
360///
361/// #[test_casing(3, CASES)]
362/// fn parsing_numbers(#[map(ref)] s: &str, expected: i32) {
363/// // Snipped...
364/// }
365///
366/// #[test_casing(3, CASES)]
367/// fn parsing_numbers_too(
368/// #[map(ref = String::as_str)] s: &str,
369/// expected: i32,
370/// ) {
371/// // Snipped...
372/// }
373/// ```
374///
375/// ## `ignore` and `should_panic` attributes
376///
377/// `ignore` or `should_panic` attributes can be specified below the `test_casing` attribute.
378/// They will apply to all generated tests.
379///
380/// ```
381/// # use test_casing::test_casing;
382/// #[test_casing(3, ["not", "implemented", "yet"])]
383/// #[ignore = "Promise this will work sometime"]
384/// fn future_test(s: &str) {
385/// unimplemented!()
386/// }
387///
388/// #[test_casing(3, ["not a number", "-", ""])]
389/// #[should_panic(expected = "ParseIntError")]
390/// fn string_conversion_fail(bogus_str: &str) {
391/// bogus_str.parse::<i32>().unwrap();
392/// }
393/// ```
394///
395/// ## Async tests
396///
397/// `test_casing` supports all kinds of async test wrappers, such as `async_std::test`,
398/// `tokio::test`, `actix::test` etc. The corresponding attribute just needs to be specified
399/// *below* the `test_casing` attribute.
400///
401/// ```
402/// # use test_casing::test_casing;
403/// # use std::error::Error;
404/// #[test_casing(3, ["0", "42", "-3"])]
405/// #[async_std::test]
406/// async fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
407/// assert!(s.parse::<i32>()?.abs() < 100);
408/// Ok(())
409/// }
410/// ```
411pub use test_casing_macro::test_casing;
412
413pub mod decorators;
414#[cfg(feature = "nightly")]
415#[doc(hidden)] // used by the `#[test_casing]` macro; logically private
416pub mod nightly;
417mod test_casing;
418
419pub use crate::test_casing::{assert_case_count, case, ArgNames, Product, ProductIter, TestCases};