From 47277bbf9da54049a1cf2e067ea578fcdeb3e63b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 3 Jan 2022 10:29:08 +0000 Subject: [PATCH] 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 --- Cargo.toml | 12 ++++++++++-- README.md | 33 +++++++++++++++++++++++++++++++-- src/lib.rs | 15 +++++++++++++++ tests/integration.rs | 16 ++++++++++++++++ testsuite/Cargo.toml | 23 ----------------------- testsuite/src/lib.rs | 7 ------- testsuite/tests/test.rs | 21 --------------------- 7 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 tests/integration.rs delete mode 100644 testsuite/Cargo.toml delete mode 100644 testsuite/src/lib.rs delete mode 100644 testsuite/tests/test.rs diff --git a/Cargo.toml b/Cargo.toml index cecfd70..5fcd52e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/README.md b/README.md index 1f92047..fad2063 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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. diff --git a/src/lib.rs b/src/lib.rs index 506b5fd..2aef086 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) + } +} diff --git a/tests/integration.rs b/tests/integration.rs new file mode 100644 index 0000000..149ab9c --- /dev/null +++ b/tests/integration.rs @@ -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) + } +} diff --git a/testsuite/Cargo.toml b/testsuite/Cargo.toml deleted file mode 100644 index c81ac48..0000000 --- a/testsuite/Cargo.toml +++ /dev/null @@ -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"] } diff --git a/testsuite/src/lib.rs b/testsuite/src/lib.rs deleted file mode 100644 index d4dd9ce..0000000 --- a/testsuite/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -#![no_std] -#![cfg_attr(test, no_main)] - -use {{crate_name}} as _; // memory layout + panic handler - -#[defmt_test::tests] -mod tests {} diff --git a/testsuite/tests/test.rs b/testsuite/tests/test.rs deleted file mode 100644 index b26f3bf..0000000 --- a/testsuite/tests/test.rs +++ /dev/null @@ -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") - } -}