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

Get test case name #177

Open
adriangb opened this issue Jan 31, 2023 · 6 comments
Open

Get test case name #177

adriangb opened this issue Jan 31, 2023 · 6 comments

Comments

@adriangb
Copy link

When using case::some_description would it be possible to also get that value as a parameter to the test function? My use case is writing a file / snapshot test with my data so I'd like to re-use that identifier for the filename.

@la10736
Copy link
Owner

la10736 commented Jan 31, 2023

You can use the current thread name instead.... see https://github.com/la10736/rstest/blob/master/rstest_test/src/utils.rs#L349
I've planned to write a fixture for it but I never did it 😢

Anyway you can write a fixture

#[fixture]
pub fn testname() -> String {
    thread::current().name().unwrap().to_string()
}

and use it in your own test:

#[rstest]
#[case("my_test")]
fn my_test(testname: String, v: &str) {
    assert!(testname.contains(v);
}

I didn't try it but should work

@adriangb
Copy link
Author

adriangb commented Feb 1, 2023

Hmm not sure I’m understanding. What I meant is something like:

#[rstest]
#[case::case1("case1")]
#[case::case2("case2")]
fn my_test(case_name: &str, v: &str) {
    assert_eq!(case_name, v)
}

@la10736
Copy link
Owner

la10736 commented Feb 1, 2023

I mean that if you run cargo test -- --nocapture on the follow code

use rstest::*;
use std::thread;

#[fixture]
pub fn testname() -> String {
    thread::current().name().unwrap().to_string()
}

#[rstest]
#[case("my_test")]
#[should_panic]
#[case("something_else")]
fn my_test(testname: String, #[case] v: &str) {
    assert!(dbg!(testname).contains(v));
}

You'll see the follow output

mdamico@miklap:~/dev_random/t_117$ cargo test -- --nocapture
   Compiling t_117 v0.1.0 (/home/mdamico/dev_random/t_117)
    Finished test [unoptimized + debuginfo] target(s) in 0.19s
     Running unittests src/lib.rs (target/debug/deps/t_117-755bac183b006419)

running 2 tests
[src/lib.rs:14] testname = "my_test::case_1"
[src/lib.rs:14] testname = "my_test::case_2"
thread 'my_test::case_2' panicked at 'assertion failed: dbg!(testname).contains(v)', src/lib.rs:14:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test my_test::case_1 ... ok
test my_test::case_2 - should panic ... ok

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

   Doc-tests t_117

running 0 tests

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

As you can see testname fixture is exactly the name of the test.

@la10736
Copy link
Owner

la10736 commented Feb 2, 2023

I forgot to mention that is not so hard to get description from the test name.
I understand that you would like to have the description available in the test. It could be a possibility to introduce something like

use rstest::{rstest, Ctx};

#[rstest]
#[case::case1("case1")]
#[case::case2("case2")]
fn my_test(#[context] ctx: Ctx, v: &str) {
    assert_eq!(ctx.description().unwrap(), v)
}

It could be useful and clean.

I hope to have some time to implement it.... but is quite hard 😢

@adriangb
Copy link
Author

adriangb commented Feb 4, 2023

Here’s what I ended up with: https://github.com/adriangb/pgpq/blob/b0b0f8c77c862c0483d81571e76f3a2b746136fc/pgpq/src/lib.rs#L649-L669

thanks for the help and crate!

@adriangb adriangb closed this as completed Feb 4, 2023
@la10736
Copy link
Owner

la10736 commented Feb 4, 2023

I don't want to lose the context idea.

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