[英]How to get a list of all cargo tests?
我正在嘗試創建一個需要我進行代碼覆蓋和普通測試的 CI 工作流程。 我們此時同時運行兩者,因為為grcov
檢測的覆蓋率測試可能會失敗,但正常測試可能會通過。 我想自動將覆蓋檢測失敗的測試與沒有它的測試進行比較。 因此,對於在grcov
儀器中失敗的每個測試,我想在沒有儀器的情況下運行類似的測試。
目前,這樣做的方法是對測試路徑進行硬編碼,和/或使用不可靠的基於文本的測試檢測。 一旦人們開始添加更多測試(他們會這樣做),這既是勞動密集型的,也不是很可靠。
所以我的問題是
cargo -test
失敗時將操作附加到它,而不停止貨物測試。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.