Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated tests when combined with test_log #146

Open
bjackman opened this issue Sep 1, 2024 · 1 comment
Open

Duplicated tests when combined with test_log #146

bjackman opened this issue Sep 1, 2024 · 1 comment

Comments

@bjackman
Copy link

bjackman commented Sep 1, 2024

It's unclear to me if this is really a bug or just the expected outcome of Rust's weird test case registration system.

❯❯ cat Cargo.toml
[package]
name = "t"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
test-case = "3.3.1"
test-log = "0.2.16"

[[bin]]
name = "t"
path = "main.rs"

❯❯ cat main.rs
fn main() {
    println!("Hello, world!");
}

mod tests {
    use test_case::test_case;
    use test_log;
    
    #[test_case(1; "pollo mundo")]
    #[test_log::test]
    fn test_once(_arg: u8) {
        assert!(true);
    }
}

❯❯ cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
     Running unittests main.rs (target/debug/deps/t-fe670cbfe3e221f9)

running 2 tests
test tests::test_once::pollo_mundo ... ok
test tests::test_once::pollo_mundo ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Any idea if there's a workaround for that? In this case it's quite easy to drop test_log but I guess there might be many similar cases where the test wrapper macro is more valuable.

@autarch
Copy link

autarch commented Oct 27, 2024

I think to make this work the test_case macro would have to take an argument specifying the value to replace "test" in #[cfg(test)] in the generated code. In this case it could be set to test_log::test.

kezhuw added a commit to kezhuw/test-case that referenced this issue Nov 19, 2024
This pr proposes a generic mechanism among different test proc macros to
avoid to generate multiple `[::core::prelude::v1::test]` on test method.

`proc_macro_attribute` function is fed with tokens after its attribute
and no tokens before it.

Give the above, this pr proposes test proc macros to append newly
generated macros after existing ones. This way, proc macros processed
later can read all macros including generated and handwritten and make
further decisions. Specifically, proc macros can append
`#[::core::prelude::v1::test]` only if it does not exist.

Macros that transform test method signature can append
`#[::core::prelude::v1::test]` directly without checking its existence
once they generate valid signature for test method.

Closes frondeus#101, frondeus#146.
kezhuw added a commit to kezhuw/test-case that referenced this issue Nov 20, 2024
This pr proposes a generic mechanism among different test proc macros to
avoid to generate multiple `[::core::prelude::v1::test]` on test method.

`proc_macro_attribute` function is fed with tokens after its attribute
and no tokens before it.

Give the above, this pr proposes test proc macros to append newly
generated macros after existing ones. This way, proc macros processed
later can read all macros including generated and handwritten and make
further decisions. Specifically, proc macros can append
`#[::core::prelude::v1::test]` only if it does not exist.

Macros that transform test method signature can append
`#[::core::prelude::v1::test]` directly without checking its existence
once they generate valid signature for test method.

Closes frondeus#101, frondeus#146.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants