0

I am trying to expand on an existing crate. When I run the standard crate tests, some of them are failing. The crate creator has used macros and iterators extensively to create the tests. I'm unable to decode what the actual test being run are, so that I can trace down what is going on to debug the test failure. The output from cargo test does not refer to any existing functions within the crate.

Is there a way to create a dry run log of what calls are expected when running cargo test? I would like to be able to see all the test calls that will be performed without actually running the tests.

I've tried tracing through the code manually to recreate the test. Also, using RUST_BACKTRACE=1 doesn't get me any closer either. I'm not able to discern how the test was generated to back create it from the output.

  • If the output of `cargo test` doesn't help, what are you expecting to see from a "dry run log"? Are you hoping to see what happens within a `#[test]` without running it? That isn't going to happen. – kmdreko May 29 '23 at 15:30
  • The output should tell you the module path and file path of the failing tests, which should tell you where they are. Then you can expand the macros with rust-analyzer or cargo-expand. Other than that, you'd probably need to ask the author of the macros or the crate. Not sure what you mean by "what calls are expected"; it already shows all the tests that are being called. – drewtato May 29 '23 at 15:32
  • If certain code is confusing you, you'll have to actually show it for us to help you. There's no general answer to "what do macros do", only answers for specific macros. – drewtato May 29 '23 at 15:35

1 Answers1

1

If you pass the --list option to the test harness — that is, run

cargo test -- --list

then you will see all test names, which are all the paths to the test functions. That is, if foo::bar::baz is in the list, then there must be (after macro expansion) a function

#[test]
fn baz() {}

that can be found in the foo::bar module. However, this does not tell you what the code in that function is, only that it exists, just as running the tests would — that's all the information the test harness has, so that's the best dry-run it can give you.

In order to find out what code the macro-generated test runs, you will have to either understand the macros, or expand them. rust-analyzer provides an “Expand macro recursively” command which can help with that.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108