gpt4 book ai didi

testing - 如何递归测试目录下的所有 crate ?

转载 作者:行者123 更新时间:2023-11-29 07:42:25 24 4
gpt4 key购买 nike

有些项目包含多个 crate,这使得在每个 crate 中手动运行所有测试变得很麻烦。

有没有方便的方法递归运行 cargo test

最佳答案

更新:自添加此答案 1.15 发布以来,添加 cargo test --all,会将其与自定义脚本进行比较。


此 shell 脚本在 git 存储库上对包含 Cargo.toml 文件的所有目录递归运行测试(对于其他 VCS 来说足够容易编辑)。

  • 出现第一个错误时退出。
  • 使用 nocapture 以显示标准输出
    (取决于个人喜好,易于调整)
  • 使用 RUST_BACKTRACE 集运行测试,以获得更有用的输出。
  • 在两个独立的步骤中构建和运行
    (1.14 稳定版中 this bug 的解决方法)。
  • 可选的 CARGO_BIN 环境变量来覆盖 cargo 命令
    (如果您想使用诸如 cargo-out-of-source builder 的 cargo-wrapper 则很方便)。<

脚本:

#!/bin/bash

# exit on first error, see: http://stackoverflow.com/a/185900/432509
error() {
local parent_lineno="$1"
local message="$2"
local code="${3:-1}"
if [[ -n "$message" ]] ; then
echo "Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}"
else
echo "Error on or near line ${parent_lineno}; exiting with status ${code}"
fi
exit "${code}"
}
trap 'error ${LINENO}' ERR
# done with trap

# support cargo command override
if [[ -z $CARGO_BIN ]]; then
CARGO_BIN=cargo
fi

# toplevel git repo
ROOT=$(git rev-parse --show-toplevel)

for cargo_dir in $(find "$ROOT" -name Cargo.toml -printf '%h\n'); do
echo "Running tests in: $cargo_dir"
pushd "$cargo_dir"
RUST_BACKTRACE=0 $CARGO_BIN test --no-run
RUST_BACKTRACE=1 $CARGO_BIN test -- --nocapture
popd
done

感谢@набиячлэвэли 的回答,这是一个扩展版本。

关于testing - 如何递归测试目录下的所有 crate ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com