comparison ext/UnitTest++/src/tests/TestTestRunner.cpp @ 37:0d325e9d5953

added unittest++ files into ext. Not hooked into build scripts yet
author jasoka@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 12 Jul 2008 12:00:57 +0000
parents
children
comparison
equal deleted inserted replaced
36:6f1227f4932b 37:0d325e9d5953
1 #include "../UnitTest++.h"
2 #include "RecordingReporter.h"
3 #include "../ReportAssert.h"
4 #include "../TestList.h"
5 #include "../TimeHelpers.h"
6 #include "../TimeConstraint.h"
7
8 using namespace UnitTest;
9
10 namespace
11 {
12
13 struct MockTest : public Test
14 {
15 MockTest(char const* testName, bool const success_, bool const assert_, int const count_ = 1)
16 : Test(testName)
17 , success(success_)
18 , asserted(assert_)
19 , count(count_)
20 {
21 }
22
23 virtual void RunImpl(TestResults& testResults_) const
24 {
25 for (int i=0; i < count; ++i)
26 {
27 if (asserted)
28 ReportAssert("desc", "file", 0);
29 else if (!success)
30 testResults_.OnTestFailure(m_details, "message");
31 }
32 }
33
34 bool const success;
35 bool const asserted;
36 int const count;
37 };
38
39
40 struct TestRunnerFixture
41 {
42 RecordingReporter reporter;
43 TestList list;
44 };
45
46 TEST_FIXTURE(TestRunnerFixture, TestStartIsReportedCorrectly)
47 {
48 MockTest test("goodtest", true, false);
49 list.Add(&test);
50
51 RunAllTests(reporter, list, 0);
52 CHECK_EQUAL(1, reporter.testRunCount);
53 CHECK_EQUAL("goodtest", reporter.lastStartedTest);
54 }
55
56 TEST_FIXTURE(TestRunnerFixture, TestFinishIsReportedCorrectly)
57 {
58 MockTest test("goodtest", true, false);
59 list.Add(&test);
60
61 RunAllTests(reporter, list, 0);
62 CHECK_EQUAL(1, reporter.testFinishedCount);
63 CHECK_EQUAL("goodtest", reporter.lastFinishedTest);
64 }
65
66 class SlowTest : public Test
67 {
68 public:
69 SlowTest() : Test("slow", "somesuite", "filename", 123) {}
70 virtual void RunImpl(TestResults&) const
71 {
72 TimeHelpers::SleepMs(20);
73 }
74 };
75
76 TEST_FIXTURE(TestRunnerFixture, TestFinishIsCalledWithCorrectTime)
77 {
78 SlowTest test;
79 list.Add(&test);
80
81 RunAllTests(reporter, list, 0);
82 CHECK (reporter.lastFinishedTestTime >= 0.005f && reporter.lastFinishedTestTime <= 0.050f);
83 }
84
85 TEST_FIXTURE(TestRunnerFixture, FailureCountIsZeroWhenNoTestsAreRun)
86 {
87 CHECK_EQUAL(0, RunAllTests(reporter, list, 0));
88 CHECK_EQUAL(0, reporter.testRunCount);
89 CHECK_EQUAL(0, reporter.testFailedCount);
90 }
91
92 TEST_FIXTURE(TestRunnerFixture, CallsReportFailureOncePerFailingTest)
93 {
94 MockTest test1("test", false, false);
95 list.Add(&test1);
96 MockTest test2("test", true, false);
97 list.Add(&test2);
98 MockTest test3("test", false, false);
99 list.Add(&test3);
100
101 CHECK_EQUAL(2, RunAllTests(reporter, list, 0));
102 CHECK_EQUAL(2, reporter.testFailedCount);
103 }
104
105 TEST_FIXTURE(TestRunnerFixture, TestsThatAssertAreReportedAsFailing)
106 {
107 MockTest test("test", true, true);
108 list.Add(&test);
109
110 RunAllTests(reporter, list, 0);
111 CHECK_EQUAL(1, reporter.testFailedCount);
112 }
113
114
115 TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfTestCount)
116 {
117 MockTest test1("test", true, false);
118 MockTest test2("test", true, false);
119 MockTest test3("test", true, false);
120 list.Add(&test1);
121 list.Add(&test2);
122 list.Add(&test3);
123
124 RunAllTests(reporter, list, 0);
125 CHECK_EQUAL(3, reporter.summaryTotalTestCount);
126 }
127
128 TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailedTests)
129 {
130 MockTest test1("test", false, false, 2);
131 MockTest test2("test", true, false);
132 MockTest test3("test", false, false, 3);
133 list.Add(&test1);
134 list.Add(&test2);
135 list.Add(&test3);
136
137 RunAllTests(reporter, list, 0);
138 CHECK_EQUAL(2, reporter.summaryFailedTestCount);
139 }
140
141 TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailures)
142 {
143 MockTest test1("test", false, false, 2);
144 MockTest test2("test", true, false);
145 MockTest test3("test", false, false, 3);
146 list.Add(&test1);
147 list.Add(&test2);
148 list.Add(&test3);
149
150 RunAllTests(reporter, list, 0);
151 CHECK_EQUAL(5, reporter.summaryFailureCount);
152 }
153
154 TEST_FIXTURE(TestRunnerFixture, SlowTestPassesForHighTimeThreshold)
155 {
156 SlowTest test;
157 list.Add(&test);
158 RunAllTests(reporter, list, 0);
159 CHECK_EQUAL (0, reporter.testFailedCount);
160 }
161
162 TEST_FIXTURE(TestRunnerFixture, SlowTestFailsForLowTimeThreshold)
163 {
164 SlowTest test;
165 list.Add(&test);
166 RunAllTests(reporter, list, 0, 3);
167 CHECK_EQUAL (1, reporter.testFailedCount);
168 }
169
170 TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation)
171 {
172 SlowTest test;
173 list.Add(&test);
174 RunAllTests(reporter, list, 0, 3);
175 CHECK_EQUAL (test.m_details.testName, reporter.lastFailedTest);
176 CHECK (std::strstr(test.m_details.filename, reporter.lastFailedFile));
177 CHECK_EQUAL (test.m_details.lineNumber, reporter.lastFailedLine);
178 CHECK (std::strstr(reporter.lastFailedMessage, "Global time constraint failed"));
179 CHECK (std::strstr(reporter.lastFailedMessage, "3ms"));
180 }
181
182 TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses)
183 {
184 class SlowExemptedTest : public Test
185 {
186 public:
187 SlowExemptedTest() : Test("slowexempted", "", 0) {}
188 virtual void RunImpl(TestResults&) const
189 {
190 UNITTEST_TIME_CONSTRAINT_EXEMPT();
191 TimeHelpers::SleepMs(20);
192 }
193 };
194
195 SlowExemptedTest test;
196 list.Add(&test);
197 RunAllTests(reporter, list, 0, 3);
198 CHECK_EQUAL (0, reporter.testFailedCount);
199 }
200
201 struct TestSuiteFixture
202 {
203 TestSuiteFixture()
204 : test1("TestInDefaultSuite")
205 , test2("TestInOtherSuite", "OtherSuite")
206 , test3("SecondTestInDefaultSuite")
207 {
208 list.Add(&test1);
209 list.Add(&test2);
210 }
211
212 Test test1;
213 Test test2;
214 Test test3;
215 RecordingReporter reporter;
216 TestList list;
217 };
218
219 TEST_FIXTURE(TestSuiteFixture, TestRunnerRunsAllSuitesIfNullSuiteIsPassed)
220 {
221 RunAllTests(reporter, list, 0);
222 CHECK_EQUAL(2, reporter.summaryTotalTestCount);
223 }
224
225 TEST_FIXTURE(TestSuiteFixture,TestRunnerRunsOnlySpecifiedSuite)
226 {
227 RunAllTests(reporter, list, "OtherSuite");
228 CHECK_EQUAL(1, reporter.summaryTotalTestCount);
229 CHECK_EQUAL("TestInOtherSuite", reporter.lastFinishedTest);
230 }
231
232
233 }