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

Allow to customize test function name #5

Open
Mingun opened this issue Jul 13, 2020 · 2 comments
Open

Allow to customize test function name #5

Mingun opened this issue Jul 13, 2020 · 2 comments

Comments

@Mingun
Copy link

Mingun commented Jul 13, 2020

For example, I want to test all formats from kaitai-struct gallery. I use following code (folder formats is git checkout of https://github.com/kaitai-io/kaitai_struct_formats repository):

#[cfg(test)]
mod formats {
  use std::fs::File;
  use test_generator::test_resources;
  use super::Ksy;

  #[test_resources("formats/**/*.ksy")]
  fn parse(resource: &str) {
    let file = File::open(resource).expect(&format!("can't read file {}", resource));
    let _: Ksy = serde_yaml::from_reader(file).expect(&format!("invalid file {}", resource));
  }
}

and get following output:

test parser::formats::parse_formats_archive_cpio_old_le_ksy ... ok
...

Generated function name seems repeat module name. I would be like see something like

test parser::formats::parse_archive_cpio_old_le_ksy ... ok
...

That can be implemented, for example, by introducing capture group in glob pattern, something like #[test_resources("formats/(**/*).ksy")]

@frehberg
Copy link
Owner

Would be a grood improvement, I like this idea :)

@ian-h-chamberlain
Copy link

+1 to this idea, for an example of a similar crate that allows customization like this you could look at test_case

Their generate tests are actually #[cfg(test)] modules, e.g. this code

#[test_case(1; "my test 1")]
#[test_case(2; "my test 2")]
fn do_test(x: usize) {}

would generate something like

#[cfg(test)]
mod do_test {
   fn do_test(x: usize) {}

    #[test]
    fn my_test_1() {do_test(1);}

    #[test]
    fn my_test_2() {do_test(2);}
}

Personally I'm a fan of this model as it logically groups related tests into a proper module structure.

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

3 participants