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//! ## `tracing`
108//!
109//! *(Off by default)*
110//!
111//! Enables tracing / logging using [the `tracing` library](https://docs.rs/tracing/). This includes
112//! the [`Trace`](decorators::Trace) decorator.
113//!
114//! [custom test frameworks]: https://github.com/rust-lang/rust/issues/50297
115//! [the CI config]: https://github.com/slowli/test-casing/blob/main/.github/workflows/ci.yml
116
117#![cfg_attr(feature = "nightly", feature(custom_test_frameworks, test))]
118// Documentation settings
119#![cfg_attr(docsrs, feature(doc_cfg))]
120#![doc(html_root_url = "https://docs.rs/test-casing/0.2.0-beta.1")]
121
122/// Wraps a tested function to add retries, timeouts etc.
123///
124/// # Inputs
125///
126/// This attribute must be placed on a test function (i.e., one decorated with `#[test]`,
127/// `#[tokio::test]`, etc.). The attribute must be invoked with a comma-separated list
128/// of one or more [test decorators](decorators::DecorateTest). Each decorator must
129/// be a constant expression (i.e., it should be usable as a definition of a `static` variable).
130///
131/// # Examples
132///
133/// ## Basic usage
134///
135/// ```no_run
136/// use test_casing::{decorate, decorators::Timeout};
137///
138/// #[test]
139/// #[decorate(Timeout::secs(1))]
140/// fn test_with_timeout() {
141/// // test logic
142/// }
143/// ```
144///
145/// ## Tests returning `Result`s
146///
147/// Decorators can be used on tests returning `Result`s, too:
148///
149/// ```no_run
150/// use test_casing::{decorate, decorators::{Retry, Timeout}};
151/// use std::error::Error;
152///
153/// #[test]
154/// #[decorate(Timeout::millis(200), Retry::times(2))]
155/// // ^ Decorators are applied in the order of their mention. In this case,
156/// // if the test times out, errors or panics, it will be retried up to 2 times.
157/// fn test_with_retries() -> Result<(), Box<dyn Error + Send>> {
158/// // test logic
159/// # Ok(())
160/// }
161/// ```
162///
163/// ## Multiple `decorate` attributes
164///
165/// Multiple `decorate` attributes are allowed. Thus, the test above is equivalent to
166///
167/// ```no_run
168/// # use test_casing::{decorate, decorators::{Retry, Timeout}};
169/// # use std::error::Error;
170/// #[test]
171/// #[decorate(Timeout::millis(200))]
172/// #[decorate(Retry::times(2))]
173/// fn test_with_retries() -> Result<(), Box<dyn Error + Send>> {
174/// // test logic
175/// # Ok(())
176/// }
177/// ```
178///
179/// ## Async tests
180///
181/// Decorators work on async tests as well, as long as the `decorate` macro is applied after
182/// the test macro:
183///
184/// ```no_run
185/// # use test_casing::{decorate, decorators::Retry};
186/// #[tokio::test]
187/// #[decorate(Retry::times(3))]
188/// async fn async_test() {
189/// // test logic
190/// }
191/// ```
192///
193/// ## Composability and reuse
194///
195/// Decorators can be extracted to a `const`ant or a `static` for readability, composability
196/// and/or reuse:
197///
198/// ```no_run
199/// # use test_casing::{decorate, decorators::*};
200/// # use std::time::Duration;
201/// const RETRY: RetryErrors<String> = Retry::times(2)
202/// .with_delay(Duration::from_secs(1))
203/// .on_error(|s| s.contains("oops"));
204///
205/// static SEQUENCE: Sequence = Sequence::new().abort_on_failure();
206///
207/// #[test]
208/// #[decorate(RETRY, &SEQUENCE)]
209/// fn test_with_error_retries() -> Result<(), String> {
210/// // test logic
211/// # Ok(())
212/// }
213///
214/// #[test]
215/// #[decorate(&SEQUENCE)]
216/// fn other_test() {
217/// // test logic
218/// }
219/// ```
220///
221/// ## Use with `test_casing`
222///
223/// When used together with the [`test_casing`](macro@test_casing) macro, the decorators will apply
224/// to each generated case.
225///
226/// ```
227/// use test_casing::{decorate, test_casing, decorators::Timeout};
228///
229/// #[test_casing(3, [3, 5, 42])]
230/// #[decorate(Timeout::secs(1))]
231/// fn parameterized_test_with_timeout(input: u64) {
232/// // test logic
233/// }
234/// ```
235pub use test_casing_macro::decorate;
236/// Flattens a parameterized test into a collection of test cases.
237///
238/// # Inputs
239///
240/// This attribute must be placed on a freestanding function with 1..8 arguments.
241/// The attribute must be invoked with 2 values:
242///
243/// 1. Number of test cases, a number literal
244/// 2. A *case iterator* expression evaluating to an implementation of [`IntoIterator`]
245/// with [`Debug`]gable, `'static` items.
246/// If the target function has a single argument, the iterator item type must equal to
247/// the argument type. Otherwise, the iterator must return a tuple in which each item
248/// corresponds to the argument with the same index.
249///
250/// A case iterator expression may reference the environment (e.g., it can be a name of a constant).
251/// It doesn't need to be a constant expression (e.g., it may allocate in heap). It should
252/// return at least the number of items specified as the first attribute argument, and can
253/// return more items; these additional items will not be tested.
254///
255/// [`Debug`]: core::fmt::Debug
256///
257/// # Mapping arguments
258///
259/// To support more idiomatic signatures for parameterized test functions, it is possible
260/// to *map* from the type returned by the case iterator. The only supported kind of mapping
261/// so far is taking a shared reference (i.e., `T` → `&T`). The mapping is enabled by placing
262/// the `#[map(ref)]` attribute on the corresponding argument. Optionally, the reference `&T`
263/// can be further mapped with a function / method (e.g., `&String` → `&str` with
264/// [`String::as_str()`]). This is specified as `#[map(ref = path::to::method)]`, a la
265/// `serde` transforms.
266///
267/// # Examples
268///
269/// ## Basic usage
270///
271/// `test_casing` macro accepts 2 args: number of cases and the iterator expression.
272/// The latter can be any valid Rust expression.
273///
274/// ```
275/// # use test_casing::test_casing;
276/// #[test_casing(5, 0..5)]
277/// // #[test] attribute is optional and is added automatically
278/// // provided that the test function is not `async`.
279/// fn number_is_small(number: i32) {
280/// assert!(number < 10);
281/// }
282/// ```
283///
284/// Functions returning `Result`s are supported as well.
285///
286/// ```
287/// # use test_casing::test_casing;
288/// use std::error::Error;
289///
290/// #[test_casing(3, ["0", "42", "-3"])]
291/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
292/// let number: i32 = s.parse()?;
293/// assert!(number.abs() < 100);
294/// Ok(())
295/// }
296/// ```
297///
298/// The function on which the `test_casing` attribute is placed can be accessed from other code
299/// (e.g., for more tests):
300///
301/// ```no_run
302/// # use test_casing::test_casing;
303/// # use std::error::Error;
304/// #[test_casing(3, ["0", "42", "-3"])]
305/// fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
306/// // snipped...
307/// # Ok(())
308/// }
309///
310/// #[test]
311/// fn parsing_number_error() {
312/// assert!(parsing_numbers("?").is_err());
313/// }
314/// ```
315///
316/// The number of test cases is asserted in a separate generated test. This ensures that no cases
317/// are skipped by "underreporting" this count (e.g., if the test cases expression is extended).
318///
319/// ## Case expressions
320///
321/// Case expressions can be extracted to a constant for reuse or better code structuring.
322///
323/// ```
324/// # use test_casing::{cases, test_casing, TestCases};
325/// const CASES: TestCases<(String, i32)> = cases! {
326/// [0, 42, -3].map(|i| (i.to_string(), i))
327/// };
328///
329/// #[test_casing(3, CASES)]
330/// fn parsing_numbers(s: String, expected: i32) {
331/// let parsed: i32 = s.parse().unwrap();
332/// assert_eq!(parsed, expected);
333/// }
334/// ```
335///
336/// This example also shows that semantics of args is up to the writer; some of the args may be
337/// expected values, etc.
338///
339/// ## Cartesian product
340///
341/// One of possible case expressions is a [`Product`]; it can be used to generate test cases
342/// as a Cartesian product of the expressions for separate args.
343///
344/// ```
345/// # use test_casing::{test_casing, Product};
346/// #[test_casing(6, Product((0_usize..3, ["foo", "bar"])))]
347/// fn numbers_and_strings(number: usize, s: &str) {
348/// assert!(s.len() <= number);
349/// }
350/// ```
351///
352/// ## Reference args
353///
354/// It is possible to go from a generated argument to its reference by adding
355/// a `#[map(ref)]` attribute on the argument. The attribute may optionally specify
356/// a path to the transform function from the reference to the desired type
357/// (similar to transform specifications in the [`serde`](https://docs.rs/serde/) attr).
358///
359/// ```
360/// # use test_casing::{cases, test_casing, TestCases};
361/// const CASES: TestCases<(String, i32)> = cases! {
362/// [0, 42, -3].map(|i| (i.to_string(), i))
363/// };
364///
365/// #[test_casing(3, CASES)]
366/// fn parsing_numbers(#[map(ref)] s: &str, expected: i32) {
367/// // Snipped...
368/// }
369///
370/// #[test_casing(3, CASES)]
371/// fn parsing_numbers_too(
372/// #[map(ref = String::as_str)] s: &str,
373/// expected: i32,
374/// ) {
375/// // Snipped...
376/// }
377/// ```
378///
379/// ## `ignore` and `should_panic` attributes
380///
381/// `ignore` or `should_panic` attributes can be specified below the `test_casing` attribute.
382/// They will apply to all generated tests.
383///
384/// ```
385/// # use test_casing::test_casing;
386/// #[test_casing(3, ["not", "implemented", "yet"])]
387/// #[ignore = "Promise this will work sometime"]
388/// fn future_test(s: &str) {
389/// unimplemented!()
390/// }
391///
392/// #[test_casing(3, ["not a number", "-", ""])]
393/// #[should_panic(expected = "ParseIntError")]
394/// fn string_conversion_fail(bogus_str: &str) {
395/// bogus_str.parse::<i32>().unwrap();
396/// }
397/// ```
398///
399/// ## Async tests
400///
401/// `test_casing` supports all kinds of async test wrappers, such as `async_std::test`,
402/// `tokio::test`, `actix::test` etc. The corresponding attribute just needs to be specified
403/// *below* the `test_casing` attribute.
404///
405/// ```
406/// # use test_casing::test_casing;
407/// # use std::error::Error;
408/// #[test_casing(3, ["0", "42", "-3"])]
409/// #[async_std::test]
410/// async fn parsing_numbers(s: &str) -> Result<(), Box<dyn Error>> {
411/// assert!(s.parse::<i32>()?.abs() < 100);
412/// Ok(())
413/// }
414/// ```
415pub use test_casing_macro::test_casing;
416
417#[cfg(any(feature = "nightly", not(feature = "tracing")))]
418mod arg_names;
419pub mod decorators;
420#[cfg(feature = "nightly")]
421#[doc(hidden)] // used by the `#[test_casing]` macro; logically private
422pub mod nightly;
423mod test_casing;
424
425pub use crate::test_casing::{Product, ProductIter, TestCases};
426
427#[doc(hidden)] // only should be used by proc macros
428pub mod _private {
429 #[cfg(feature = "tracing")]
430 pub use tracing::{info, info_span};
431
432 #[cfg(not(feature = "tracing"))]
433 pub use crate::arg_names::ArgNames;
434 pub use crate::test_casing::{assert_case_count, case};
435
436 #[cfg(not(feature = "tracing"))]
437 #[macro_export]
438 #[doc(hidden)] // Used in the proc macros; logically private
439 macro_rules! __describe_test_case {
440 ($name:tt, $index:tt, $arg:tt = $val:expr,) => {
441 ::std::println!(
442 "Testing case #{}: {}",
443 $index,
444 $crate::_private::ArgNames::print_with_args([$arg], $val)
445 );
446 };
447 ($name:tt, $index:tt, $($arg:tt = $val:expr,)+) => {
448 ::std::println!(
449 "Testing case #{}: {}",
450 $index,
451 $crate::_private::ArgNames::print_with_args([$($arg,)+], ($(&$val,)+))
452 );
453 };
454 }
455
456 /// Strips the repeating module name at the end of `path`.
457 #[cfg(feature = "tracing")]
458 pub const fn parent_module(path: &'static str, test_name: &'static str) -> &'static str {
459 const fn str_eq(x: &str, y: &str) -> bool {
460 if x.len() != y.len() {
461 return false;
462 }
463
464 let x = x.as_bytes();
465 let y = y.as_bytes();
466 let mut i = 0;
467 while i < x.len() {
468 if x[i] != y[i] {
469 return false;
470 }
471 i += 1;
472 }
473
474 true
475 }
476
477 let Some((head, test_module)) = split_last(path) else {
478 return path;
479 };
480
481 if str_eq(test_module, test_name) {
482 head
483 } else {
484 path
485 }
486 }
487
488 #[cfg(feature = "tracing")]
489 const fn split_last(path: &str) -> Option<(&str, &str)> {
490 let path_bytes = path.as_bytes();
491 let mut i = path_bytes.len().checked_sub(1);
492 let mut found = None;
493 while let Some(pos) = i {
494 if path_bytes[pos] == b':' && path_bytes[pos + 1] == b':' {
495 found = Some(pos);
496 break;
497 }
498 i = pos.checked_sub(1);
499 }
500
501 if let Some(pos) = found {
502 let (head_bytes, tail_bytes) = path_bytes.split_at(pos);
503 let (_, tail_bytes) = tail_bytes.split_at(2); // remove `::` at the start
504 Some(unsafe {
505 // SAFETY: `path_bytes` is a valid UTF-8 string, and we split it at a char boundary.
506 (
507 core::str::from_utf8_unchecked(head_bytes),
508 core::str::from_utf8_unchecked(tail_bytes),
509 )
510 })
511 } else {
512 None
513 }
514 }
515
516 #[cfg(feature = "tracing")]
517 #[macro_export]
518 #[doc(hidden)] // Used in the proc macros; logically private
519 macro_rules! __describe_test_case {
520 ($name:tt, $index:tt, $($arg:tt = $val:expr,)+) => {{
521 const TARGET: &'static str = $crate::_private::parent_module(::core::module_path!(), ::core::stringify!($name));
522
523 let _guard = $crate::_private::info_span!(
524 target: TARGET,
525 ::core::stringify!($name),
526 case.index = $index,
527 $($arg = ?$val,)+
528 )
529 .entered();
530
531 $crate::_private::info!(target: TARGET, "started test");
532 _guard
533 }}
534 }
535}