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:
+10
-2
@@ -5,8 +5,13 @@ name = "{{project-name}}"
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[workspace]
|
[lib]
|
||||||
members = ["testsuite"]
|
harness = false
|
||||||
|
|
||||||
|
# needed for each integration test
|
||||||
|
[[test]]
|
||||||
|
name = "integration"
|
||||||
|
harness = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cortex-m = "0.7.3"
|
cortex-m = "0.7.3"
|
||||||
@@ -17,6 +22,9 @@ panic-probe = { version = "0.3.0", features = ["print-defmt"] }
|
|||||||
# TODO(4) enter your HAL here
|
# TODO(4) enter your HAL here
|
||||||
# some-hal = "1.2.3"
|
# some-hal = "1.2.3"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
defmt-test = "0.3.0"
|
||||||
|
|
||||||
# cargo build/run
|
# cargo build/run
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|||||||
@@ -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
|
[RA docs]: https://rust-analyzer.github.io/manual.html#configuration
|
||||||
[rust-analyzer]: https://rust-analyzer.github.io/
|
[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
|
## 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.
|
This template is configured to use the latest crates.io release (the "stable" release) of the `defmt` framework.
|
||||||
|
|||||||
+15
@@ -1,3 +1,4 @@
|
|||||||
|
#![no_main]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
use defmt_rtt as _; // global logger
|
use defmt_rtt as _; // global logger
|
||||||
@@ -20,3 +21,17 @@ pub fn exit() -> ! {
|
|||||||
cortex_m::asm::bkpt();
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"] }
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#![no_std]
|
|
||||||
#![cfg_attr(test, no_main)]
|
|
||||||
|
|
||||||
use {{crate_name}} as _; // memory layout + panic handler
|
|
||||||
|
|
||||||
#[defmt_test::tests]
|
|
||||||
mod tests {}
|
|
||||||
@@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user