Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 337
fghj Avatar asked Jul 06 '17 11:07

fghj


People also ask

Does cargo run tests in parallel?

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.

Does cargo build run tests?

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.

How do I run a cargo file?

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 .

Where do I put tests in Rust project?

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) .


2 Answers

There are two kinds of tests supported by cargo test, one is the normal tests (#[test] fns 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).

like image 104
kennytm Avatar answered Sep 28 '22 10:09

kennytm


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.

like image 43
Ted Mielczarek Avatar answered Sep 28 '22 09:09

Ted Mielczarek