How to run #[test] in embedded projects in QEMU?

Any experience how to make #[test] work in embedded project testing?

For example, we are working on a project running inside FreeRTOS,
without real board, but currently can be run inside QEMU

The target is riscv64gc-unknown-none-elf

1 Like

I haven't done testing with it myself, but cargo cross may help you configure the correct environment to test with qemu.

In .cargo/config it is possible to configure a 'runner' for binaries. Reference documentation here. This runner can be any executable, which is then passed the actual target and arguments.

For instance, here's a runner for an embedded project in my home network. It is actually doing hardware-in-the-loop and your case should be far simpler. This runner will execute the binary by first transmitting it to the device via ssh/rsync before running the binary itself.

#!/bin/bash
# (filename: send_and_run.sh)
rsync -e "ssh -i ./contrib/id" "$1" anon@anon: \
  && ssh -tti ./contrib/id "anon@anon" sudo "./$(basename $1)" $2
# In .cargo/config
[target.armv7-unknown-linux-musleabihf]
linker = "arm-linux-musleabihf-gcc"
runner = "./send_and_run.sh"

You can follow the guide of QEMU – Embedded Book and supplement the runner configuration above appropriately to run the binary under a configured qemu instead of real hardware. There might be a preconfigured one for your specific target architecture.

Not sure if it is supported by cross, as I am working on a bare metal environment without Linux OS,
and I do not see riscv64gc-unknown-none-elf in its supported target matrix

I will follow this to run tests inside QEMU.
Another problem is that #[test] does not work.

I think I should not use #[test],
but use

[[test]]
harness = false

to avoid auto linking libtest provided by the Rust compiler,
and to build a custom ELF executable
https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-harness-field

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.