1
#include "../UnitTest++.h"
2
#include "RecordingReporter.h"
3
#include "../ReportAssert.h"
4
#include "../TestList.h"
5
#include "../TimeHelpers.h"
6
#include "../TimeConstraint.h"
8
using namespace UnitTest;
13
struct MockTest : public Test
15
MockTest(char const* testName, bool const success_, bool const assert_, int const count_ = 1)
23
virtual void RunImpl(TestResults& testResults_) const
25
for (int i=0; i < count; ++i)
28
ReportAssert("desc", "file", 0);
30
testResults_.OnTestFailure(m_details, "message");
40
struct TestRunnerFixture
47
RecordingReporter reporter;
52
TEST_FIXTURE(TestRunnerFixture, TestStartIsReportedCorrectly)
54
MockTest test("goodtest", true, false);
57
runner.RunTestsIf(list, NULL, True(), 0);
58
CHECK_EQUAL(1, reporter.testRunCount);
59
CHECK_EQUAL("goodtest", reporter.lastStartedTest);
62
TEST_FIXTURE(TestRunnerFixture, TestFinishIsReportedCorrectly)
64
MockTest test("goodtest", true, false);
67
runner.RunTestsIf(list, NULL, True(), 0);
68
CHECK_EQUAL(1, reporter.testFinishedCount);
69
CHECK_EQUAL("goodtest", reporter.lastFinishedTest);
72
class SlowTest : public Test
75
SlowTest() : Test("slow", "somesuite", "filename", 123) {}
76
virtual void RunImpl(TestResults&) const
78
TimeHelpers::SleepMs(20);
82
TEST_FIXTURE(TestRunnerFixture, TestFinishIsCalledWithCorrectTime)
87
runner.RunTestsIf(list, NULL, True(), 0);
88
CHECK(reporter.lastFinishedTestTime >= 0.005f && reporter.lastFinishedTestTime <= 0.050f);
91
TEST_FIXTURE(TestRunnerFixture, FailureCountIsZeroWhenNoTestsAreRun)
93
CHECK_EQUAL(0, runner.RunTestsIf(list, NULL, True(), 0));
94
CHECK_EQUAL(0, reporter.testRunCount);
95
CHECK_EQUAL(0, reporter.testFailedCount);
98
TEST_FIXTURE(TestRunnerFixture, CallsReportFailureOncePerFailingTest)
100
MockTest test1("test", false, false);
102
MockTest test2("test", true, false);
104
MockTest test3("test", false, false);
107
CHECK_EQUAL(2, runner.RunTestsIf(list, NULL, True(), 0));
108
CHECK_EQUAL(2, reporter.testFailedCount);
111
TEST_FIXTURE(TestRunnerFixture, TestsThatAssertAreReportedAsFailing)
113
MockTest test("test", true, true);
116
runner.RunTestsIf(list, NULL, True(), 0);
117
CHECK_EQUAL(1, reporter.testFailedCount);
121
TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfTestCount)
123
MockTest test1("test", true, false);
124
MockTest test2("test", true, false);
125
MockTest test3("test", true, false);
130
runner.RunTestsIf(list, NULL, True(), 0);
131
CHECK_EQUAL(3, reporter.summaryTotalTestCount);
134
TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailedTests)
136
MockTest test1("test", false, false, 2);
137
MockTest test2("test", true, false);
138
MockTest test3("test", false, false, 3);
143
runner.RunTestsIf(list, NULL, True(), 0);
144
CHECK_EQUAL(2, reporter.summaryFailedTestCount);
147
TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailures)
149
MockTest test1("test", false, false, 2);
150
MockTest test2("test", true, false);
151
MockTest test3("test", false, false, 3);
156
runner.RunTestsIf(list, NULL, True(), 0);
157
CHECK_EQUAL(5, reporter.summaryFailureCount);
160
TEST_FIXTURE(TestRunnerFixture, SlowTestPassesForHighTimeThreshold)
165
runner.RunTestsIf(list, NULL, True(), 0);
166
CHECK_EQUAL(0, reporter.testFailedCount);
169
TEST_FIXTURE(TestRunnerFixture, SlowTestFailsForLowTimeThreshold)
174
runner.RunTestsIf(list, NULL, True(), 3);
175
CHECK_EQUAL(1, reporter.testFailedCount);
178
TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation)
183
runner.RunTestsIf(list, NULL, True(), 3);
187
CHECK_EQUAL(test.m_details.testName, reporter.lastFailedTest);
188
CHECK(strstr(test.m_details.filename, reporter.lastFailedFile));
189
CHECK_EQUAL(test.m_details.lineNumber, reporter.lastFailedLine);
191
CHECK(strstr(reporter.lastFailedMessage, "Global time constraint failed"));
192
CHECK(strstr(reporter.lastFailedMessage, "3ms"));
195
TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses)
197
class SlowExemptedTest : public Test
200
SlowExemptedTest() : Test("slowexempted", "", 0) {}
201
virtual void RunImpl(TestResults&) const
203
UNITTEST_TIME_CONSTRAINT_EXEMPT();
204
TimeHelpers::SleepMs(20);
208
SlowExemptedTest test;
211
runner.RunTestsIf(list, NULL, True(), 3);
212
CHECK_EQUAL(0, reporter.testFailedCount);
215
struct TestSuiteFixture
218
: test1("TestInDefaultSuite")
219
, test2("TestInOtherSuite", "OtherSuite")
220
, test3("SecondTestInDefaultSuite")
230
RecordingReporter reporter;
235
TEST_FIXTURE(TestSuiteFixture, TestRunnerRunsAllSuitesIfNullSuiteIsPassed)
237
runner.RunTestsIf(list, NULL, True(), 0);
238
CHECK_EQUAL(2, reporter.summaryTotalTestCount);
241
TEST_FIXTURE(TestSuiteFixture,TestRunnerRunsOnlySpecifiedSuite)
243
runner.RunTestsIf(list, "OtherSuite", True(), 0);
244
CHECK_EQUAL(1, reporter.summaryTotalTestCount);
245
CHECK_EQUAL("TestInOtherSuite", reporter.lastFinishedTest);
248
struct RunTestIfNameIs
250
RunTestIfNameIs(char const* name_)
255
bool operator()(const Test* const test) const
258
return (0 == strcmp(test->m_details.testName, name));
264
TEST(TestMockPredicateBehavesCorrectly)
266
RunTestIfNameIs predicate("pass");
271
CHECK(predicate(&pass));
272
CHECK(!predicate(&fail));
275
TEST_FIXTURE(TestRunnerFixture, TestRunnerRunsTestsThatPassPredicate)
277
Test should_run("goodtest");
278
list.Add(&should_run);
280
Test should_not_run("badtest");
281
list.Add(&should_not_run);
283
runner.RunTestsIf(list, NULL, RunTestIfNameIs("goodtest"), 0);
284
CHECK_EQUAL(1, reporter.testRunCount);
285
CHECK_EQUAL("goodtest", reporter.lastStartedTest);
288
TEST_FIXTURE(TestRunnerFixture, TestRunnerOnlyRunsTestsInSpecifiedSuiteAndThatPassPredicate)
290
Test runningTest1("goodtest", "suite");
291
Test skippedTest2("goodtest");
292
Test skippedTest3("badtest", "suite");
293
Test skippedTest4("badtest");
295
list.Add(&runningTest1);
296
list.Add(&skippedTest2);
297
list.Add(&skippedTest3);
298
list.Add(&skippedTest4);
300
runner.RunTestsIf(list, "suite", RunTestIfNameIs("goodtest"), 0);
302
CHECK_EQUAL(1, reporter.testRunCount);
303
CHECK_EQUAL("goodtest", reporter.lastStartedTest);
304
CHECK_EQUAL("suite", reporter.lastStartedSuite);