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
233/// Flattens a parameterized test into a collection of test cases.
234///
235/// # Inputs
236///
237/// This attribute must be placed on a freestanding function with 1..8 arguments.
238/// The attribute must be invoked with 2 values:
239///
240/// 1. Number of test cases, a number literal
241/// 2. A *case iterator* expression evaluating to an implementation of [`IntoIterator`]
242///    with [`Debug`]gable, `'static` items.
243///    If the target function has a single argument, the iterator item type must equal to
244///    the argument type. Otherwise, the iterator must return a tuple in which each item
245///    corresponds to the argument with the same index.
246///
247/// A case iterator expression may reference the environment (e.g., it can be a name of a constant).
248/// It doesn't need to be a constant expression (e.g., it may allocate in heap). It should
249/// return at least the number of items specified as the first attribute argument, and can
250/// return more items; these additional items will not be tested.
251///
252/// [`Debug`]: core::fmt::Debug
253///
254/// # Mapping arguments
255///
256/// To support more idiomatic signatures for parameterized test functions, it is possible
257/// to *map* from the type returned by the case iterator. The only supported kind of mapping
258/// so far is taking a shared reference (i.e., `T` → `&T`). The mapping is enabled by placing
259/// the `#[map(ref)]` attribute on the corresponding argument. Optionally, the reference `&T`
260/// can be further mapped with a function / method (e.g., `&String` → `&str` with
261/// [`String::as_str()`]). This is specified as `#[map(ref = path::to::method)]`, a la
262/// `serde` transforms.
263///
264/// # Examples
265///
266/// ## Basic usage
267///
268/// `test_casing` macro accepts 2 args: number of cases and the iterator expression.
269/// The latter can be any valid Rust expression.
270///
271/// ```
272/// # use test_casing::test_casing;
273/// #[test_casing(5, 0..5)]
274/// // #[test] attribute is optional and is added automatically
275/// // provided that the test function is not `async`.
276/// fn number_is_small(number: i32) {
277///     assert!(number < 10);
278/// }
279/// ```
280///
281/// Functions returning `Result`s are supported as well.
282///
283/// ```
284/// # use test_casing::test_casing;
285/// use std::error::Error;
286///
287/// #[test_casing(3, ["0", "42", "-3"])]
288/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
289///     let number: i32 = s.parse()?;
290///     assert!(number.abs() < 100);
291///     Ok(())
292/// }
293/// ```
294///
295/// The function on which the `test_casing` attribute is placed can be accessed from other code
296/// (e.g., for more tests):
297///
298/// ```no_run
299/// # use test_casing::test_casing;
300/// # use std::error::Error;
301/// #[test_casing(3, ["0", "42", "-3"])]
302/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
303///     // snipped...
304/// #   Ok(())
305/// }
306///
307/// #[test]
308/// fn parsing_number_error() {
309///     assert!(parsing_numbers("?").is_err());
310/// }
311/// ```
312///
313/// ## Case expressions
314///
315/// Case expressions can be extracted to a constant for reuse or better code structuring.
316///
317/// ```
318/// # use test_casing::{cases, test_casing, TestCases};
319/// const CASES: TestCases<(String, i32)> = cases! {
320///     [0, 42, -3].map(|i| (i.to_string(), i))
321/// };
322///
323/// #[test_casing(3, CASES)]
324/// fn parsing_numbers(s: String, expected: i32) {
325///     let parsed: i32 = s.parse().unwrap();
326///     assert_eq!(parsed, expected);
327/// }
328/// ```
329///
330/// This example also shows that semantics of args is up to the writer; some of the args may be
331/// expected values, etc.
332///
333/// ## Cartesian product
334///
335/// One of possible case expressions is a [`Product`]; it can be used to generate test cases
336/// as a Cartesian product of the expressions for separate args.
337///
338/// ```
339/// # use test_casing::{test_casing, Product};
340/// #[test_casing(6, Product((0_usize..3, ["foo", "bar"])))]
341/// fn numbers_and_strings(number: usize, s: &str) {
342///     assert!(s.len() <= number);
343/// }
344/// ```
345///
346/// ## Reference args
347///
348/// It is possible to go from a generated argument to its reference by adding
349/// a `#[map(ref)]` attribute on the argument. The attribute may optionally specify
350/// a path to the transform function from the reference to the desired type
351/// (similar to transform specifications in the [`serde`](https://docs.rs/serde/) attr).
352///
353/// ```
354/// # use test_casing::{cases, test_casing, TestCases};
355/// const CASES: TestCases<(String, i32)> = cases! {
356///     [0, 42, -3].map(|i| (i.to_string(), i))
357/// };
358///
359/// #[test_casing(3, CASES)]
360/// fn parsing_numbers(#[map(ref)] s: &str, expected: i32) {
361///     // Snipped...
362/// }
363///
364/// #[test_casing(3, CASES)]
365/// fn parsing_numbers_too(
366///     #[map(ref = String::as_str)] s: &str,
367///     expected: i32,
368/// ) {
369///     // Snipped...
370/// }
371/// ```
372///
373/// ## `ignore` and `should_panic` attributes
374///
375/// `ignore` or `should_panic` attributes can be specified below the `test_casing` attribute.
376/// They will apply to all generated tests.
377///
378/// ```
379/// # use test_casing::test_casing;
380/// #[test_casing(3, ["not", "implemented", "yet"])]
381/// #[ignore = "Promise this will work sometime"]
382/// fn future_test(s: &str) {
383///     unimplemented!()
384/// }
385///
386/// #[test_casing(3, ["not a number", "-", ""])]
387/// #[should_panic(expected = "ParseIntError")]
388/// fn string_conversion_fail(bogus_str: &str) {
389///     bogus_str.parse::<i32>().unwrap();
390/// }
391/// ```
392///
393/// ## Async tests
394///
395/// `test_casing` supports all kinds of async test wrappers, such as `async_std::test`,
396/// `tokio::test`, `actix::test` etc. The corresponding attribute just needs to be specified
397/// *below* the `test_casing` attribute.
398///
399/// ```
400/// # use test_casing::test_casing;
401/// # use std::error::Error;
402/// #[test_casing(3, ["0", "42", "-3"])]
403/// #[async_std::test]
404/// async fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
405///     assert!(s.parse::<i32>()?.abs() < 100);
406///     Ok(())
407/// }
408/// ```
409pub use test_casing_macro::test_casing;
410
411pub mod decorators;
412#[cfg(feature = "nightly")]
413#[doc(hidden)] // used by the `#[test_casing]` macro; logically private
414pub mod nightly;
415mod test_casing;
416
417pub use crate::test_casing::{case, ArgNames, Product, ProductIter, TestCases};