@@ -17,7 +17,13 @@ Since the test runner (`probe-rs test`) is libtest compatible (using [libtest-mi
17
17
18
18
19
19
## WARNING
20
- This project is development state. Don't rely on it for anything important yet.
20
+ This project is in development state. Don't rely on it for anything important yet.
21
+
22
+ ## Features
23
+ * Runs each test case individually, and resets the device between each test case
24
+ * Supports an init function which will be called before each test case and can pass state to the test cases
25
+ * Supports async test and init functions (needs feature ` embassy ` )
26
+ * Support ` #[should_panic] ` , ` #[ignore] ` and ` #[timeout(<seconds>)] ` attributes for each test case
21
27
22
28
## Usage
23
29
@@ -59,36 +65,99 @@ Example for `tests/example_test.rs`
59
65
#![no_std]
60
66
#![no_main]
61
67
62
- use esp32c6_hal as _; // exception handler
63
- use panic_probe as _; // semihosting::process::abort on test failure
64
-
65
68
#[cfg(test)]
66
69
#[embedded_test:: tests]
67
70
mod unit_tests {
68
71
69
- #[test]
70
- fn it_works () {
71
- assert! (true )
72
- }
73
-
74
- #[test]
75
- #[cfg(abc)]
76
- fn it_works2 () {
77
- assert! (false )
78
- }
79
-
80
- #[test]
81
- #[ignore]
82
- #[cfg(not(abc))]
83
- fn it_works3 () {
84
- assert! (false )
85
- }
86
-
87
- #[test]
88
- #[cfg(not(abc))]
89
- fn it_works4 () {
90
- assert! (false )
91
- }
72
+ // import hal which provides exception handler
73
+ use esp32c6_hal :: {clock :: ClockControl , peripherals :: Peripherals , prelude :: * , IO };
74
+
75
+ use panic_probe as _; // calls semihosting::process::abort on test failure, printing is done by probe-rs
76
+
77
+ // Optional: A init function which is called before every test
78
+ // asyncness is optional and needs feature embassy
79
+ #[init]
80
+ async fn init () -> IO {
81
+ let peripherals = Peripherals :: take ();
82
+ let system = peripherals . SYSTEM . split ();
83
+ ClockControl :: boot_defaults (system . clock_control). freeze ();
84
+ let io = IO :: new (peripherals . GPIO , peripherals . IO_MUX );
85
+
86
+ #[cfg(feature = " log" )]
87
+ esp_println :: logger :: init_logger_from_env ();
88
+
89
+ // The init function can return some state, which can be consumed by the testcases
90
+ io
91
+ }
92
+
93
+ // A test which takes the state returned by the init function (optional)
94
+ // asyncness is optional and needs feature embassy
95
+ #[test]
96
+ async fn takes_state (_state : IO ) {
97
+ assert! (true )
98
+ }
99
+
100
+ // Example for a test which is conditionally enabled
101
+ #[test]
102
+ #[cfg(feature = " log" )]
103
+ fn log () {
104
+ log :: info! (" Hello, log!" ); // Prints via esp-println to rtt
105
+ assert! (true )
106
+ }
107
+
108
+ // Another example for a conditionally enabled test
109
+ #[test]
110
+ #[cfg(feature = " defmt" )]
111
+ fn defmt () {
112
+ use defmt_rtt as _;
113
+ defmt :: info! (" Hello, defmt!" ); // Prints via defmt-rtt to rtt
114
+ assert! (true )
115
+ }
116
+
117
+ // A test which is cfg'ed out
118
+ #[test]
119
+ #[cfg(abc)]
120
+ fn it_works_disabled () {
121
+ assert! (false )
122
+ }
123
+
124
+ // Tests can be ignored with the #[ignore] attribute
125
+ #[test]
126
+ #[ignore]
127
+ fn it_works_ignored () {
128
+ assert! (false )
129
+ }
130
+
131
+ // A test that fails with a panic
132
+ #[test]
133
+ fn it_fails1 () {
134
+ assert! (false )
135
+ }
136
+
137
+ // A test that fails with a returned Err()
138
+ #[test]
139
+ fn it_fails2 () -> Result <(), ()> {
140
+ Err (())
141
+ }
142
+
143
+ // Tests can be annotated with #[should_panic] if they are expected to panic
144
+ #[test]
145
+ #[should_panic]
146
+ fn it_passes () {
147
+ assert! (false )
148
+ }
149
+
150
+ // This test should panic, but doesn't => it fails
151
+ #[test]
152
+ #[should_panic]
153
+ fn it_fails3 () {}
154
+
155
+ // Tests can be annotated with #[timeout(<secs>)] to change the default timeout of 60s
156
+ #[test]
157
+ #[timeout(10)]
158
+ fn it_timeouts () {
159
+ loop {} // should run into the 60s timeout
160
+ }
92
161
}
93
162
```
94
163
0 commit comments