Skip to content

Commit 5dbc72d

Browse files
committed
Add 2-argument versions of include() and exclude() filters, matching testF() and testingF()
1 parent ac86198 commit 5dbc72d

File tree

4 files changed

+89
-16
lines changed

4 files changed

+89
-16
lines changed

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# AUnit - Unit Testing Framework for Arduino Platforms
1+
# AUnit
2+
3+
A unit testing framework for Arduino platforms inspired by ArduinoUnit and
4+
Google Test.
25

36
Version: 0.4.0 (2018-03-30)
47

@@ -87,6 +90,8 @@ Here are the features in AUnit which are not available in ArduinoUnit:
8790
* `externTestF()`
8891
* `externTestingF()`
8992
* AUnit works on the ESP8266 platform.
93+
* Test filters (`TestRunner::include()` and `TestRunner::exclude()`) support the
94+
same 2 arguments versions corresponding to `testF()` and `testingF()`
9095

9196
### Beta Status
9297

@@ -130,6 +135,7 @@ The `examples/` directory has a number of examples:
130135
In the `tests/` directory:
131136

132137
* `AUnitTest` - the unit test for `AUnit` itself has a large number of examples
138+
* `FilterTest` - manual tests for `include()` and `exclude()` filters
133139

134140
### Header and Namespace
135141

