如何在Rust Cargo中生成crate测试调用的dry run日志?

huangapple go评论67阅读模式
英文:

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

问题

我正在尝试扩展现有的 crate。当我运行标准 crate 测试时,有些测试失败了。创建 crate 的人广泛使用了宏和迭代器来创建这些测试。我无法解码实际运行的测试是什么,以便我可以追踪并调试测试失败的原因。cargo test 的输出没有涉及 crate 内的任何现有函数。

是否有一种方法可以创建运行 cargo test 时预期的调用的详细日志?我想要能够查看将执行的所有测试调用,而不必实际运行测试。

我已经尝试手动浏览代码以重新创建测试。此外,使用 RUST_BACKTRACE=1 也没有使我更接近目标。我无法确定测试是如何生成的,以便从输出中重新创建它。

英文:

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.

答案1

得分: 1

如果你在测试运行时传递了 --list 选项 给测试工具,也就是说执行

cargo test -- --list

那么你将会看到所有测试名称,它们实际上是测试函数的路径。也就是说,如果列表中包含了 foo::bar::baz,那么在宏展开之后,必须存在一个函数

#[test]
fn baz() {}

可以在 foo::bar 模块中找到。然而,这并不告诉你这个函数中的代码是什么,只是它存在而已,就像运行测试一样 — 这是测试工具所知道的所有信息,因此这是它能提供给你的最好的试运行。

要想找出宏生成的测试运行了什么代码,你要么需要理解这些宏,要么展开它们。 rust-analyzer 提供了一个“递归展开宏”命令,可以帮助你实现这一点。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月29日 22:51:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定