# Adding host unit tests to an embedded project

**URL:** <https://users.rust-lang.org/t/adding-host-unit-tests-to-an-embedded-project/93434>\
**Category:** embedded\
**Created:** [May 3, 2023, 3:51am UTC](https://users.rust-lang.org/t/adding-host-unit-tests-to-an-embedded-project/93434 "2023-05-03T03:51:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsettles](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tsettles/32/29328_2.png) [@tsettles](https://users.rust-lang.org/u/tsettles)\
**Post date:** [May 3, 2023, 3:51am UTC](https://users.rust-lang.org/t/adding-host-unit-tests-to-an-embedded-project/93434/1 "2023-05-03T03:51:23Z")

</div>

This is my first project trying to write embedded rust and my first time working on a project larger than one crate. I'm trying to write software for my Arduino in Rust. I've found [Rahix's template](https://github.com/Rahix/avr-hal-template) which runs a simple blink program without any extra setup.

I've found Ferrous System's blog post on [Testing an embedded app](https://ferrous-systems.com/blog/test-embedded-app/) and tried adapting their project structure to mine, but I'm not familiar enough to know what I need to change on my project.

As far as I understand, I need to have a top-level workspace which contains three crates:

1. cross - contains the `main.rs` file that runs on the Arduino. This is a `#![no_std]` crate and imports the logic crate
2. logic - contains all the interesting things that will run on the Arduino and get unit tested. This is a `#![cfg_attr(not(test), no_std)]` crate
3. local - contains all the unit tests for the project. This is a `std` crate. It imports the logic crate, and could, in principle, contain a `main.rs`, but I don't see how that's useful in this setup.

I got as far as putting Rahix's template as the cross crate, but then I get errors like:

```rust
LLVM ERROR: Expected a constant shift amount!
error: could not compile `core` (lib)

```

if I compile in release mode, and

```rust
error: ran out of registers during register allocation

```

if I just do a normal `cargo run`

I've tried looking up these errors, but I'm not sure if I'm even going down the right path. Does someone have guidance on how to adapt my project, a different technique, or other resources I can look at?

---

<div class="post-metadata">

**Author:** ![hax10](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/hax10/32/33598_2.png) [@hax10](https://users.rust-lang.org/u/hax10)\
**Post date:** [May 3, 2023, 6:07am UTC](https://users.rust-lang.org/t/adding-host-unit-tests-to-an-embedded-project/93434/2 "2023-05-03T06:07:50Z")

</div>

What Arduino board are you using? This code sample should be compatible with the Arduino Nano 3.x.

> **[GitHub - avr-rust/blink: A blinking LED program written in Rust for the AVR](https://github.com/avr-rust/blink)**
>
> A blinking LED program written in Rust for the AVR - GitHub - avr-rust/blink: A blinking LED program written in Rust for the AVR

---

<div class="post-metadata">

**Author:** ![tsettles](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tsettles/32/29328_2.png) [@tsettles](https://users.rust-lang.org/u/tsettles)\
**Post date:** [May 5, 2023, 5:31pm UTC](https://users.rust-lang.org/t/adding-host-unit-tests-to-an-embedded-project/93434/3 "2023-05-05T17:31:01Z")

</div>

Thanks for the link to that repo. That seems useful in some situations, but the Rahix template is solving that same problem for my situation.

I've figured out a solution to my problem! I was able to make a single crate work. I added the `no_std`, `no_main`, and the embedded main function behind the `not(test)` feature with the `cfg!` macro. The part that made it really work is removing the `build.target` and `unstable.build-std` from the `.cargo/config.toml` file, and passing those in during `cargo build` or `cargo run`. Because of that, I could just do `cargo test`.

Hopefully others can find this useful

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 3, 2023, 5:31pm UTC](https://users.rust-lang.org/t/adding-host-unit-tests-to-an-embedded-project/93434/4 "2023-08-03T17:31:41Z")

</div>

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.
