Skip to content

Commit d96da53

Browse files
authored
Merge pull request #108 from ianfixes/2019-01-31_fix_unittest_setup
Run unittest_setup and _teardown with every unit test
2 parents 36cd9b6 + 890a694 commit d96da53

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
### Removed
1616

1717
### Fixed
18+
- `unittest_setup()` and `unittest_teardown()` were not being executed for each unit test, only for the set of all tests. My bad.
1819

1920
### Security
2021

SampleProjects/TestSomething/test/deviceusingbytes.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ class FakeHayesModem : public DeviceUsingBytes {
2828
}
2929
};
3030

31-
unittest(modem_hardware)
31+
GodmodeState* state = GODMODE();
32+
33+
unittest_setup()
3234
{
33-
GodmodeState* state = GODMODE();
3435
state->reset();
36+
}
3537

38+
unittest(modem_hardware)
39+
{
3640
String cmd = "AT\n";
3741

3842
FakeHayesModem m;
@@ -57,9 +61,6 @@ unittest(modem_hardware)
5761

5862
unittest(modem_software)
5963
{
60-
GodmodeState* state = GODMODE();
61-
state->reset();
62-
6364
bool bigEndian = false;
6465
bool flipLogic = false;
6566
SoftwareSerial ss(1, 2, flipLogic);

SampleProjects/TestSomething/test/godmode.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#include <ArduinoUnitTests.h>
22
#include <Arduino.h>
33

4-
unittest(millis_micros_and_delay)
4+
GodmodeState* state = GODMODE();
5+
6+
unittest_setup()
57
{
6-
GodmodeState* state = GODMODE();
78
state->reset();
9+
}
10+
11+
unittest(millis_micros_and_delay)
12+
{
813
assertEqual(0, millis());
914
assertEqual(0, micros());
1015
delay(3);
@@ -17,8 +22,6 @@ unittest(millis_micros_and_delay)
1722

1823
unittest(random)
1924
{
20-
GodmodeState* state = GODMODE();
21-
state->reset();
2225
randomSeed(1);
2326
assertEqual(state->seed, 1);
2427

@@ -36,8 +39,6 @@ unittest(random)
3639

3740
unittest(pins)
3841
{
39-
GodmodeState* state = GODMODE();
40-
state->reset();
4142
pinMode(1, OUTPUT); // this is a no-op in unit tests. it's just here to prove compilation
4243
digitalWrite(1, HIGH);
4344
assertEqual(HIGH, state->digitalPin[1]);
@@ -65,9 +66,6 @@ unittest(pins)
6566

6667
unittest(pin_read_history)
6768
{
68-
GodmodeState* state = GODMODE();
69-
state->reset();
70-
7169
int future[6] = {33, 22, 55, 11, 44, 66};
7270
state->analogPin[1].fromArray(future, 6);
7371
for (int i = 0; i < 6; ++i)
@@ -92,8 +90,6 @@ unittest(pin_read_history)
9290

9391
unittest(pin_write_history)
9492
{
95-
GodmodeState *state = GODMODE();
96-
state->reset();
9793
int numMoved;
9894

9995
// history for digital pin
@@ -162,8 +158,6 @@ unittest(pin_write_history)
162158
}
163159

164160
unittest(spi) {
165-
GodmodeState *state = GODMODE();
166-
state->reset();
167161
assertEqual("", state->spi.dataIn);
168162
assertEqual("", state->spi.dataOut);
169163

@@ -215,7 +209,6 @@ unittest(spi) {
215209

216210
unittest(does_nothing_if_no_data)
217211
{
218-
GodmodeState* state = GODMODE();
219212
int myPin = 3;
220213
state->serialPort[0].dataIn = "";
221214
state->serialPort[0].dataOut = "";
@@ -227,7 +220,6 @@ unittest(spi) {
227220

228221
unittest(keeps_pin_low_and_acks)
229222
{
230-
GodmodeState* state = GODMODE();
231223
int myPin = 3;
232224
state->serialPort[0].dataIn = "0";
233225
state->serialPort[0].dataOut = "";
@@ -240,7 +232,6 @@ unittest(spi) {
240232

241233
unittest(flips_pin_high_and_acks)
242234
{
243-
GodmodeState* state = GODMODE();
244235
int myPin = 3;
245236
state->serialPort[0].dataIn = "1";
246237
state->serialPort[0].dataOut = "";
@@ -253,7 +244,6 @@ unittest(spi) {
253244

254245
unittest(two_flips)
255246
{
256-
GodmodeState* state = GODMODE();
257247
int myPin = 3;
258248
state->serialPort[0].dataIn = "10junk";
259249
state->serialPort[0].dataOut = "";

SampleProjects/TestSomething/test/setup_and_teardown.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@ unittest(add)
7070
assertEqual("11 + 22 = 33", lcd_p->s);
7171
}
7272

73+
unittest(add_again)
74+
{
75+
int result = c->add(33, 44);
76+
assertEqual(77, result);
77+
assertEqual("33 + 44 = 77", lcd_p->s);
78+
}
79+
7380
unittest_main()

cpp/unittest/ArduinoUnitTests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ class Test
154154
p->mReporter = reporter;
155155
TestData t1 = {p->name(), p->result()};
156156
if (reporter) reporter->onTestStart(t1);
157+
if (TestSetup::sInstance) TestSetup::sInstance->run();
157158
p->test();
159+
if (TestTeardown::sInstance) TestTeardown::sInstance->run();
158160
if (p->mResult == RESULT_PASS) ++results.passed;
159161
if (p->mResult == RESULT_FAIL) ++results.failed;
160162
if (p->mResult == RESULT_SKIP) ++results.skipped;
@@ -172,9 +174,7 @@ class Test
172174
static int run_and_report(int argc, char *argv[]) {
173175
// TODO: pick a reporter based on args
174176
ReporterTAP rep;
175-
if (TestSetup::sInstance) TestSetup::sInstance->run();
176177
Results results = run(&rep);
177-
if (TestTeardown::sInstance) TestTeardown::sInstance->run();
178178
return results.failed + results.skipped;
179179
}
180180

0 commit comments

Comments
 (0)