Giter Site home page Giter Site logo

Get test case name about rstest HOT 6 OPEN

adriangb avatar adriangb commented on July 30, 2024
Get test case name

from rstest.

Comments (6)

la10736 avatar la10736 commented on July 30, 2024

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

from rstest.

adriangb avatar adriangb commented on July 30, 2024

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)
}

from rstest.

la10736 avatar la10736 commented on July 30, 2024

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.

from rstest.

la10736 avatar la10736 commented on July 30, 2024

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 😢

from rstest.

adriangb avatar adriangb commented on July 30, 2024

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!

from rstest.

la10736 avatar la10736 commented on July 30, 2024

I don't want to lose the context idea.

from rstest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.