Proper way to use helper functions to test stuff

Sometimes, test are not as simple as calling assert_eq. Sometimes you want to do lots of assertions inside the test function:

#[test]
    pub fn product_test() {
        fn test(
            operands: &[u64],
            expected_result: &[u64],
        ) {
           //assert lots of stuff here
        }
        test(
            &[0],
            &[0],
        );
        test(
            &[1,4],
            &[7,8,9],
        );
        test(
            &[1,2,3],
            &[6,7],
        );

however, when it fails, I don't know which of the 3 tests above failed. It's hard to look at the backtrace. On a simple assert_eq!, I'd see which line.

Is there a way for knowing which of these 3 tests failed without looking at the backtrace?

Try adding #[track_caller] onto the helper function definition:

+ #[track_caller]
  fn helper (
      operands: &[u64],
  …
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.