Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit c142db6

Browse files
committed
Add example of writing tests that run on host machine
1 parent 207bca3 commit c142db6

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

examples/test_on_host.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Conditionally compiling tests with std and our executable with no_std.
2+
//!
3+
//! Ensure there are no targets specified under `[build]` in `.cargo/config`
4+
//! In order to make this work, we lose the convenience of having a default target that isn't the
5+
//! host.
6+
//!
7+
//! cargo build --example test_on_host --target thumbv7m-none-eabi
8+
//! cargo test --example test_on_host
9+
10+
#![cfg_attr(test, allow(unused_imports))]
11+
12+
#![cfg_attr(not(test), no_std)]
13+
#![cfg_attr(not(test), no_main)]
14+
15+
// pick a panicking behavior
16+
#[cfg(not(test))]
17+
extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics
18+
// extern crate panic_abort; // requires nightly
19+
// extern crate panic_itm; // logs messages over ITM; requires ITM support
20+
// extern crate panic_semihosting; // logs messages to the host stderr; requires a debugger
21+
22+
use cortex_m::asm;
23+
use cortex_m_rt::entry;
24+
25+
#[cfg(not(test))]
26+
#[entry]
27+
fn main() -> ! {
28+
asm::nop(); // To not have main optimize to abort in release mode, remove when you add code
29+
30+
loop {
31+
// your code goes here
32+
}
33+
}
34+
35+
#[cfg(test)]
36+
mod test {
37+
use super::*;
38+
39+
#[test]
40+
fn foo() {
41+
println!("tests work!");
42+
assert!(1 == 1);
43+
}
44+
}

0 commit comments

Comments
 (0)