How to implement unit tests for a project with embedded rust?


I am looking for an example project (on GitHub) or tutorial: How to write unit tests for STM32.
Can you recommend something?
How do I organize my project to build for thumbv7m-none-eabi and test on x86_64-unknown-linux-gnu?
I am going to use embedded-hal-mock.

My example project: GitHub - hanusek/embedded-unit-tests

1 Like

As long as the units under test do not depend directly on types that cannot be used on the host, you can put them into a platform-agnostic crate and run your tests directly on the host system (macOS, Linux, Windows, whatever) with cargo test --package crate-name-here. The crate needs to be #[no_std], but the test profile will pull in std anyway. Put both crates into a Cargo workspace.

If you need to run tests on the embedded device itself for some reason, you might be able to use something like defmt-test. This is really a "last resort" option, because setup is a real pain if you aren't already using defmt. And it's a really weird way of doing something as pervasive as printing text.

Huh, well, ok. I don't know if you really need that kind of integration with unit tests. (That's integration testing, a different discipline.) Unit tests generally are for testing business logic. If your business logic is intermixed with platform integration, then your architecture is violating the principle of separation of concerns.

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.