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/// ```
132/// use test_casing::{decorate, decorators::Timeout};
133///
134/// #[test]
135/// # fn eat_test_attribute() {}
136/// #[decorate(Timeout::secs(1))]
137/// fn test_with_timeout() {
138/// // test logic
139/// }
140/// ```
141///
142/// ## Tests returning `Result`s
143///
144/// Decorators can be used on tests returning `Result`s, too:
145///
146/// ```
147/// use test_casing::{decorate, decorators::{Retry, Timeout}};
148/// use std::error::Error;
149///
150/// #[test]
151/// # fn eat_test_attribute() {}
152/// #[decorate(Timeout::millis(200), Retry::times(2))]
153/// // ^ Decorators are applied in the order of their mention. In this case,
154/// // if the test times out, errors or panics, it will be retried up to 2 times.
155/// fn test_with_retries() -> Result<(), Box<dyn Error + Send>> {
156/// // test logic
157/// # Ok(())
158/// }
159/// ```
160///
161/// ## Multiple `decorate` attributes
162///
163/// Multiple `decorate` attributes are allowed. Thus, the test above is equivalent to
164///
165/// ```
166/// # use test_casing::{decorate, decorators::{Retry, Timeout}};
167/// # use std::error::Error;
168/// #[test]
169/// # fn eat_test_attribute() {}
170/// #[decorate(Timeout::millis(200))]
171/// #[decorate(Retry::times(2))]
172/// fn test_with_retries() -> Result<(), Box<dyn Error + Send>> {
173/// // test logic
174/// # Ok(())
175/// }
176/// ```
177///
178/// ## Async tests
179///
180/// Decorators work on async tests as well, as long as the `decorate` macro is applied after
181/// the test macro:
182///
183/// ```
184/// # use test_casing::{decorate, decorators::Retry};
185/// #[async_std::test]
186/// #[decorate(Retry::times(3))]
187/// async fn async_test() {
188/// // test logic
189/// }
190/// ```
191///
192/// ## Composability and reuse
193///
194/// Decorators can be extracted to a `const`ant or a `static` for readability, composability
195/// and/or reuse:
196///
197/// ```
198/// # use test_casing::{decorate, decorators::*};
199/// # use std::time::Duration;
200/// const RETRY: RetryErrors<String> = Retry::times(2)
201/// .with_delay(Duration::from_secs(1))
202/// .on_error(|s| s.contains("oops"));
203///
204/// static SEQUENCE: Sequence = Sequence::new().abort_on_failure();
205///
206/// #[test]
207/// # fn eat_test_attribute() {}
208/// #[decorate(RETRY, &SEQUENCE)]
209/// fn test_with_error_retries() -> Result<(), String> {
210/// // test logic
211/// # Ok(())
212/// }
213///
214/// #[test]
215/// # fn eat_test_attribute2() {}
216/// #[decorate(&SEQUENCE)]
217/// fn other_test() {
218/// // test logic
219/// }
220/// ```
221///
222/// ## Use with `test_casing`
223///
224/// When used together with the [`test_casing`](macro@test_casing) macro, the decorators will apply
225/// to each generated case.
226///
227/// ```
228/// use test_casing::{decorate, test_casing, decorators::Timeout};
229///
230/// #[test_casing(3, [3, 5, 42])]
231/// #[decorate(Timeout::secs(1))]
232/// fn parameterized_test_with_timeout(input: u64) {
233/// // test logic
234/// }
235/// ```
236pub use test_casing_macro::decorate;
237
238/// Flattens a parameterized test into a collection of test cases.
239///
240/// # Inputs
241///
242/// This attribute must be placed on a free-standing function with 1..8 arguments.
243/// The attribute must be invoked with 2 values:
244///
245/// 1. Number of test cases, a number literal
246/// 2. A *case iterator* expression evaluating to an implementation of [`IntoIterator`]
247/// with [`Debug`]gable, `'static` items.
248/// If the target function has a single argument, the iterator item type must equal to
249/// the argument type. Otherwise, the iterator must return a tuple in which each item
250/// corresponds to the argument with the same index.
251///
252/// A case iterator expression may reference the environment (e.g., it can be a name of a constant).
253/// It doesn't need to be a constant expression (e.g., it may allocate in heap). It should
254/// return at least the number of items specified as the first attribute argument, and can
255/// return more items; these additional items will not be tested.
256///
257/// [`Debug`]: core::fmt::Debug
258///
259/// # Mapping arguments
260///
261/// To support more idiomatic signatures for parameterized test functions, it is possible
262/// to *map* from the type returned by the case iterator. The only supported kind of mapping
263/// so far is taking a shared reference (i.e., `T` → `&T`). The mapping is enabled by placing
264/// the `#[map(ref)]` attribute on the corresponding argument. Optionally, the reference `&T`
265/// can be further mapped with a function / method (e.g., `&String` → `&str` with
266/// [`String::as_str()`]). This is specified as `#[map(ref = path::to::method)]`, a la
267/// `serde` transforms.
268///
269/// # Examples
270///
271/// ## Basic usage
272///
273/// `test_casing` macro accepts 2 args: number of cases and the iterator expression.
274/// The latter can be any valid Rust expression.
275///
276/// ```
277/// # use test_casing::test_casing;
278/// #[test_casing(5, 0..5)]
279/// // #[test] attribute is optional and is added automatically
280/// // provided that the test function is not `async`.
281/// fn number_is_small(number: i32) {
282/// assert!(number < 10);
283/// }
284/// ```
285///
286/// Functions returning `Result`s are supported as well.
287///
288/// ```
289/// # use test_casing::test_casing;
290/// use std::error::Error;
291///
292/// #[test_casing(3, ["0", "42", "-3"])]
293/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
294/// let number: i32 = s.parse()?;
295/// assert!(number.abs() < 100);
296/// Ok(())
297/// }
298/// ```
299///
300/// The function on which the `test_casing` attribute is placed can be accessed from other code
301/// (e.g., for more tests):
302///
303/// ```
304/// # use test_casing::test_casing;
305/// # use std::error::Error;
306/// #[test_casing(3, ["0", "42", "-3"])]
307/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
308/// // snipped...
309/// # Ok(())
310/// }
311///
312/// #[test]
313/// fn parsing_number_error() {
314/// assert!(parsing_numbers("?").is_err());
315/// }
316/// ```
317///
318/// ## Case expressions
319///
320/// Case expressions can be extracted to a constant for reuse or better code structuring.
321///
322/// ```
323/// # use test_casing::{cases, test_casing, TestCases};
324/// const CASES: TestCases<(String, i32)> = cases! {
325/// [0, 42, -3].map(|i| (i.to_string(), i))
326/// };
327///
328/// #[test_casing(3, CASES)]
329/// fn parsing_numbers(s: String, expected: i32) {
330/// let parsed: i32 = s.parse().unwrap();
331/// assert_eq!(parsed, expected);
332/// }
333/// ```
334///
335/// This example also shows that semantics of args is up to the writer; some of the args may be
336/// expected values, etc.
337///
338/// ## Cartesian product
339///
340/// One of possible case expressions is a [`Product`]; it can be used to generate test cases
341/// as a Cartesian product of the expressions for separate args.
342///
343/// ```
344/// # use test_casing::{test_casing, Product};
345/// #[test_casing(6, Product((0_usize..3, ["foo", "bar"])))]
346/// fn numbers_and_strings(number: usize, s: &str) {
347/// assert!(s.len() <= number);
348/// }
349/// ```
350///
351/// ## Reference args
352///
353/// It is possible to go from a generated argument to its reference by adding
354/// a `#[map(ref)]` attribute on the argument. The attribute may optionally specify
355/// a path to the transform function from the reference to the desired type
356/// (similar to transform specifications in the [`serde`](https://docs.rs/serde/) attr).
357///
358/// ```
359/// # use test_casing::{cases, test_casing, TestCases};
360/// const CASES: TestCases<(String, i32)> = cases! {
361/// [0, 42, -3].map(|i| (i.to_string(), i))
362/// };
363///
364/// #[test_casing(3, CASES)]
365/// fn parsing_numbers(#[map(ref)] s: &str, expected: i32) {
366/// // Snipped...
367/// }
368///
369/// #[test_casing(3, CASES)]
370/// fn parsing_numbers_too(
371/// #[map(ref = String::as_str)] s: &str,
372/// expected: i32,
373/// ) {
374/// // Snipped...
375/// }
376/// ```
377///
378/// ## `ignore` and `should_panic` attributes
379///
380/// `ignore` or `should_panic` attributes can be specified below the `test_casing` attribute.
381/// They will apply to all generated tests.
382///
383/// ```
384/// # use test_casing::test_casing;
385/// #[test_casing(3, ["not", "implemented", "yet"])]
386/// #[ignore = "Promise this will work sometime"]
387/// fn future_test(s: &str) {
388/// unimplemented!()
389/// }
390///
391/// #[test_casing(3, ["not a number", "-", ""])]
392/// #[should_panic(expected = "ParseIntError")]
393/// fn string_conversion_fail(bogus_str: &str) {
394/// bogus_str.parse::<i32>().unwrap();
395/// }
396/// ```
397///
398/// ## Async tests
399///
400/// `test_casing` supports all kinds of async test wrappers, such as `async_std::test`,
401/// `tokio::test`, `actix::test` etc. The corresponding attribute just needs to be specified
402/// *below* the `test_casing` attribute.
403///
404/// ```
405/// # use test_casing::test_casing;
406/// # use std::error::Error;
407/// #[test_casing(3, ["0", "42", "-3"])]
408/// #[async_std::test]
409/// async fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
410/// assert!(s.parse::<i32>()?.abs() < 100);
411/// Ok(())
412/// }
413/// ```
414pub use test_casing_macro::test_casing;
415
416pub mod decorators;
417#[cfg(feature = "nightly")]
418#[doc(hidden)] // used by the `#[test_casing]` macro; logically private
419pub mod nightly;
420mod test_casing;
421
422pub use crate::test_casing::{case, ArgNames, Product, ProductIter, TestCases};