#test-macro #harness #proc-macro #setup #macro #test

macro dev test-harness

a little test macro to wrap your tests with an arbitrary setup/teardown function

4 releases (2 breaking)

0.3.0 Jun 18, 2024
0.2.0 Jan 2, 2024
0.1.1 Apr 3, 2023
0.1.0 Mar 31, 2023

#314 in Testing

Download history 923/week @ 2024-09-27 1179/week @ 2024-10-04 1759/week @ 2024-10-11 1492/week @ 2024-10-18 1753/week @ 2024-10-25 1649/week @ 2024-11-01 1519/week @ 2024-11-08 1664/week @ 2024-11-15 1895/week @ 2024-11-22 1535/week @ 2024-11-29 1652/week @ 2024-12-06 1455/week @ 2024-12-13 1692/week @ 2024-12-20 1301/week @ 2024-12-27 2083/week @ 2025-01-03 1058/week @ 2025-01-10

6,466 downloads per month
Used in 12 crates

MIT/Apache

14KB
99 lines

test-harness

this proc macro wraps your tests with any function that accepts a function with your test's signature. Your test function can accept any number of arguments and return anything, as long as you call it with the correct arguments in the harness.

Example

use test_harness::test;

fn my_test_harness(test: impl FnOnce(String)) {
    let string = std::iter::repeat_with(fastrand::alphanumeric).take(10).collect();
    test(string)
}

#[test(harness = my_test_harness)]
fn my_test(random_string: String) {
    assert_eq!(string.len(), 10);
}

This expands to the following, with no further macro magic

fn my_test_harness<F>(test: impl FnOnce(String)) {
    let string = std::iter::repeat_with(fastrand::alphanumeric).take(10).collect();
    test(string)
}

#[test]
fn my_test() -> impl std::process::Termination {
    fn my_test(random_string: String) -> Result<(), &'static str> {
        assert_eq!(string.len(), 10);
        Ok(())
    }
    my_test_harness(my_test)
}

Returning Result

use test_harness::test;

fn my_test_harness<F>(test: F) -> Result<(), &'static str>
where F: FnOnce(String) -> Result<(), &'static str> {
    let string = std::iter::repeat_with(fastrand::alphanumeric).take(10).collect();
    test(string)
}

#[test(harness = my_test_harness)]
fn my_test(random_string: String) -> Result<(), &'static str> {
    assert_eq!(string.len(), 10);
    Ok(())
}

This expands to the following, with no further macro magic

fn my_test_harness<F>(test: F) -> Result<(), &'static str>
where F: FnOnce(String) -> Result<(), &'static str> {
    let string = std::iter::repeat_with(fastrand::alphanumeric).take(10).collect();
    test(string)
}

#[test]
fn my_test() -> impl std::process::Termination {
    fn my_test(random_string: String) -> Result<(), &'static str> {
        assert_eq!(string.len(), 10);
        Ok(())
    }
    my_test_harness(my_test)
}

Async example

You can use this to set up an async runtime and spawn or block on the test.

use test_harness::test;

mod my_mod {
    pub fn set_up<F, Fut>(test: F) -> Result<(), Box<dyn std::error::Error>>
    where
        F: FnOnce(&'static str) -> Fut,
        Fut: std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send + 'static,
    {
        futures_lite::future::block_on(test("hello"))
    }
}

#[test(harness = my_mod::set_up)]
async fn my_test(s: &'static str) -> Result<(), Box<dyn std::error::Error>> {
    assert_eq!(s, "hello");
    Ok(())
}

Eliding harness name

If you name your harness harness, you can elide the harness name, like so:

use test_harness::test;

pub fn harness<F, Fut, Out>(test: F) -> Out
where
    F: FnOnce(&'static str) -> Fut,
    Fut: std::future::Future<Output = Out> + Send + 'static,
    Out: std::process::Termination
{
    futures_lite::future::block_on(test("hello"))
}


#[test(harness)]
async fn test_one(s: &'static str) -> Result<(), Box<dyn std::error::Error>> {
    assert_eq!(s, "hello");
    Ok(())
}

#[test(harness)]
async fn test_two(s: &'static str) {
    assert_eq!(s, "hello");
}

Drop down to standard #[test]

If this macro is used without any additional arguments, it works identically to the built-in #[test] macro.

use test_harness::test;
#[test]
fn normal_test() {
    assert!(true);
}

Dependencies

~220–660KB
~16K SLoC

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy