@@ -81,6 +81,8 @@ class Test
81
81
const char * mName ;
82
82
83
83
// linked list structure for active tests
84
+ static Test* sSetup ;
85
+ static Test* sTeardown ;
84
86
static Test* sRoot ;
85
87
Test* mNext ;
86
88
@@ -119,7 +121,13 @@ class Test
119
121
Test (const char * _name) : mName (_name) {
120
122
mResult = RESULT_NONE;
121
123
mReporter = 0 ;
122
- append ();
124
+ if (_name == " s e t u p" ) {
125
+ sSetup = this ;
126
+ } else if (_name == " t e a r d o w n" ) {
127
+ sTeardown = this ;
128
+ } else {
129
+ append ();
130
+ }
123
131
}
124
132
125
133
inline void fail () { mResult = RESULT_FAIL; }
@@ -152,7 +160,9 @@ class Test
152
160
static int run_and_report (int argc, char *argv[]) {
153
161
// TODO: pick a reporter based on args
154
162
ReporterTAP rep;
163
+ if (sSetup ) sSetup ->task ();
155
164
Results results = run (&rep);
165
+ if (sTeardown ) sTeardown ->task ();
156
166
return results.failed + results.skipped ;
157
167
}
158
168
@@ -217,6 +227,31 @@ class Test
217
227
} test_##name##_instance; \
218
228
void test_##name ::task ()
219
229
230
+ /* *
231
+ * The unittest_setup and unittest_teardown functions are intended to be used
232
+ * to set up "external" dependencies that needs to present but are not directly
233
+ * related to the functionality that you are testing, for instance a LCD.
234
+ */
235
+ #define unittest_setup () \
236
+ struct xtest_unittest_setup : Test \
237
+ { \
238
+ xtest_unittest_setup () : Test(" s e t u p" ){}; \
239
+ void task (); \
240
+ } xtest_unittest_setup_instance; \
241
+ void xtest_unittest_setup::task ()
242
+
243
+ #define unittest_teardown () \
244
+ struct xtest_unittest_teardown : Test \
245
+ { \
246
+ xtest_unittest_teardown () : Test(" t e a r d o w n" ){}; \
247
+ void task (); \
248
+ } xtest_unittest_teardown_instance; \
249
+ void xtest_unittest_teardown::task ()
250
+
251
+ // To avoid potentionally breaking existing code by re-using the Test class for
252
+ // setup and teardown, add a "x" prefix in front of names so that it will be
253
+ // different from code generated by the unittest macro. Also using _name
254
+ // arguments to Test that cannot be used in the unittest macro.
220
255
221
256
#define unittest_main () \
222
257
int main (int argc, char *argv[]) { \
0 commit comments