Debian Codegraph @

tests/CppUTest/CommandLineTestRunnerTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/CommandLineTestRunner.h"
30 #include "CppUTest/TestRegistry.h"
31 #include "CppUTest/TestTestingFixture.h"
32 #include "CppUTest/TestPlugin.h"
33 #include "CppUTest/JUnitTestOutput.h"
34 #include "CppUTest/PlatformSpecificFunctions.h"
35 #include "CppUTestExt/MockSupport.h"
36
37
38 class DummyPluginWhichCountsThePlugins : public TestPlugin
39 {
40 public:
41
42 bool returnValue;
43 int amountOfPlugins;
44
45 DummyPluginWhichCountsThePlugins(const SimpleString& name, TestRegistry* registry) :
46 TestPlugin(name), returnValue(true), amountOfPlugins(0), registry_(registry)
47 {
48 }
49
50 virtual bool parseArguments(int, const char *const *, int)
51 {
52 /* Remove ourselves from the count */
53 amountOfPlugins = registry_->countPlugins() - 1;
54 return returnValue;
55 }
56 private:
57 TestRegistry* registry_;
58 };
59
60 class CommandLineTestRunnerWithStringBufferOutput : public CommandLineTestRunner
61 {
62 public:
63 StringBufferTestOutput* fakeJUnitOutputWhichIsReallyABuffer_;
64 StringBufferTestOutput* fakeConsoleOutputWhichIsReallyABuffer;
65 StringBufferTestOutput* fakeTCOutputWhichIsReallyABuffer;
66
67 CommandLineTestRunnerWithStringBufferOutput(int argc, const char *const *argv, TestRegistry* registry)
68 : CommandLineTestRunner(argc, argv, registry), fakeJUnitOutputWhichIsReallyABuffer_(NULLPTR),
69 fakeConsoleOutputWhichIsReallyABuffer(NULLPTR), fakeTCOutputWhichIsReallyABuffer(NULLPTR)
70 {}
71
72 TestOutput* createConsoleOutput()
73 {
74 fakeConsoleOutputWhichIsReallyABuffer = new StringBufferTestOutput;
75 return fakeConsoleOutputWhichIsReallyABuffer;
76 }
77
78 TestOutput* createJUnitOutput(const SimpleString&)
79 {
80 fakeJUnitOutputWhichIsReallyABuffer_ = new StringBufferTestOutput;
81 return fakeJUnitOutputWhichIsReallyABuffer_;
82 }
83
84 TestOutput* createTeamCityOutput()
85 {
86 fakeTCOutputWhichIsReallyABuffer = new StringBufferTestOutput;
87 return fakeTCOutputWhichIsReallyABuffer;
88 }
89 };
90
91 TEST_GROUP(CommandLineTestRunner)
92 {
93 TestRegistry registry;
94 UtestShell *test1;
95 UtestShell *test2;
96 DummyPluginWhichCountsThePlugins* pluginCountingPlugin;
97
98 void setup()
99 {
100 test1 = new UtestShell("group1", "test1", "file1", 1);
101 test2 = new UtestShell("group2", "test2", "file2", 2);
102 registry.addTest(test1);
103 pluginCountingPlugin = new DummyPluginWhichCountsThePlugins("PluginCountingPlugin", &registry);
104 }
105 void teardown()
106 {
107 delete pluginCountingPlugin;
108 delete test2;
109 delete test1;
110 }
111
112 SimpleString runAndGetOutput(const int argc, const char* argv[])
113 {
114 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(argc, argv, &registry);
115 commandLineTestRunner.runAllTestsMain();
116 return commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput();
117 }
118 };
119
120 TEST(CommandLineTestRunner, OnePluginGetsInstalledDuringTheRunningTheTests)
121 {
122 const char* argv[] = { "tests.exe", "-psomething"};
123
124 registry.installPlugin(pluginCountingPlugin);
125
126 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
127 commandLineTestRunner.runAllTestsMain();
128 registry.removePluginByName("PluginCountingPlugin");
129
130 LONGS_EQUAL(0, registry.countPlugins());
131 LONGS_EQUAL(1, pluginCountingPlugin->amountOfPlugins);
132 }
133
134 TEST(CommandLineTestRunner, NoPluginsAreInstalledAtTheEndOfARunWhenTheArgumentsAreInvalid)
135 {
136 const char* argv[] = { "tests.exe", "-fdskjnfkds"};
137
138 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
139 commandLineTestRunner.runAllTestsMain();
140
141 LONGS_EQUAL(0, registry.countPlugins());
142
143 }
144
145 TEST(CommandLineTestRunner, ReturnsOneWhenTheArgumentsAreInvalid)
146 {
147 const char* argv[] = { "tests.exe", "-some-invalid=parameter" };
148
149 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
150 int returned = commandLineTestRunner.runAllTestsMain();
151
152 LONGS_EQUAL(1, returned);
153 }
154
155 TEST(CommandLineTestRunner, ReturnsOnePrintsHelpOnHelp)
156 {
157 const char* argv[] = { "tests.exe", "-h" };
158
159 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
160 int returned = commandLineTestRunner.runAllTestsMain();
161
162 LONGS_EQUAL(1, returned);
163 STRCMP_CONTAINS("Thanks for using CppUTest.", commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().asCharString());
164 }
165
166
167 TEST(CommandLineTestRunner, ReturnsZeroWhenNoErrors)
168 {
169 const char* argv[] = { "tests.exe" };
170
171 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(1, argv, &registry);
172 int returned = commandLineTestRunner.runAllTestsMain();
173
174 LONGS_EQUAL(0, returned);
175 }
176
177 TEST(CommandLineTestRunner, ReturnsOneWhenNoTestsMatchProvidedFilter)
178 {
179 const char* argv[] = { "tests.exe", "-g", "NoSuchGroup"};
180
181 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(3, argv, &registry);
182 int returned = commandLineTestRunner.runAllTestsMain();
183
184 LONGS_EQUAL(1, returned);
185 }
186
187 TEST(CommandLineTestRunner, TeamcityOutputEnabled)
188 {
189 const char* argv[] = {"tests.exe", "-oteamcity"};
190 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
191 commandLineTestRunner.runAllTestsMain();
192 CHECK(commandLineTestRunner.fakeTCOutputWhichIsReallyABuffer != NULLPTR);
193 }
194
195 TEST(CommandLineTestRunner, JunitOutputEnabled)
196 {
197 const char* argv[] = { "tests.exe", "-ojunit"};
198
199 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
200 commandLineTestRunner.runAllTestsMain();
201 CHECK(commandLineTestRunner.fakeJUnitOutputWhichIsReallyABuffer_ != NULLPTR);
202 }
203
204 TEST(CommandLineTestRunner, JunitOutputAndVerboseEnabled)
205 {
206 const char* argv[] = { "tests.exe", "-ojunit", "-v"};
207
208 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(3, argv, &registry);
209 commandLineTestRunner.runAllTestsMain();
210 STRCMP_CONTAINS("TEST(group1, test1)", commandLineTestRunner.fakeJUnitOutputWhichIsReallyABuffer_->getOutput().asCharString());
211 STRCMP_CONTAINS("TEST(group1, test1)", commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().asCharString());
212 }
213
214 TEST(CommandLineTestRunner, veryVerboseSetOnOutput)
215 {
216 const char* argv[] = { "tests.exe", "-vv"};
217
218 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
219 commandLineTestRunner.runAllTestsMain();
220 STRCMP_CONTAINS("TEST(group1, test1)", commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().asCharString());
221 STRCMP_CONTAINS("destroyTest", commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().asCharString());
222 }
223
224 TEST(CommandLineTestRunner, defaultTestsAreRunInOrderTheyAreInRepository)
225 {
226 const char* argv[] = { "tests.exe", "-v"};
227
228 registry.addTest(test2);
229 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
230 commandLineTestRunner.runAllTestsMain();
231
232 SimpleStringCollection stringCollection;
233 commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().split("\n", stringCollection);
234 STRCMP_CONTAINS("test2", stringCollection[0].asCharString());
235 STRCMP_CONTAINS("test1", stringCollection[1].asCharString());
236 }
237
238 TEST(CommandLineTestRunner, testsCanBeRunInReverseOrder)
239 {
240 const char* argv[] = { "tests.exe", "-v", "-b"};
241
242 registry.addTest(test2);
243 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(3, argv, &registry);
244 commandLineTestRunner.runAllTestsMain();
245
246 SimpleStringCollection stringCollection;
247 commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().split("\n", stringCollection);
248 STRCMP_CONTAINS("test1", stringCollection[0].asCharString());
249 STRCMP_CONTAINS("test2", stringCollection[1].asCharString());
250 }
251
252 TEST(CommandLineTestRunner, listTestGroupNamesShouldWorkProperly)
253 {
254 const char* argv[] = { "tests.exe", "-lg" };
255
256 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
257 commandLineTestRunner.runAllTestsMain();
258
259 STRCMP_CONTAINS("group", commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().asCharString());
260 }
261
262 TEST(CommandLineTestRunner, listTestGroupAndCaseNamesShouldWorkProperly)
263 {
264 const char* argv[] = { "tests.exe", "-ln" };
265
266 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
267 commandLineTestRunner.runAllTestsMain();
268
269 STRCMP_CONTAINS("group1.test1", commandLineTestRunner.fakeConsoleOutputWhichIsReallyABuffer->getOutput().asCharString());
270 }
271
272 TEST(CommandLineTestRunner, randomShuffleSeedIsPrintedAndRandFuncIsExercised)
273 {
274 // more than 1 item in test list ensures that shuffle algorithm calls rand_()
275 UtestShell *anotherTest = new UtestShell("group", "test2", "file", 1);
276 registry.addTest(anotherTest);
277
278 const char* argv[] = { "tests.exe", "-s"};
279 SimpleString text = runAndGetOutput(2, argv);
280 STRCMP_CONTAINS("shuffling enabled with seed:", text.asCharString());
281
282 delete anotherTest;
283 }
284
285 TEST(CommandLineTestRunner, specificShuffleSeedIsPrintedVerbose)
286 {
287 const char* argv[] = { "tests.exe", "-s2", "-v"};
288 SimpleString text = runAndGetOutput(3, argv);
289 STRCMP_CONTAINS("shuffling enabled with seed: 2", text.asCharString());
290 }
291
292 extern "C" {
293 typedef PlatformSpecificFile (*FOpenFunc)(const char*, const char*);
294 typedef void (*FPutsFunc)(const char*, PlatformSpecificFile);
295 typedef void (*FCloseFunc)(PlatformSpecificFile);
296 typedef int (*PutcharFunc)(int);
297 }
298
299 struct FakeOutput
300 {
301 FakeOutput() : SaveFOpen(PlatformSpecificFOpen), SaveFPuts(PlatformSpecificFPuts),
302 SaveFClose(PlatformSpecificFClose), SavePutchar(PlatformSpecificPutchar)
303 {
304 installFakes();
305 currentFake = this;
306 }
307
308 ~FakeOutput()
309 {
310 currentFake = NULLPTR;
311 restoreOriginals();
312 }
313
314 void installFakes()
315 {
316 PlatformSpecificFOpen = (FOpenFunc)fopen_fake;
317 PlatformSpecificFPuts = (FPutsFunc)fputs_fake;
318 PlatformSpecificFClose = (FCloseFunc)fclose_fake;
319 PlatformSpecificPutchar = (PutcharFunc)putchar_fake;
320 }
321
322 void restoreOriginals()
323 {
324 PlatformSpecificPutchar = SavePutchar;
325 PlatformSpecificFOpen = SaveFOpen;
326 PlatformSpecificFPuts = SaveFPuts;
327 PlatformSpecificFClose = SaveFClose;
328 }
329
330 static PlatformSpecificFile fopen_fake(const char*, const char*)
331 {
332 return (PlatformSpecificFile) NULLPTR;
333 }
334
335 static void fputs_fake(const char* str, PlatformSpecificFile)
336 {
337 currentFake->file += str;
338 }
339
340 static void fclose_fake(PlatformSpecificFile)
341 {
342 }
343
344 static int putchar_fake(int c)
345 {
346 currentFake->console += StringFrom((char)c);
347 return c;
348 }
349
350 SimpleString file;
351 SimpleString console;
352
353 static FakeOutput* currentFake;
354 private:
355 FOpenFunc SaveFOpen;
356 FPutsFunc SaveFPuts;
357 FCloseFunc SaveFClose;
358 PutcharFunc SavePutchar;
359 };
360
361 FakeOutput* FakeOutput::currentFake = NULLPTR;
362
363 TEST(CommandLineTestRunner, realJunitOutputShouldBeCreatedAndWorkProperly)
364 {
365 const char* argv[] = { "tests.exe", "-ojunit", "-v", "-kpackage", };
366
367 FakeOutput fakeOutput; /* UT_PTR_SET() is not reentrant */
368
369 CommandLineTestRunner commandLineTestRunner(4, argv, &registry);
370 commandLineTestRunner.runAllTestsMain();
371
372 fakeOutput.restoreOriginals();
373
374 STRCMP_CONTAINS("<testcase classname=\"package.group1\" name=\"test1\"", fakeOutput.file.asCharString());
375 STRCMP_CONTAINS("TEST(group1, test1)", fakeOutput.console.asCharString());
376
377 }
378
379 TEST(CommandLineTestRunner, realTeamCityOutputShouldBeCreatedAndWorkProperly)
380 {
381 const char* argv[] = { "tests.exe", "-oteamcity", "-v", "-kpackage", };
382
383 FakeOutput fakeOutput; /* UT_PTR_SET() is not reentrant */
384
385 CommandLineTestRunner commandLineTestRunner(4, argv, &registry);
386 commandLineTestRunner.runAllTestsMain();
387
388 fakeOutput.restoreOriginals();
389
390 STRCMP_CONTAINS("##teamcity[testSuiteStarted name='group1'", fakeOutput.console.asCharString());
391 STRCMP_CONTAINS("##teamcity[testStarted name='test1'", fakeOutput.console.asCharString());
392 STRCMP_CONTAINS("##teamcity[testFinished name='test1'", fakeOutput.console.asCharString());
393 STRCMP_CONTAINS("##teamcity[testSuiteFinished name='group1'", fakeOutput.console.asCharString());
394 }
395
396
397 class RunIgnoredUtest : public Utest
398 {
399 public:
400 static bool Checker;
401 void testBody()
402 {
403 Checker = true;
404 }
405 };
406
407 bool RunIgnoredUtest::Checker = false;
408
409 class RunIgnoredUtestShell : public IgnoredUtestShell
410 {
411 public:
412 RunIgnoredUtestShell(const char* groupName, const char* testName, const char* fileName, size_t lineNumber)
413 : IgnoredUtestShell(groupName, testName, fileName, lineNumber) {}
414 virtual Utest* createTest() _override { return new RunIgnoredUtest; }
415 };
416
417 TEST_GROUP(RunIgnoredTest)
418 {
419 TestRegistry registry;
420 RunIgnoredUtestShell *runIgnoredTest;
421 DummyPluginWhichCountsThePlugins* pluginCountingPlugin;
422
423 void setup()
424 {
425 runIgnoredTest = new RunIgnoredUtestShell("group", "test", "file", 1);
426 registry.addTest(runIgnoredTest);
427 pluginCountingPlugin = new DummyPluginWhichCountsThePlugins("PluginCountingPlugin", &registry);
428 }
429 void teardown()
430 {
431 delete pluginCountingPlugin;
432 delete runIgnoredTest;
433 RunIgnoredUtest::Checker = false;
434 }
435 };
436
437 TEST(RunIgnoredTest, IgnoreTestWillBeIgnoredIfNoOptionSpecified)
438 {
439 const char* argv[] = { "tests.exe" };
440
441 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(1, argv, &registry);
442 commandLineTestRunner.runAllTestsMain();
443
444 CHECK_FALSE( RunIgnoredUtest::Checker );
445 }
446
447 TEST(RunIgnoredTest, IgnoreTestWillGetRunIfOptionSpecified)
448 {
449 const char* argv[] = { "tests.exe", "-ri" };
450
451 CommandLineTestRunnerWithStringBufferOutput commandLineTestRunner(2, argv, &registry);
452 commandLineTestRunner.runAllTestsMain();
453
454 CHECK_TRUE( RunIgnoredUtest::Checker );
455 }
456