add unit tests (#60)

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
This commit is contained in:
Jorge Aparicio
2022-01-03 11:29:08 +01:00
committed by GitHub
parent d82fad8c96
commit 47277bbf9d
7 changed files with 72 additions and 55 deletions
+10 -2
View File
@@ -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"
@@ -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
+29
View File
@@ -160,6 +160,35 @@ If you are using [rust-analyzer] with VS Code for IDE-like features you can add
[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.
+15
View File
@@ -1,3 +1,4 @@
#![no_main]
#![no_std]
use defmt_rtt as _; // global logger
@@ -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
View File
@@ -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
View File
@@ -1,23 +0,0 @@
[package]
# TODO(1) fix `authors` if you didn't use `cargo-generate`
authors = ["{{authors}}"]
name = "testsuite"
publish = false
edition = "2018"
version = "0.1.0"
[lib]
harness = false
[[test]]
name = "test"
harness = false
[dependencies]
{{project-name}} = { path = ".." }
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
defmt = "0.3.0"
defmt-rtt = "0.3.0"
defmt-test = "0.3.0"
panic-probe = { version = "0.3.0", features = ["print-defmt"] }
-7
View File
@@ -1,7 +0,0 @@
#![no_std]
#![cfg_attr(test, no_main)]
use {{crate_name}} as _; // memory layout + panic handler
#[defmt_test::tests]
mod tests {}
-21
View File
@@ -1,21 +0,0 @@
#![no_std]
#![no_main]
use {{crate_name}} as _; // memory layout + panic handler
// See https://crates.io/crates/defmt-test/0.1.0 for more documentation (e.g. about the 'state'
// feature)
#[defmt_test::tests]
mod tests {
use defmt::{assert, assert_eq};
#[test]
fn assert_true() {
assert!(true)
}
#[test]
fn assert_eq() {
assert_eq!(24, 42, "TODO: write actual tests")
}
}