Skip to content

Commit

Permalink
Fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
t-moe committed Dec 12, 2023
1 parent 47d60df commit 3bb6c63
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "embedded-test"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
repository = "https://github.com/probe-rs/embedded-test"
license = "MIT OR Apache-2.0"
Expand Down
123 changes: 96 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ Since the test runner (`probe-rs test`) is libtest compatible (using [libtest-mi


## WARNING
This project is development state. Don't rely on it for anything important yet.
This project is in development state. Don't rely on it for anything important yet.

## Features
* Runs each test case individually, and resets the device between each test case
* Supports an init function which will be called before each test case and can pass state to the test cases
* Supports async test and init functions (needs feature `embassy`)
* Support `#[should_panic]`, `#[ignore]` and `#[timeout(<seconds>)]` attributes for each test case

## Usage

Expand Down Expand Up @@ -59,36 +65,99 @@ Example for `tests/example_test.rs`
#![no_std]
#![no_main]

use esp32c6_hal as _; // exception handler
use panic_probe as _; // semihosting::process::abort on test failure

#[cfg(test)]
#[embedded_test::tests]
mod unit_tests {

#[test]
fn it_works() {
assert!(true)
}

#[test]
#[cfg(abc)]
fn it_works2() {
assert!(false)
}

#[test]
#[ignore]
#[cfg(not(abc))]
fn it_works3() {
assert!(false)
}

#[test]
#[cfg(not(abc))]
fn it_works4() {
assert!(false)
}
// import hal which provides exception handler
use esp32c6_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, IO};

use panic_probe as _; // calls semihosting::process::abort on test failure, printing is done by probe-rs

// Optional: A init function which is called before every test
// asyncness is optional and needs feature embassy
#[init]
async fn init() -> IO {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
ClockControl::boot_defaults(system.clock_control).freeze();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

#[cfg(feature = "log")]
esp_println::logger::init_logger_from_env();

// The init function can return some state, which can be consumed by the testcases
io
}

// A test which takes the state returned by the init function (optional)
// asyncness is optional and needs feature embassy
#[test]
async fn takes_state(_state: IO) {
assert!(true)
}

// Example for a test which is conditionally enabled
#[test]
#[cfg(feature = "log")]
fn log() {
log::info!("Hello, log!"); // Prints via esp-println to rtt
assert!(true)
}

// Another example for a conditionally enabled test
#[test]
#[cfg(feature = "defmt")]
fn defmt() {
use defmt_rtt as _;
defmt::info!("Hello, defmt!"); // Prints via defmt-rtt to rtt
assert!(true)
}

// A test which is cfg'ed out
#[test]
#[cfg(abc)]
fn it_works_disabled() {
assert!(false)
}

// Tests can be ignored with the #[ignore] attribute
#[test]
#[ignore]
fn it_works_ignored() {
assert!(false)
}

// A test that fails with a panic
#[test]
fn it_fails1() {
assert!(false)
}

// A test that fails with a returned Err()
#[test]
fn it_fails2() -> Result<(), ()> {
Err(())
}

// Tests can be annotated with #[should_panic] if they are expected to panic
#[test]
#[should_panic]
fn it_passes() {
assert!(false)
}

// This test should panic, but doesn't => it fails
#[test]
#[should_panic]
fn it_fails3() {}

// Tests can be annotated with #[timeout(<secs>)] to change the default timeout of 60s
#[test]
#[timeout(10)]
fn it_timeouts() {
loop {} // should run into the 60s timeout
}
}
```

0 comments on commit 3bb6c63

Please sign in to comment.