我如何在没有 Rust 编译器的情况下 运行 在另一台机器上进行 cargo 测试?

How can I run cargo tests on another machine without the Rust compiler?

我知道编译器可以直接在 arm-linux-androideabi 上 运行,但是 Android 模拟器(我的意思是在 x86/amd64 上模拟 ARM)很慢, 所以我不想在模拟器上使用 cargorustc,我只想 运行 测试它。

我想在我的 PC 上交叉编译测试 (cargo test --target=arm-linux-androideabi --no-run?),然后上传并 运行 在模拟器上, 希望能捕获像 this.

这样的错误

没有运行宁cargo test我怎么能运行cargo test呢?它是否像 运行 宁所有用 cargo test --no-run 构建的二进制文件一样简单?

cargo test支持两种测试,一种是普通测试(#[test] fns和tests/里面的文件),另一种是doc测试。

正常测试就像运行所有二进制文件一样简单。如果测试以错误代码 0 退出,则认为测试成功。

文档测试不能进行交叉测试。文档测试由 rustdoc 使用编译器库直接编译和执行,因此必须在 ARM 机器上安装编译器才能 运行 文档测试。事实上,运行宁cargo test --doc当HOST≠TARGET时什么都不做。

因此,只要您不依赖文档测试进行覆盖,您最后一个问题的答案是


从Rust 1.19开始,cargo支持target specific runners,可以指定一个脚本上传到ARM机器上执行测试程序

#!/bin/sh
set -e
adb push "" "/sdcard/somewhere/"
adb shell "chmod 755 /sdcard/somewhere/ && /sdcard/somewhere/" 
# ^ note: may need to change this line, see 

把这个放到你的 .cargo/config:

[target.arm-linux-androideabi]
runner = ["/path/to/your/run/script.sh"]

那么 cargo test --target=arm-linux-androideabi 应该 Just Work™。


如果您的项目托管在 GitHub 上并使用 Travis CI,您可能还想查看 trust。它提供了一个预打包的解决方案,用于在许多架构上进行测试,包括 CI 上的 ARMv7 Linux(不幸的是没有 Android)。

我在 Android 上的测试建议是使用 dinghy,它提供了很好的包装器命令,用于在 Android/iOS devices/emulator/simulators.

上构建和测试

对于可能仍然对此感兴趣的人: 运行 cargo -v test-v

然后寻找这个输出

 Finished release [optimized] target(s) in 21.31s
     Running `/my-dir/target/release/deps/my-binary-29b03924d05690f1`  

然后只需将测试二进制文件/my-dir/target/release/deps/my-binary-29b03924d05690f1复制到没有rustc的机器上