@@ -237,7 +243,7 @@ class CustomTestAgain: public TestAgain {
237243
}
238244
};
239245
240-
testingF(CustomTestAgain, examle_test) {
246+
testingF(CustomTestAgain, example_test) {
241247
...
242248
assertBigStuff();
243249
...
@@ -249,6 +255,7 @@ void setup() {
249255
250256
TestRunner::exclude("*");
251257
TestRunner::include("looping*");
258+
TestRunner::include("CustomTestAgain", "example*");
252259
}
253260
254261
void loop() {
@@ -628,36 +635,45 @@ ArduinoUnit, each call to `Test::run()` will process the entire list of
628635
currently active test cases. In AUnit, each call to `TestRunner::run()` performs
629636
only a single test case, then returns._
630637

631-
### Excluding and Including Test Cases
638+
### Filtering Test Cases
632639

633-
We can `exclude()` or `include()` test cases using a pattern match,
634-
just like ArduinoUnit. The names are slightly different:
640+
We can `exclude()` or `include()` test cases using a pattern match:
641+
642+
* `TestRunner::exclude(pattern)`
643+
* `TestRunner::exclude(testClass, pattern)`
644+
* `TestRunner::include(pattern)`
645+
* `TestRunner::include(testClass, pattern)`
635646

636-
* `TestRunner::exclude()`
637-
* `TestRunner::include()`
638647

639648
These methods are called from the global `setup()` method:
640649

641650
```
642651
void setup() {
643652
TestRunner::exclude("*");
644653
TestRunner::include("looping*");
654+
TestRunner::exclude("CustomTestAgain", "*");
655+
TestRunner::include("CustomTestAgain", "test*");
645656
...
646657
}
647658
```
648659

660+
The 2-argument versions of `include()` and `exclude()` correspond to the
661+
2 arguments of `testF()` and `testingF()`.
662+
649663
***ArduinoUnit Compatibility***:
650664
_The equivalent versions in ArduinoUnit are `Test::exclude()` and
651-
`Test::include()` The matching algorithm in AUnit is not as powerful as one in
652-
ArduinoUnit. AUnit supports only a single wildcard character `*` and that
665+
`Test::include()` The matching algorithm in AUnit is not as powerful as the one
666+
in ArduinoUnit. AUnit supports only a single wildcard character `*` and that
653667
character can appear only at the end if it is present. For example, the
654668
following are accepted:_
655669

656670
* `TestRunner::exclude("*");`
657671
* `TestRunner::include("f*");`
658672
* `TestRunner::exclude("flash_*");`
659673
* `TestRunner::include("looping*");`
660-
* `TestRunner::include("flashTest");`
674+
* `TestRunner::include("CustomTestOnce", "flashTest*");`
675+
676+
_AUnit provides 2-argument versions of `include()` and `exclude()`_
661677

662678
### Output Printer
663679

src/aunit/TestRunner.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ void TestRunner::setStatusMatchingPattern(const char* pattern, uint8_t status) {
6464
}
6565
}
6666

67+
void TestRunner::setStatusMatchingPattern(const char* testClass,
68+
const char* pattern, uint8_t status) {
69+
String fullPattern(testClass);
70+
fullPattern.concat('_');
71+
fullPattern.concat(pattern);
72+
73+
setStatusMatchingPattern(fullPattern.c_str(), status);
74+
}
75+
6776
TestRunner::TestRunner():
6877
mCurrent(nullptr),
6978
mIsResolved(false),

src/aunit/TestRunner.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ class TestRunner {
5757
getRunner()->setStatusMatchingPattern(pattern, Test::kStatusSkipped);
5858
}
5959

60+
/**
61+
* Exclude the tests which match the pattern given by (testClass + "_" +
62+
* pattern), the same concatenation rule used by the testF() macro.
63+
* Currently supports only a trailing '*'. For example,
64+
* exclude("CustomTest", "flash*").
65+
*/
66+
static void exclude(const char* testClass, const char* pattern) {
67+
getRunner()->setStatusMatchingPattern(testClass, pattern,
68+
Test::kStatusSkipped);
69+
}
70+
6071
/**
6172
* Include the tests which match the pattern.
6273
* Currently supports only a trailing '*'. For example, include("flash*").
@@ -65,6 +76,17 @@ class TestRunner {
6576
getRunner()->setStatusMatchingPattern(pattern, Test::kStatusNew);
6677
}
6778

79+
/**
80+
* Include the tests which match the pattern given by (testClass + "_" +
81+
* pattern), the same concatenation rule used by the testF() macro.
82+
* Currently supports only a trailing '*'. For example,
83+
* include("CustomTest", "flash*").
84+
*/
85+
static void include(const char* testClass, const char* pattern) {
86+
getRunner()->setStatusMatchingPattern(testClass, pattern,
87+
Test::kStatusNew);
88+
}
89+
6890
/** Set the verbosity flag. */
6991
static void setVerbosity(uint8_t verbosity) {
7092
getRunner()->setVerbosityFlag(verbosity);
@@ -129,6 +151,13 @@ class TestRunner {
129151
/** Set the status of the tests which match the pattern. */
130152
void setStatusMatchingPattern(const char* pattern, uint8_t status);
131153

154+
/**
155+
* Set the status of the tests which match the pattern formed by (testClass
156+
* + "_" + pattern), the same rule used by testF() and testingF()
157+
*/
158+
void setStatusMatchingPattern(const char* testClass, const char* pattern,
159+
uint8_t status);
160+
132161
/** Set the test runner timeout. */
133162
void setRunnerTimeout(TimeoutType seconds);
134163

tests/FilterTest/FilterTest.ino

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,44 @@ testingF(CustomAgain, display) { pass(); }
4949

5050
void setup() {
5151
delay(1000); // Wait for stability on some boards, otherwise garage on Serial
52-
Serial.begin(74880); // 74880 is the default for some ESP8266 boards
52+
Serial.begin(115200); // ESP8266 default of 74880 not supported on Linux
5353
while (! Serial); // Wait until Serial is ready - Leonardo
5454

5555
// Verify that the names of these tests don't collide and can be
56-
// independently selected. Name of test = "{test_class}_{name}".
56+
// independently selected. Name of test is "{test_class}_{name}", but we can
57+
// use the new 2-argument versions of include(testClass, pattern) and
58+
// exclude(testClass, pattern) instead.
59+
5760
TestRunner::list();
61+
62+
Serial.println("exclude(\"*\")");
5863
TestRunner::exclude("*");
5964
TestRunner::list();
65+
66+
Serial.println("include(\"configure*\")");
67+
TestRunner::include("configure");
68+
TestRunner::list();
69+
70+
Serial.println("include(\"CustomAgain*\")");
6071
TestRunner::include("CustomAgain*");
6172
TestRunner::list();
62-
TestRunner::include("CustomOnce_dis*");
73+
74+
Serial.println("exclude(\"CustomAgain\", \"*\")");
75+
TestRunner::exclude("CustomAgain", "*");
6376
TestRunner::list();
64-
TestRunner::include("configure");
77+
78+
Serial.println("include(\"CustomAgain\", \"display\")");
79+
TestRunner::include("CustomAgain", "display");
80+
TestRunner::list();
81+
82+
Serial.println("include(\"CustomOnce_dis*\")");
83+
TestRunner::include("CustomOnce_dis*");
6584
TestRunner::list();
6685
}
6786

6887
void loop() {
69-
// Should get:
88+
// Should get something like:
7089
// TestRunner summary:
71-
// 4 passed, 0 failed, 2 skipped, 0 timed out, out of 6 test(s).
90+
// 3 passed, 0 failed, 3 skipped, 0 timed out, out of 6 test(s).
7291
TestRunner::run();
7392
}

0 commit comments

Comments
 (0)