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

Omit case_n for named cases of parameterized tests #167

Open
DanielSWolf opened this issue Nov 12, 2022 · 6 comments
Open

Omit case_n for named cases of parameterized tests #167

DanielSWolf opened this issue Nov 12, 2022 · 6 comments

Comments

@DanielSWolf
Copy link

Consider the following parameterized test for a trivial function square:

fn square(i: i32) -> i32 { i * i }

#[rstest]
#[case::negative_value(-5, 25)]
#[case::zero(0, 0)]
#[case::positive_value(3, 9)]
#[case::large_value(10000, 100000000)]
fn squares_the_value(#[case] value: i32, #[case] expected: i32) {
    assert_eq!(square(value), expected);
}

I've given each test case an expressive name to make the generated test names more helpful. The output is as follows:

test repro::squares_the_value::case_1_negative_value ... ok
test repro::squares_the_value::case_2_zero ... ok
test repro::squares_the_value::case_3_positive_value ... ok
test repro::squares_the_value::case_4_large_value ... ok

The generated test name contains both a numbered case_n and the manual case names. To me, the case_n part doesn't add any benefit. For more complex tests with longer names, I find that it makes the test names harder to read.

Would it make sense to omit case_n from the generated names of parameterized tests if the corresponding case has an explicit name?

@la10736 la10736 mentioned this issue Nov 13, 2022
@la10736
Copy link
Owner

la10736 commented Nov 13, 2022

It was a design choose: I don't want take care of duplication and I would give the possibility to have same description for more than one tests. There are cases where you would provide some cases for the same semantic cases exactly like when you don't provide a description at all.

Of course I can identify clashes and add a postfix only in these cases. I don't reject it because I would consider better this possibility.

@DanielSWolf
Copy link
Author

That sounds good. Let me recap to make sure we're on the same page:

  • If a case is unnamed, it gets named case_n, where n is the 1-based index of the case within the list of cases.
  • If a case is named but not unique, its name gets suffixed with _m, where m is the 1-based index within the clashing group.

To illustrate:

Test:

#[rstest]
#[case::zero(0, 0)]
#[case::negative_value(-100, 10000)]
#[case::negative_value(-5, 25)]
#[case::negative_value(-1, 1)]
#[case(1, 1)]
#[case(3, 9)]
#[case(10000, 100000000)]
fn squares_the_value(#[case] value: i32, #[case] expected: i32) {
    assert_eq!(square(value), expected);
}

Output:

test squares_the_value::zero ... ok
test squares_the_value::negative_value_1 ... ok
test squares_the_value::negative_value_2 ... ok
test squares_the_value::negative_value_3 ... ok
test squares_the_value::case_5 ... ok
test squares_the_value::case_6 ... ok
test squares_the_value::case_7 ... ok

Correct?

@la10736
Copy link
Owner

la10736 commented Nov 14, 2022 via email

@drdavella
Copy link

drdavella commented Nov 16, 2022

Would you be willing to consider deriving names for unnamed cases from the given case parameters? It is currently quite difficult to determine which test failed based on case_n alone. I do not have a problem with the case_n prefix, though.

EDIT: I see there is already an issue for this here #160. However, I still think this would be a very useful feature, although I do see the problem with converting the expressions to strings.

@la10736
Copy link
Owner

la10736 commented Nov 16, 2022 via email

@DanielJoyce
Copy link

DanielJoyce commented Dec 4, 2023

If a case fails, the error message should log the actual content of the failing #[case], or the line number of the #[case] attribute that failed. That would make it easier. First one should be doable. Second one might be harder as the macros in macros probably messes up line numbers from the original source file.

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

4 participants