FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
unittest-cpp/tests/TestTestRunner.cpp at master · unittest-cpp/unittest-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
unittest-cpp
/
unittest-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
175
Star
583
Code
Issues
33
Pull requests
11
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
unittest-cpp
/
tests
/
TestTestRunner.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
333 lines (272 loc) · 8.78 KB
Breadcrumbs
unittest-cpp
/
tests
/
TestTestRunner.cpp
Copy path
File metadata and controls
333 lines (272 loc) · 8.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#
include
"
UnitTest++/UnitTestPP.h
"
#
include
"
RecordingReporter.h
"
#
include
"
UnitTest++/ReportAssert.h
"
#
include
"
UnitTest++/TestList.h
"
#
include
"
UnitTest++/TimeHelpers.h
"
#
include
"
UnitTest++/TimeConstraint.h
"
#
include
"
UnitTest++/ReportAssertImpl.h
"
using
namespace
UnitTest
;
namespace
{
struct
MockTest
:
public
Test
{
MockTest
(
char
const
* testName,
bool
const
success_,
bool
const
assert_,
int
const
count_ =
1
)
: Test(testName)
, success(success_)
, asserted(assert_)
, count(count_)
{}
virtual
void
RunImpl
()
const
{
TestResults& testResults_ = *
CurrentTest::Results
();
for
(
int
i=
0
; i < count; ++i)
{
if
(asserted)
{
ReportAssert
(
"
desc
"
,
"
file
"
,
0
);
}
else
if
(!success)
{
testResults_.
OnTestFailure
(m_details,
"
message
"
);
}
}
}
bool
const
success;
bool
const
asserted;
int
const
count;
};
struct
FixtureBase
{
FixtureBase
()
: runner(reporter)
{}
template
<
class
Predicate
>
int
RunTestsIf
(TestList
const
& list,
char
const
* suiteName,
const
Predicate& predicate,
int
maxTestTimeInMs)
{
TestResults* oldResults =
CurrentTest::Results
();
const
TestDetails* oldDetails =
CurrentTest::Details
();
int
result = runner.
RunTestsIf
(list, suiteName, predicate, maxTestTimeInMs);
CurrentTest::Results
() = oldResults;
CurrentTest::Details
() = oldDetails;
return
result;
}
TestRunner runner;
RecordingReporter reporter;
};
struct
TestRunnerFixture
:
public
FixtureBase
{
TestList list;
};
TEST_FIXTURE
(TestRunnerFixture, TestStartIsReportedCorrectly)
{
MockTest
test
(
"
goodtest
"
,
true
,
false
);
list.
Add
(&test);
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
1
, reporter.
testRunCount
);
CHECK_EQUAL
(
"
goodtest
"
, reporter.
lastStartedTest
);
}
TEST_FIXTURE
(TestRunnerFixture, TestFinishIsReportedCorrectly)
{
MockTest
test
(
"
goodtest
"
,
true
,
false
);
list.
Add
(&test);
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
1
, reporter.
testFinishedCount
);
CHECK_EQUAL
(
"
goodtest
"
, reporter.
lastFinishedTest
);
}
class
SlowTest
:
public
Test
{
public:
SlowTest
()
: Test(
"
slow
"
,
"
somesuite
"
,
"
filename
"
,
123
)
{}
virtual
void
RunImpl
()
const
{
TimeHelpers::SleepMs
(
20
);
}
};
TEST_FIXTURE
(TestRunnerFixture, TestFinishIsCalledWithCorrectTime)
{
SlowTest test;
list.
Add
(&test);
//
Using UnitTest::Timer here is arguably a bit hokey and self-referential, but
//
it should guarantee that the test time recorded is less than that plus the
//
overhead of RunTestsIf -- the only thing we can reliably assert without
//
reworking the test to not use sleeps at all
Timer actual;
actual.
Start
();
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK
(reporter.
lastFinishedTestTime
>=
0
.
005f
&& reporter.
lastFinishedTestTime
<= actual.
GetTimeInMs
());
}
TEST_FIXTURE
(TestRunnerFixture, FailureCountIsZeroWhenNoTestsAreRun)
{
CHECK_EQUAL
(
0
,
RunTestsIf
(list,
NULL
,
True
(),
0
));
CHECK_EQUAL
(
0
, reporter.
testRunCount
);
CHECK_EQUAL
(
0
, reporter.
testFailedCount
);
}
TEST_FIXTURE
(TestRunnerFixture, CallsReportFailureOncePerFailingTest)
{
MockTest
test1
(
"
test
"
,
false
,
false
);
list.
Add
(&test1);
MockTest
test2
(
"
test
"
,
true
,
false
);
list.
Add
(&test2);
MockTest
test3
(
"
test
"
,
false
,
false
);
list.
Add
(&test3);
CHECK_EQUAL
(
2
,
RunTestsIf
(list,
NULL
,
True
(),
0
));
CHECK_EQUAL
(
2
, reporter.
testFailedCount
);
}
TEST_FIXTURE
(TestRunnerFixture, TestsThatAssertAreReportedAsFailing)
{
MockTest
test
(
"
test
"
,
true
,
true
);
list.
Add
(&test);
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
1
, reporter.
testFailedCount
);
}
TEST_FIXTURE
(TestRunnerFixture, ReporterNotifiedOfTestCount)
{
MockTest
test1
(
"
test
"
,
true
,
false
);
MockTest
test2
(
"
test
"
,
true
,
false
);
MockTest
test3
(
"
test
"
,
true
,
false
);
list.
Add
(&test1);
list.
Add
(&test2);
list.
Add
(&test3);
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
3
, reporter.
summaryTotalTestCount
);
}
TEST_FIXTURE
(TestRunnerFixture, ReporterNotifiedOfFailedTests)
{
MockTest
test1
(
"
test
"
,
false
,
false
,
2
);
MockTest
test2
(
"
test
"
,
true
,
false
);
MockTest
test3
(
"
test
"
,
false
,
false
,
3
);
list.
Add
(&test1);
list.
Add
(&test2);
list.
Add
(&test3);
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
2
, reporter.
summaryFailedTestCount
);
}
TEST_FIXTURE
(TestRunnerFixture, ReporterNotifiedOfFailures)
{
MockTest
test1
(
"
test
"
,
false
,
false
,
2
);
MockTest
test2
(
"
test
"
,
true
,
false
);
MockTest
test3
(
"
test
"
,
false
,
false
,
3
);
list.
Add
(&test1);
list.
Add
(&test2);
list.
Add
(&test3);
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
5
, reporter.
summaryFailureCount
);
}
TEST_FIXTURE
(TestRunnerFixture, SlowTestPassesForHighTimeThreshold)
{
SlowTest test;
list.
Add
(&test);
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
0
, reporter.
testFailedCount
);
}
TEST_FIXTURE
(TestRunnerFixture, SlowTestFailsForLowTimeThreshold)
{
SlowTest test;
list.
Add
(&test);
RunTestsIf
(list,
NULL
,
True
(),
3
);
CHECK_EQUAL
(
1
, reporter.
testFailedCount
);
}
TEST_FIXTURE
(TestRunnerFixture, SlowTestHasCorrectFailureInformation)
{
SlowTest test;
list.
Add
(&test);
RunTestsIf
(list,
NULL
,
True
(),
3
);
using
namespace
std
;
CHECK_EQUAL
(test.
m_details
.
testName
, reporter.
lastFailedTest
);
CHECK
(
strstr
(test.
m_details
.
filename
, reporter.
lastFailedFile
));
CHECK_EQUAL
(test.
m_details
.
lineNumber
, reporter.
lastFailedLine
);
CHECK
(
strstr
(reporter.
lastFailedMessage
,
"
Global time constraint failed
"
));
CHECK
(
strstr
(reporter.
lastFailedMessage
,
"
3ms
"
));
}
TEST_FIXTURE
(TestRunnerFixture, SlowTestWithTimeExemptionPasses)
{
class
SlowExemptedTest
:
public
Test
{
public:
SlowExemptedTest
() : Test(
"
slowexempted
"
,
"
"
,
0
) {}
virtual
void
RunImpl
()
const
{
UNITTEST_TIME_CONSTRAINT_EXEMPT
();
TimeHelpers::SleepMs
(
20
);
}
};
SlowExemptedTest test;
list.
Add
(&test);
RunTestsIf
(list,
NULL
,
True
(),
3
);
CHECK_EQUAL
(
0
, reporter.
testFailedCount
);
}
struct
TestSuiteFixture
: FixtureBase
{
TestSuiteFixture
()
: test1(
"
TestInDefaultSuite
"
)
, test2(
"
TestInOtherSuite
"
,
"
OtherSuite
"
)
, test3(
"
SecondTestInDefaultSuite
"
)
{
list.
Add
(&test1);
list.
Add
(&test2);
}
Test test1;
Test test2;
Test test3;
TestList list;
};
TEST_FIXTURE
(TestSuiteFixture, TestRunnerRunsAllSuitesIfNullSuiteIsPassed)
{
RunTestsIf
(list,
NULL
,
True
(),
0
);
CHECK_EQUAL
(
2
, reporter.
summaryTotalTestCount
);
}
TEST_FIXTURE
(TestSuiteFixture,TestRunnerRunsOnlySpecifiedSuite)
{
RunTestsIf
(list,
"
OtherSuite
"
,
True
(),
0
);
CHECK_EQUAL
(
1
, reporter.
summaryTotalTestCount
);
CHECK_EQUAL
(
"
TestInOtherSuite
"
, reporter.
lastFinishedTest
);
}
struct
RunTestIfNameIs
{
RunTestIfNameIs
(
char
const
* name_)
: name(name_)
{}
bool
operator
()(
const
Test*
const
test)
const
{
using
namespace
std
;
return
(
0
==
strcmp
(test->
m_details
.
testName
, name));
}
char
const
* name;
};
TEST
(TestMockPredicateBehavesCorrectly)
{
RunTestIfNameIs
predicate
(
"
pass
"
);
Test
pass
(
"
pass
"
);
Test
fail
(
"
fail
"
);
CHECK
(
predicate
(&pass));
CHECK
(!
predicate
(&fail));
}
TEST_FIXTURE
(TestRunnerFixture, TestRunnerRunsTestsThatPassPredicate)
{
Test
should_run
(
"
goodtest
"
);
list.
Add
(&should_run);
Test
should_not_run
(
"
badtest
"
);
list.
Add
(&should_not_run);
RunTestsIf
(list,
NULL
,
RunTestIfNameIs
(
"
goodtest
"
),
0
);
CHECK_EQUAL
(
1
, reporter.
testRunCount
);
CHECK_EQUAL
(
"
goodtest
"
, reporter.
lastStartedTest
);
}
TEST_FIXTURE
(TestRunnerFixture, TestRunnerOnlyRunsTestsInSpecifiedSuiteAndThatPassPredicate)
{
Test
runningTest1
(
"
goodtest
"
,
"
suite
"
);
Test
skippedTest2
(
"
goodtest
"
);
Test
skippedTest3
(
"
badtest
"
,
"
suite
"
);
Test
skippedTest4
(
"
badtest
"
);
list.
Add
(&runningTest1);
list.
Add
(&skippedTest2);
list.
Add
(&skippedTest3);
list.
Add
(&skippedTest4);
RunTestsIf
(list,
"
suite
"
,
RunTestIfNameIs
(
"
goodtest
"
),
0
);
CHECK_EQUAL
(
1
, reporter.
testRunCount
);
CHECK_EQUAL
(
"
goodtest
"
, reporter.
lastStartedTest
);
CHECK_EQUAL
(
"
suite
"
, reporter.
lastStartedSuite
);
}
}
Back
|
FazBrowse Home
|
New Git URL