I know that the compiler can run directly on arm-linux-androideabi
, but the Android emulator (I mean emulation of ARM on x86/amd64) is slow,
so I don't want to use cargo
and rustc
on the emulator, I only want to run tests on it.
I want to cross-compile tests on my PC (cargo test --target=arm-linux-androideabi --no-run
?), and then upload and run them on emulator,
hoping to catch bugs like this.
How can I run cargo test
without running cargo test
? Is it as simple as running all binaries that were built with cargo test --no-run
?
The default behavior of the binary produced by cargo test is to run all the tests in parallel and capture output generated during test runs, preventing the output from being displayed and making it easier to read the output related to the test results.
cargo test runs additional checks as well. It will compile any examples you've included to ensure they are still compiles. It also run documentation tests to ensure your code samples from documentation comments compiles.
Because the default build is a debug build, Cargo puts the binary in a directory named debug. You can run the executable with this command: $ ./target/debug/hello_cargo # or .
You'll put unit tests in the src directory in each file with the code that they're testing. The convention is to create a module named tests in each file to contain the test functions and to annotate the module with cfg(test) .
There are two kinds of tests supported by cargo test
, one is the normal tests (#[test] fn
s and files inside tests/
), the other is the doc tests.
The normal tests are as simple as running all binaries. The test is considered successful if it exits with error code 0.
Doc tests cannot be cross-tested. Doc tests are compiled and executed directly by rustdoc
using the compiler libraries, so the compiler must be installed on the ARM machine to run the doc tests. In fact, running cargo test --doc
when HOST ≠ TARGET will do nothing.
So, the answer to your last question is yes as long as you don't rely on doc-tests for coverage.
Starting from Rust 1.19, cargo
supports target specific runners, which allows you to specify a script to upload and execute the test program on the ARM machine.
#!/bin/sh
set -e
adb push "$1" "/sdcard/somewhere/$1"
adb shell "chmod 755 /sdcard/somewhere/$1 && /sdcard/somewhere/$1"
# ^ note: may need to change this line, see https://stackoverflow.com/q/9379400
Put this to your .cargo/config
:
[target.arm-linux-androideabi]
runner = ["/path/to/your/run/script.sh"]
then cargo test --target=arm-linux-androideabi
should Just Work™.
If your project is hosted on GitHub and uses Travis CI, you may also want to check out trust
. It provides a pre-packaged solution for testing on many architectures including ARMv7 Linux on the CI (no Android unfortunately).
My recommendation for testing on Android would be to use dinghy
which provides nice wrapper commands for building and testing on Android/iOS devices/emulator/simulators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With