#generic #generic-type #parameters #macro #type #generic-parameters

macro typed_test_gen

Macros to help generate tests from functions with a generic type parameter

4 releases

0.1.3 Oct 3, 2025
0.1.2 Oct 1, 2024
0.1.1 Jul 20, 2024
0.1.0 Sep 1, 2023

#1289 in Testing

Download history 620/week @ 2025-09-29 469/week @ 2025-10-06 431/week @ 2025-10-13 348/week @ 2025-10-20 585/week @ 2025-10-27 628/week @ 2025-11-03 555/week @ 2025-11-10 347/week @ 2025-11-17 355/week @ 2025-11-24 270/week @ 2025-12-01 206/week @ 2025-12-08 369/week @ 2025-12-15 89/week @ 2025-12-22 70/week @ 2025-12-29 183/week @ 2026-01-05 221/week @ 2026-01-12

573 downloads per month
Used in 2 crates

GPL-3.0-only

17KB
136 lines

typed_test_gen

crate documentation

Rust crate for defining a macro that automatically specializes generic tests on provided types. This is useful when we want to write identical tests with different underlying types. This crate provide the syntax #[test_with(Type1, Type2)] which will instantiate 2 tests: one on Type1, the other on Type2. Those types could be any path to any generic type like module::something::Type<Gen>.

An example is better than words:

#[test_with(u32, u64, char)]
fn test_vec<T>() {
    let vec = Vec::<T>::with_capacity(10);
    assert_eq!(vec.len(), 0);
    assert!(vec.capacity() >= 10);
}

This code will generate 3 tests function: _specialized__test_vec__u32_, _specialized__test_vec__u64_, and _specialized__test_vec__char_.

This support adding the attribute #[should_panic] to the definition.

#[test_with(u32, u64, char)]
#[should_panic]
fn test_vec_fail<T>() {
    let vec = Vec::<T>::with_capacity(10);
    assert_eq!(vec.len(), 0);
    assert!(vec.capacity() < 10);
}

Dependencies

~135–530KB
~13K SLoC