Skip to content

Commit

Permalink
add unit tests (#60)
Browse files Browse the repository at this point in the history
also, flatten the workspace into a single crate. I forget what the original reason for the workspace
was -- I think it was added because `cargo t -p testsuite` is nicer than running
`cargo t --test $filename` N times. However, a single crate removes the need to repeat the list of
dependencies in `testsuite/Cargo.toml`

also, document the two sets of tests included with the template and how to run them in the README
file
  • Loading branch information
japaric authored Jan 3, 2022
1 parent d82fad8 commit 47277bb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 55 deletions.
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ name = "{{project-name}}"
edition = "2018"
version = "0.1.0"

[workspace]
members = ["testsuite"]
[lib]
harness = false

# needed for each integration test
[[test]]
name = "integration"
harness = false

[dependencies]
cortex-m = "0.7.3"
Expand All @@ -17,6 +22,9 @@ panic-probe = { version = "0.3.0", features = ["print-defmt"] }
# TODO(4) enter your HAL here
# some-hal = "1.2.3"

[dev-dependencies]
defmt-test = "0.3.0"

# cargo build/run
[profile.dev]
codegen-units = 1
Expand Down
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ $ echo $?
```

If you're running out of memory (`flip-link` bails with an overflow error), you can decrease the size of the device memory buffer by setting the `DEFMT_RTT_BUFFER_SIZE` environment variable. The default value is 1024 bytes, and powers of two should be used for optimal performance:

``` console
$ DEFMT_RTT_BUFFER_SIZE=64 cargo rb hello
```
Expand All @@ -154,12 +154,41 @@ If you are using [rust-analyzer] with VS Code for IDE-like features you can add
"Cargo.toml",
"firmware/Cargo.toml",
]
}
}
```

[RA docs]: https://rust-analyzer.github.io/manual.html#configuration
[rust-analyzer]: https://rust-analyzer.github.io/

## Running tests

The template comes configured for running unit tests and integration tests on the target.

Unit tests reside in the library crate and can test private API; the initial set of unit tests are in `src/lib.rs`.
`cargo test --lib` will run those unit tests.

``` console
$ cargo test --lib
(1/1) running `it_works`...
└─ app::unit_tests::__defmt_test_entry @ src/lib.rs:33
all tests passed!
└─ app::unit_tests::__defmt_test_entry @ src/lib.rs:28
```

Integration tests reside in the `tests` directory; the initial set of integration tests are in `tests/integration.rs`.
`cargo test --test integration` will run those integration tests.
Note that the argument of the `--test` flag must match the name of the test file in the `tests` directory.

``` console
$ cargo test --test integration
(1/1) running `it_works`...
└─ integration::tests::__defmt_test_entry @ tests/integration.rs:13
all tests passed!
└─ integration::tests::__defmt_test_entry @ tests/integration.rs:8
```

Note that to add a new test file to the `tests` directory you also need to add a new `[[test]]` section to `Cargo.toml`.

## Trying out the git version of defmt

This template is configured to use the latest crates.io release (the "stable" release) of the `defmt` framework.
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![no_main]
#![no_std]

use defmt_rtt as _; // global logger
Expand All @@ -20,3 +21,17 @@ pub fn exit() -> ! {
cortex_m::asm::bkpt();
}
}

// defmt-test 0.3.0 has the limitation that this `#[tests]` attribute can only be used
// once within a crate. the module can be in any file but there can only be at most
// one `#[tests]` module in this library crate
#[cfg(test)]
#[defmt_test::tests]
mod unit_tests {
use defmt::assert;

#[test]
fn it_works() {
assert!(true)
}
}
16 changes: 16 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![no_std]
#![no_main]

use {{crate_name}} as _; // memory layout + panic handler

// See https://crates.io/crates/defmt-test/0.3.0 for more documentation (e.g. about the 'state'
// feature)
#[defmt_test::tests]
mod tests {
use defmt::assert;

#[test]
fn it_works() {
assert!(true)
}
}
23 changes: 0 additions & 23 deletions testsuite/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions testsuite/src/lib.rs

This file was deleted.

21 changes: 0 additions & 21 deletions testsuite/tests/test.rs

This file was deleted.

0 comments on commit 47277bb

Please sign in to comment.