繁体   English   中英

如何获得所有货物测试的清单?

[英]How to get a list of all cargo tests?

我正在尝试创建一个需要我进行代码覆盖和普通测试的 CI 工作流程。 我们此时同时运行两者,因为为grcov检测的覆盖率测试可能会失败,但正常测试可能会通过。 我想自动将覆盖检测失败的测试与没有它的测试进行比较。 因此,对于在grcov仪器中失败的每个测试,我想在没有仪器的情况下运行类似的测试。

目前,这样做的方法是对测试路径进行硬编码,和/或使用不可靠的基于文本的测试检测。 一旦人们开始添加更多测试(他们会这样做),这既是劳动密集型的,也不是很可靠。

所以我的问题是

  1. 有没有办法获取要从多箱项目运行的所有测试的列表。
  2. 有没有办法在cargo -test失败时将操作附加到它,而不停止货物测试。
  3. 有没有办法让cargo test只提及失败测试的名称(如果有的话)。

cargo test结果采取行动的最佳选择是使用不稳定的--format json选项。 它将 output 事件采用 JSON 行格式:

cargo test -- -Zunstable-options --format json
{ "type": "suite", "event": "started", "test_count": 2 }
{ "type": "test", "event": "started", "name": "tests::my_other_test" }
{ "type": "test", "event": "started", "name": "tests::my_test" }
{ "type": "test", "name": "tests::my_test", "event": "ok" }
{ "type": "test", "name": "tests::my_other_test", "event": "failed", "stdout": "thread 'tests::my_other_test' panicked at 'assertion failed: false', src/lib.rs:20:9\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n" }
{ "type": "suite", "event": "failed", "passed": 1, "failed": 1, "ignored": 0, "measured": 0, "filtered_out": 0, "exec_time": 0.003150667 }

您可以将其保存到文件中,然后使用jq等其他实用程序来解析相关数据。 以下是一些可能对您有帮助的示例。

获取所有已运行的测试:

jq -c 'select(.type=="test" and .event=="started") | .name' <test-output.json
"tests::my_other_test"
"tests::my_test"

获取所有失败的测试:

jq -c 'select(.type=="test" and .event=="failed") | .name' <test-output.json
"tests::my_other_test"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM