Search code examples
unit-testingrustrust-cargo

How to generate a dry run log of crate test calls in Rust Cargo?


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.


Solution

  • 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.