| 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/JUnitTestOutput.h" |
| 30 | #include "CppUTest/TestResult.h" |
| 31 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 32 | #include "CppUTest/SimpleString.h" |
| 33 | |
| 34 | class FileForJUnitOutputTests |
| 35 | { |
| 36 | SimpleString name_; |
| 37 | bool isOpen_; |
| 38 | SimpleString buffer_; |
| 39 | FileForJUnitOutputTests* next_; |
| 40 | |
| 41 | SimpleStringCollection linesOfFile_; |
| 42 | |
| 43 | public: |
| 44 | |
| 45 | FileForJUnitOutputTests(const SimpleString& filename, FileForJUnitOutputTests* next) : |
| 46 | name_(filename), isOpen_(true), next_(next) {} |
| 47 | |
| 48 | FileForJUnitOutputTests* nextFile() |
| 49 | { |
| 50 | return next_; |
| 51 | } |
| 52 | |
| 53 | SimpleString name() |
| 54 | { |
| 55 | return name_; |
| 56 | } |
| 57 | |
| 58 | void write(const SimpleString& buffer) |
| 59 | { |
| 60 | buffer_ += buffer; |
| 61 | } |
| 62 | |
| 63 | void close() |
| 64 | { |
| 65 | isOpen_ = false; |
| 66 | } |
| 67 | |
| 68 | const char* line(size_t lineNumber) |
| 69 | { |
| 70 | buffer_.split("\n", linesOfFile_); |
| 71 | return linesOfFile_[lineNumber-1].asCharString(); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | const char* lineFromTheBack(size_t lineNumberFromTheBack) |
| 76 | { |
| 77 | return line(amountOfLines() - (lineNumberFromTheBack - 1)); |
| 78 | } |
| 79 | |
| 80 | size_t amountOfLines() |
| 81 | { |
| 82 | buffer_.split("\n", linesOfFile_); |
| 83 | return linesOfFile_.size(); |
| 84 | } |
| 85 | |
| 86 | SimpleString content() |
| 87 | { |
| 88 | return buffer_; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | class FileSystemForJUnitTestOutputTests |
| 93 | { |
| 94 | FileForJUnitOutputTests* firstFile_; |
| 95 | |
| 96 | public: |
| 97 | FileSystemForJUnitTestOutputTests() : firstFile_(NULLPTR) {} |
| 98 | ~FileSystemForJUnitTestOutputTests() { clear(); } |
| 99 | |
| 100 | void clear(void) |
| 101 | { |
| 102 | while (firstFile_) { |
| 103 | FileForJUnitOutputTests* fileToBeDeleted = firstFile_; |
| 104 | firstFile_ = firstFile_->nextFile(); |
| 105 | delete fileToBeDeleted; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | FileForJUnitOutputTests* openFile(const SimpleString& filename) |
| 110 | { |
| 111 | firstFile_ = new FileForJUnitOutputTests(filename, firstFile_); |
| 112 | return firstFile_; |
| 113 | } |
| 114 | |
| 115 | int amountOfFiles() { |
| 116 | int totalAmountOfFiles = 0; |
| 117 | for (FileForJUnitOutputTests* current = firstFile_; current != NULLPTR; current = current->nextFile()) |
| 118 | totalAmountOfFiles++; |
| 119 | return totalAmountOfFiles; |
| 120 | } |
| 121 | |
| 122 | bool fileExists(const char* filename) |
| 123 | { |
| 124 | FileForJUnitOutputTests *searchedFile = file(filename); |
| 125 | return (searchedFile != NULLPTR); |
| 126 | } |
| 127 | |
| 128 | FileForJUnitOutputTests* file(const char* filename) |
| 129 | { |
| 130 | for (FileForJUnitOutputTests* current = firstFile_; current != NULLPTR; current = current->nextFile()) |
| 131 | if (current->name() == filename) |
| 132 | return current; |
| 133 | return NULLPTR; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | extern "C" { |
| 138 | static long millisTime = 0; |
| 139 | static const char* theTime = ""; |
| 140 | |
| 141 | static long MockGetPlatformSpecificTimeInMillis() |
| 142 | { |
| 143 | return millisTime; |
| 144 | } |
| 145 | |
| 146 | static const char* MockGetPlatformSpecificTimeString() |
| 147 | { |
| 148 | return theTime; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | class JUnitTestOutputTestRunner |
| 153 | { |
| 154 | TestResult result_; |
| 155 | |
| 156 | const char* currentGroupName_; |
| 157 | UtestShell* currentTest_; |
| 158 | bool firstTestInGroup_; |
| 159 | int timeTheTestTakes_; |
| 160 | unsigned int numberOfChecksInTest_; |
| 161 | TestFailure* testFailure_; |
| 162 | |
| 163 | public: |
| 164 | |
| 165 | explicit JUnitTestOutputTestRunner(const TestResult& result) : |
| 166 | result_(result), currentGroupName_(NULLPTR), currentTest_(NULLPTR), firstTestInGroup_(true), timeTheTestTakes_(0), numberOfChecksInTest_(0), testFailure_(NULLPTR) |
| 167 | { |
| 168 | millisTime = 0; |
| 169 | theTime = "1978-10-03T00:00:00"; |
| 170 | |
| 171 | UT_PTR_SET(GetPlatformSpecificTimeInMillis, MockGetPlatformSpecificTimeInMillis); |
| 172 | UT_PTR_SET(GetPlatformSpecificTimeString, MockGetPlatformSpecificTimeString); |
| 173 | } |
| 174 | |
| 175 | JUnitTestOutputTestRunner& start() |
| 176 | { |
| 177 | result_.testsStarted(); |
| 178 | return *this; |
| 179 | } |
| 180 | |
| 181 | JUnitTestOutputTestRunner& end() |
| 182 | { |
| 183 | endOfPreviousTestGroup(); |
| 184 | delete currentTest_; |
| 185 | result_.testsEnded(); |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | JUnitTestOutputTestRunner& endGroupAndClearTest() |
| 190 | { |
| 191 | endOfPreviousTestGroup(); |
| 192 | delete currentTest_; |
| 193 | currentTest_ = NULLPTR; |
| 194 | return *this; |
| 195 | } |
| 196 | |
| 197 | void endOfPreviousTestGroup() |
| 198 | { |
| 199 | runPreviousTest(); |
| 200 | if (currentTest_) { |
| 201 | result_.currentGroupEnded(currentTest_); |
| 202 | firstTestInGroup_ = true; |
| 203 | } |
| 204 | |
| 205 | currentGroupName_ = NULLPTR; |
| 206 | } |
| 207 | |
| 208 | JUnitTestOutputTestRunner& withGroup(const char* groupName) |
| 209 | { |
| 210 | runPreviousTest(); |
| 211 | endOfPreviousTestGroup(); |
| 212 | |
| 213 | currentGroupName_ = groupName; |
| 214 | return *this; |
| 215 | } |
| 216 | |
| 217 | JUnitTestOutputTestRunner& withTest(const char* testName) |
| 218 | { |
| 219 | runPreviousTest(); |
| 220 | delete currentTest_; |
| 221 | |
| 222 | currentTest_ = new UtestShell(currentGroupName_, testName, "file", 1); |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | JUnitTestOutputTestRunner& withIgnoredTest(const char* testName) |
| 227 | { |
| 228 | runPreviousTest(); |
| 229 | delete currentTest_; |
| 230 | |
| 231 | currentTest_ = new IgnoredUtestShell(currentGroupName_, testName, "file", 1); |
| 232 | return *this; |
| 233 | } |
| 234 | |
| 235 | JUnitTestOutputTestRunner& inFile(const char* fileName) |
| 236 | { |
| 237 | if(currentTest_) { |
| 238 | currentTest_->setFileName(fileName); |
| 239 | } |
| 240 | return *this; |
| 241 | } |
| 242 | |
| 243 | JUnitTestOutputTestRunner& onLine(size_t lineNumber) |
| 244 | { |
| 245 | if(currentTest_) { |
| 246 | currentTest_->setLineNumber(lineNumber); |
| 247 | } |
| 248 | return *this; |
| 249 | } |
| 250 | |
| 251 | void runPreviousTest() |
| 252 | { |
| 253 | if (currentTest_ == NULLPTR) return; |
| 254 | |
| 255 | if (firstTestInGroup_) { |
| 256 | result_.currentGroupStarted(currentTest_); |
| 257 | firstTestInGroup_ = false; |
| 258 | } |
| 259 | result_.currentTestStarted(currentTest_); |
| 260 | |
| 261 | millisTime += timeTheTestTakes_; |
| 262 | for(unsigned int i = 0; i < numberOfChecksInTest_; i++) { |
| 263 | result_.countCheck(); |
| 264 | } |
| 265 | numberOfChecksInTest_ = 0; |
| 266 | |
| 267 | if (testFailure_) { |
| 268 | result_.addFailure(*testFailure_); |
| 269 | delete testFailure_; |
| 270 | testFailure_ = NULLPTR; |
| 271 | } |
| 272 | |
| 273 | result_.currentTestEnded(currentTest_); |
| 274 | } |
| 275 | |
| 276 | JUnitTestOutputTestRunner& thatHasChecks(unsigned int numOfChecks) |
| 277 | { |
| 278 | numberOfChecksInTest_ = numOfChecks; |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | JUnitTestOutputTestRunner& thatTakes(int timeElapsed) |
| 283 | { |
| 284 | timeTheTestTakes_ = timeElapsed; |
| 285 | return *this; |
| 286 | } |
| 287 | |
| 288 | JUnitTestOutputTestRunner& seconds() |
| 289 | { |
| 290 | return *this; |
| 291 | } |
| 292 | |
| 293 | JUnitTestOutputTestRunner& thatFails(const char* message, const char* file, size_t line) |
| 294 | { |
| 295 | testFailure_ = new TestFailure( currentTest_, file, line, message); |
| 296 | return *this; |
| 297 | } |
| 298 | |
| 299 | JUnitTestOutputTestRunner& atTime(const char* newTime) |
| 300 | { |
| 301 | theTime = newTime; |
| 302 | return *this; |
| 303 | } |
| 304 | |
| 305 | JUnitTestOutputTestRunner& thatPrints(const char* output) |
| 306 | { |
| 307 | runPreviousTest(); |
| 308 | result_.print(output); |
| 309 | return *this; |
| 310 | } |
| 311 | }; |
| 312 | |
| 313 | extern "C" { |
| 314 | static FileSystemForJUnitTestOutputTests fileSystem; |
| 315 | |
| 316 | static PlatformSpecificFile mockFOpen(const char* filename, const char*) |
| 317 | { |
| 318 | return fileSystem.openFile(filename); |
| 319 | } |
| 320 | |
| 321 | static void mockFPuts(const char* str, PlatformSpecificFile file) |
| 322 | { |
| 323 | ((FileForJUnitOutputTests*)file)->write(str); |
| 324 | } |
| 325 | |
| 326 | static void mockFClose(PlatformSpecificFile file) |
| 327 | { |
| 328 | ((FileForJUnitOutputTests*)file)->close(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | TEST_GROUP(JUnitOutputTest) |
| 333 | { |
| 334 | JUnitTestOutput *junitOutput; |
| 335 | TestResult *result; |
| 336 | JUnitTestOutputTestRunner *testCaseRunner; |
| 337 | FileForJUnitOutputTests* outputFile; |
| 338 | |
| 339 | void setup() |
| 340 | { |
| 341 | UT_PTR_SET(PlatformSpecificFOpen, mockFOpen); |
| 342 | UT_PTR_SET(PlatformSpecificFPuts, mockFPuts); |
| 343 | UT_PTR_SET(PlatformSpecificFClose, mockFClose); |
| 344 | junitOutput = new JUnitTestOutput(); |
| 345 | result = new TestResult(*junitOutput); |
| 346 | testCaseRunner = new JUnitTestOutputTestRunner(*result); |
| 347 | } |
| 348 | |
| 349 | void teardown() |
| 350 | { |
| 351 | delete testCaseRunner; |
| 352 | delete result; |
| 353 | delete junitOutput; |
| 354 | fileSystem.clear(); |
| 355 | } |
| 356 | }; |
| 357 | |
| 358 | TEST(JUnitOutputTest, withOneTestGroupAndOneTestOnlyWriteToOneFile) |
| 359 | { |
| 360 | testCaseRunner->start() |
| 361 | .withGroup("groupname").withTest("testname") |
| 362 | .end(); |
| 363 | |
| 364 | LONGS_EQUAL(1, fileSystem.amountOfFiles()); |
| 365 | CHECK(fileSystem.fileExists("cpputest_groupname.xml")); |
| 366 | } |
| 367 | |
| 368 | TEST(JUnitOutputTest, withReservedCharactersInPackageOrTestGroupUsesUnderscoresForFileName) |
| 369 | { |
| 370 | junitOutput->setPackageName("p/a\\c?k%a*g:e|n\"a<m>e."); |
| 371 | testCaseRunner->start() |
| 372 | .withGroup("g/r\\o?u%p*n:a|m\"e<h>ere").withTest("testname") |
| 373 | .end(); |
| 374 | |
| 375 | CHECK(fileSystem.fileExists("cpputest_p_a_c_k_a_g_e_n_a_m_e._g_r_o_u_p_n_a_m_e_h_ere.xml")); |
| 376 | } |
| 377 | |
| 378 | TEST(JUnitOutputTest, withOneTestGroupAndOneTestOutputsValidXMLFiles) |
| 379 | { |
| 380 | testCaseRunner->start() |
| 381 | .withGroup("groupname").withTest("testname") |
| 382 | .end(); |
| 383 | |
| 384 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 385 | STRCMP_EQUAL("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", outputFile->line(1)); |
| 386 | } |
| 387 | |
| 388 | TEST(JUnitOutputTest, withOneTestGroupAndOneTestOutputsTestSuiteStartAndEndBlocks) |
| 389 | { |
| 390 | testCaseRunner->start() |
| 391 | .withGroup("groupname").withTest("testname") |
| 392 | .end(); |
| 393 | |
| 394 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 395 | STRCMP_EQUAL("<testsuite errors=\"0\" failures=\"0\" hostname=\"localhost\" name=\"groupname\" tests=\"1\" time=\"0.000\" timestamp=\"1978-10-03T00:00:00\">\n", outputFile->line(2)); |
| 396 | STRCMP_EQUAL("</testsuite>\n", outputFile->lineFromTheBack(1)); |
| 397 | } |
| 398 | |
| 399 | TEST(JUnitOutputTest, withOneTestGroupAndOneTestFileShouldContainAnEmptyPropertiesBlock) |
| 400 | { |
| 401 | testCaseRunner->start() |
| 402 | .withGroup("groupname").withTest("testname") |
| 403 | .end(); |
| 404 | |
| 405 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 406 | STRCMP_EQUAL("<properties>\n", outputFile->line(3)); |
| 407 | STRCMP_EQUAL("</properties>\n", outputFile->line(4)); |
| 408 | } |
| 409 | |
| 410 | TEST(JUnitOutputTest, withOneTestGroupAndOneTestFileShouldContainAnEmptyStdoutBlock) |
| 411 | { |
| 412 | testCaseRunner->start() |
| 413 | .withGroup("groupname").withTest("testname") |
| 414 | .end(); |
| 415 | |
| 416 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 417 | STRCMP_EQUAL("<system-out></system-out>\n", outputFile->lineFromTheBack(3)); |
| 418 | } |
| 419 | |
| 420 | TEST(JUnitOutputTest, withOneTestGroupAndOneTestFileShouldContainAnEmptyStderrBlock) |
| 421 | { |
| 422 | testCaseRunner->start() |
| 423 | .withGroup("groupname").withTest("testname") |
| 424 | .end(); |
| 425 | |
| 426 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 427 | STRCMP_EQUAL("<system-err></system-err>\n", outputFile->lineFromTheBack(2)); |
| 428 | } |
| 429 | |
| 430 | TEST(JUnitOutputTest, withOneTestGroupAndOneTestFileShouldContainsATestCaseBlock) |
| 431 | { |
| 432 | testCaseRunner->start() |
| 433 | .withGroup("groupname").withTest("testname") |
| 434 | .end(); |
| 435 | |
| 436 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 437 | |
| 438 | STRCMP_EQUAL("<testcase classname=\"groupname\" name=\"testname\" assertions=\"0\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 439 | STRCMP_EQUAL("</testcase>\n", outputFile->line(6)); |
| 440 | } |
| 441 | |
| 442 | TEST(JUnitOutputTest, withOneTestGroupAndTwoTestCasesCreateCorrectTestgroupBlockAndCorrectTestCaseBlock) |
| 443 | { |
| 444 | testCaseRunner->start() |
| 445 | .withGroup("twoTestsGroup").withTest("firstTestName").withTest("secondTestName") |
| 446 | .end(); |
| 447 | |
| 448 | outputFile = fileSystem.file("cpputest_twoTestsGroup.xml"); |
| 449 | |
| 450 | STRCMP_EQUAL("<testsuite errors=\"0\" failures=\"0\" hostname=\"localhost\" name=\"twoTestsGroup\" tests=\"2\" time=\"0.000\" timestamp=\"1978-10-03T00:00:00\">\n", outputFile->line(2)); |
| 451 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"firstTestName\" assertions=\"0\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 452 | STRCMP_EQUAL("</testcase>\n", outputFile->line(6)); |
| 453 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"secondTestName\" assertions=\"0\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(7)); |
| 454 | STRCMP_EQUAL("</testcase>\n", outputFile->line(8)); |
| 455 | } |
| 456 | |
| 457 | TEST(JUnitOutputTest, withOneTestGroupAndTimeHasElapsedAndTimestampChanged) |
| 458 | { |
| 459 | testCaseRunner->start().atTime("2013-07-04T22:28:00") |
| 460 | .withGroup("timeGroup").withTest("Dummy").thatTakes(10).seconds() |
| 461 | .end(); |
| 462 | |
| 463 | outputFile = fileSystem.file("cpputest_timeGroup.xml"); |
| 464 | |
| 465 | STRCMP_EQUAL("<testsuite errors=\"0\" failures=\"0\" hostname=\"localhost\" name=\"timeGroup\" tests=\"1\" time=\"0.010\" timestamp=\"2013-07-04T22:28:00\">\n", outputFile->line(2)); |
| 466 | } |
| 467 | |
| 468 | TEST(JUnitOutputTest, withOneTestGroupAndMultipleTestCasesWithElapsedTime) |
| 469 | { |
| 470 | testCaseRunner->start() |
| 471 | .withGroup("twoTestsGroup") |
| 472 | .withTest("firstTestName").thatTakes(10).seconds() |
| 473 | .withTest("secondTestName").thatTakes(50).seconds() |
| 474 | .end(); |
| 475 | |
| 476 | outputFile = fileSystem.file("cpputest_twoTestsGroup.xml"); |
| 477 | STRCMP_EQUAL("<testsuite errors=\"0\" failures=\"0\" hostname=\"localhost\" name=\"twoTestsGroup\" tests=\"2\" time=\"0.060\" timestamp=\"1978-10-03T00:00:00\">\n", outputFile->line(2)); |
| 478 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"firstTestName\" assertions=\"0\" time=\"0.010\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 479 | STRCMP_EQUAL("</testcase>\n", outputFile->line(6)); |
| 480 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"secondTestName\" assertions=\"0\" time=\"0.050\" file=\"file\" line=\"1\">\n", outputFile->line(7)); |
| 481 | STRCMP_EQUAL("</testcase>\n", outputFile->line(8)); |
| 482 | } |
| 483 | |
| 484 | TEST(JUnitOutputTest, withOneTestGroupAndOneFailingTest) |
| 485 | { |
| 486 | testCaseRunner->start() |
| 487 | .withGroup("testGroupWithFailingTest") |
| 488 | .withTest("FailingTestName").thatFails("Test failed", "thisfile", 10) |
| 489 | .end(); |
| 490 | |
| 491 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 492 | STRCMP_EQUAL("<testsuite errors=\"0\" failures=\"1\" hostname=\"localhost\" name=\"testGroupWithFailingTest\" tests=\"1\" time=\"0.000\" timestamp=\"1978-10-03T00:00:00\">\n", outputFile->line(2)); |
| 493 | STRCMP_EQUAL("<testcase classname=\"testGroupWithFailingTest\" name=\"FailingTestName\" assertions=\"0\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 494 | STRCMP_EQUAL("<failure message=\"thisfile:10: Test failed\" type=\"AssertionFailedError\">\n", outputFile->line(6)); |
| 495 | STRCMP_EQUAL("</failure>\n", outputFile->line(7)); |
| 496 | STRCMP_EQUAL("</testcase>\n", outputFile->line(8)); |
| 497 | } |
| 498 | |
| 499 | TEST(JUnitOutputTest, withTwoTestGroupAndOneFailingTest) |
| 500 | { |
| 501 | testCaseRunner->start() |
| 502 | .withGroup("testGroupWithFailingTest") |
| 503 | .withTest("FirstTest") |
| 504 | .withTest("FailingTestName").thatFails("Test failed", "thisfile", 10) |
| 505 | .end(); |
| 506 | |
| 507 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 508 | |
| 509 | STRCMP_EQUAL("<testsuite errors=\"0\" failures=\"1\" hostname=\"localhost\" name=\"testGroupWithFailingTest\" tests=\"2\" time=\"0.000\" timestamp=\"1978-10-03T00:00:00\">\n", outputFile->line(2)); |
| 510 | STRCMP_EQUAL("<testcase classname=\"testGroupWithFailingTest\" name=\"FailingTestName\" assertions=\"0\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(7)); |
| 511 | STRCMP_EQUAL("<failure message=\"thisfile:10: Test failed\" type=\"AssertionFailedError\">\n", outputFile->line(8)); |
| 512 | } |
| 513 | |
| 514 | TEST(JUnitOutputTest, testFailureWithLessThanAndGreaterThanInsideIt) |
| 515 | { |
| 516 | testCaseRunner->start() |
| 517 | .withGroup("testGroupWithFailingTest") |
| 518 | .withTest("FailingTestName").thatFails("Test <failed>", "thisfile", 10) |
| 519 | .end(); |
| 520 | |
| 521 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 522 | |
| 523 | STRCMP_EQUAL("<failure message=\"thisfile:10: Test <failed>\" type=\"AssertionFailedError\">\n", outputFile->line(6)); |
| 524 | } |
| 525 | |
| 526 | TEST(JUnitOutputTest, testFailureWithQuotesInIt) |
| 527 | { |
| 528 | testCaseRunner->start() |
| 529 | .withGroup("testGroupWithFailingTest") |
| 530 | .withTest("FailingTestName").thatFails("Test \"failed\"", "thisfile", 10) |
| 531 | .end(); |
| 532 | |
| 533 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 534 | |
| 535 | STRCMP_EQUAL("<failure message=\"thisfile:10: Test "failed"\" type=\"AssertionFailedError\">\n", outputFile->line(6)); |
| 536 | } |
| 537 | |
| 538 | TEST(JUnitOutputTest, testFailureWithNewlineInIt) |
| 539 | { |
| 540 | testCaseRunner->start() |
| 541 | .withGroup("testGroupWithFailingTest") |
| 542 | .withTest("FailingTestName").thatFails("Test \nfailed", "thisfile", 10) |
| 543 | .end(); |
| 544 | |
| 545 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 546 | |
| 547 | STRCMP_EQUAL("<failure message=\"thisfile:10: Test {newline}failed\" type=\"AssertionFailedError\">\n", outputFile->line(6)); |
| 548 | } |
| 549 | |
| 550 | TEST(JUnitOutputTest, testFailureWithDifferentFileAndLine) |
| 551 | { |
| 552 | testCaseRunner->start() |
| 553 | .withGroup("testGroupWithFailingTest") |
| 554 | .withTest("FailingTestName").thatFails("Test failed", "importantFile", 999) |
| 555 | .end(); |
| 556 | |
| 557 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 558 | |
| 559 | STRCMP_EQUAL("<failure message=\"importantFile:999: Test failed\" type=\"AssertionFailedError\">\n", outputFile->line(6)); |
| 560 | } |
| 561 | |
| 562 | TEST(JUnitOutputTest, testFailureWithAmpersandsAndLessThan) |
| 563 | { |
| 564 | testCaseRunner->start() |
| 565 | .withGroup("testGroupWithFailingTest") |
| 566 | .withTest("FailingTestName").thatFails("&object1 < &object2", "importantFile", 999) |
| 567 | .end(); |
| 568 | |
| 569 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 570 | |
| 571 | STRCMP_EQUAL("<failure message=\"importantFile:999: &object1 < &object2\" type=\"AssertionFailedError\">\n", outputFile->line(6)); |
| 572 | } |
| 573 | |
| 574 | TEST(JUnitOutputTest, testFailureWithAmpersands) |
| 575 | { |
| 576 | testCaseRunner->start() |
| 577 | .withGroup("testGroupWithFailingTest") |
| 578 | .withTest("FailingTestName").thatFails("&object1 != &object2", "importantFile", 999) |
| 579 | .end(); |
| 580 | |
| 581 | outputFile = fileSystem.file("cpputest_testGroupWithFailingTest.xml"); |
| 582 | |
| 583 | STRCMP_EQUAL("<failure message=\"importantFile:999: &object1 != &object2\" type=\"AssertionFailedError\">\n", outputFile->line(6)); |
| 584 | } |
| 585 | |
| 586 | TEST(JUnitOutputTest, aCoupleOfTestFailures) |
| 587 | { |
| 588 | testCaseRunner->start() |
| 589 | .withGroup("testGroup") |
| 590 | .withTest("passingOne") |
| 591 | .withTest("FailingTest").thatFails("Failure", "file", 99) |
| 592 | .withTest("passingTwo") |
| 593 | .withTest("passingThree") |
| 594 | .withTest("AnotherFailingTest").thatFails("otherFailure", "anotherFile", 10) |
| 595 | .end(); |
| 596 | |
| 597 | outputFile = fileSystem.file("cpputest_testGroup.xml"); |
| 598 | |
| 599 | STRCMP_EQUAL("<failure message=\"file:99: Failure\" type=\"AssertionFailedError\">\n", outputFile->line(8)); |
| 600 | STRCMP_EQUAL("<failure message=\"anotherFile:10: otherFailure\" type=\"AssertionFailedError\">\n", outputFile->line(16)); |
| 601 | } |
| 602 | |
| 603 | TEST(JUnitOutputTest, testFailuresInSeparateGroups) |
| 604 | { |
| 605 | testCaseRunner->start() |
| 606 | .withGroup("testGroup") |
| 607 | .withTest("passingOne") |
| 608 | .withTest("FailingTest").thatFails("Failure", "file", 99) |
| 609 | .withGroup("AnotherGroup") |
| 610 | .withTest("AnotherFailingTest").thatFails("otherFailure", "anotherFile", 10) |
| 611 | .end(); |
| 612 | |
| 613 | outputFile = fileSystem.file("cpputest_testGroup.xml"); |
| 614 | |
| 615 | STRCMP_EQUAL("<failure message=\"file:99: Failure\" type=\"AssertionFailedError\">\n", outputFile->line(8)); |
| 616 | |
| 617 | outputFile = fileSystem.file("cpputest_AnotherGroup.xml"); |
| 618 | STRCMP_EQUAL("<failure message=\"anotherFile:10: otherFailure\" type=\"AssertionFailedError\">\n", outputFile->line(8)); |
| 619 | } |
| 620 | |
| 621 | TEST(JUnitOutputTest, twoTestGroupsWriteToTwoDifferentFiles) |
| 622 | { |
| 623 | testCaseRunner->start() |
| 624 | .withGroup("firstTestGroup") |
| 625 | .withTest("testName") |
| 626 | .withGroup("secondTestGroup") |
| 627 | .withTest("testName") |
| 628 | .end(); |
| 629 | |
| 630 | CHECK(fileSystem.file("cpputest_firstTestGroup.xml") != NULLPTR); |
| 631 | CHECK(fileSystem.file("cpputest_secondTestGroup.xml") != NULLPTR); |
| 632 | |
| 633 | } |
| 634 | |
| 635 | TEST(JUnitOutputTest, testGroupWithWeirdName) |
| 636 | { |
| 637 | STRCMP_EQUAL("cpputest_group_weird_name.xml", junitOutput->createFileName("group/weird/name").asCharString()); |
| 638 | } |
| 639 | |
| 640 | TEST(JUnitOutputTest, TestCaseBlockWithAPackageName) |
| 641 | { |
| 642 | junitOutput->setPackageName("packagename"); |
| 643 | testCaseRunner->start() |
| 644 | .withGroup("groupname").withTest("testname") |
| 645 | .end(); |
| 646 | |
| 647 | outputFile = fileSystem.file("cpputest_packagename_groupname.xml"); |
| 648 | |
| 649 | STRCMP_EQUAL("<testcase classname=\"packagename.groupname\" name=\"testname\" assertions=\"0\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 650 | STRCMP_EQUAL("</testcase>\n", outputFile->line(6)); |
| 651 | } |
| 652 | |
| 653 | TEST(JUnitOutputTest, TestCaseBlockForIgnoredTest) |
| 654 | { |
| 655 | junitOutput->setPackageName("packagename"); |
| 656 | testCaseRunner->start() |
| 657 | .withGroup("groupname").withIgnoredTest("testname") |
| 658 | .end(); |
| 659 | |
| 660 | outputFile = fileSystem.file("cpputest_packagename_groupname.xml"); |
| 661 | |
| 662 | STRCMP_EQUAL("<testcase classname=\"packagename.groupname\" name=\"testname\" assertions=\"0\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 663 | STRCMP_EQUAL("<skipped />\n", outputFile->line(6)); |
| 664 | STRCMP_EQUAL("</testcase>\n", outputFile->line(7)); |
| 665 | } |
| 666 | |
| 667 | TEST(JUnitOutputTest, TestCaseWithTestLocation) |
| 668 | { |
| 669 | junitOutput->setPackageName("packagename"); |
| 670 | testCaseRunner->start() |
| 671 | .withGroup("groupname") |
| 672 | .withTest("testname").inFile("MySource.c").onLine(159) |
| 673 | .end(); |
| 674 | |
| 675 | outputFile = fileSystem.file("cpputest_packagename_groupname.xml"); |
| 676 | |
| 677 | STRCMP_EQUAL("<testcase classname=\"packagename.groupname\" name=\"testname\" assertions=\"0\" time=\"0.000\" file=\"MySource.c\" line=\"159\">\n", outputFile->line(5)); |
| 678 | } |
| 679 | |
| 680 | TEST(JUnitOutputTest, MultipleTestCaseWithTestLocations) |
| 681 | { |
| 682 | testCaseRunner->start() |
| 683 | .withGroup("twoTestsGroup") |
| 684 | .withTest("firstTestName").inFile("MyFirstSource.c").onLine(846) |
| 685 | .withTest("secondTestName").inFile("MySecondSource.c").onLine(513) |
| 686 | .end(); |
| 687 | |
| 688 | outputFile = fileSystem.file("cpputest_twoTestsGroup.xml"); |
| 689 | |
| 690 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"firstTestName\" assertions=\"0\" time=\"0.000\" file=\"MyFirstSource.c\" line=\"846\">\n", outputFile->line(5)); |
| 691 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"secondTestName\" assertions=\"0\" time=\"0.000\" file=\"MySecondSource.c\" line=\"513\">\n", outputFile->line(7)); |
| 692 | } |
| 693 | |
| 694 | TEST(JUnitOutputTest, TestCaseBlockWithAssertions) |
| 695 | { |
| 696 | junitOutput->setPackageName("packagename"); |
| 697 | testCaseRunner->start() |
| 698 | .withGroup("groupname") |
| 699 | .withTest("testname") |
| 700 | .thatHasChecks(24) |
| 701 | .end(); |
| 702 | |
| 703 | outputFile = fileSystem.file("cpputest_packagename_groupname.xml"); |
| 704 | |
| 705 | STRCMP_EQUAL("<testcase classname=\"packagename.groupname\" name=\"testname\" assertions=\"24\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 706 | } |
| 707 | |
| 708 | TEST(JUnitOutputTest, MultipleTestCaseBlocksWithAssertions) |
| 709 | { |
| 710 | testCaseRunner->start() |
| 711 | .withGroup("twoTestsGroup") |
| 712 | .withTest("firstTestName").thatHasChecks(456) |
| 713 | .withTest("secondTestName").thatHasChecks(567) |
| 714 | .end(); |
| 715 | |
| 716 | outputFile = fileSystem.file("cpputest_twoTestsGroup.xml"); |
| 717 | |
| 718 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"firstTestName\" assertions=\"456\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 719 | STRCMP_EQUAL("<testcase classname=\"twoTestsGroup\" name=\"secondTestName\" assertions=\"567\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(7)); |
| 720 | } |
| 721 | |
| 722 | TEST(JUnitOutputTest, MultipleTestCasesInDifferentGroupsWithAssertions) |
| 723 | { |
| 724 | testCaseRunner->start() |
| 725 | .withGroup("groupOne") |
| 726 | .withTest("testA").thatHasChecks(456) |
| 727 | .endGroupAndClearTest() |
| 728 | .withGroup("groupTwo") |
| 729 | .withTest("testB").thatHasChecks(678) |
| 730 | .end(); |
| 731 | |
| 732 | outputFile = fileSystem.file("cpputest_groupOne.xml"); |
| 733 | STRCMP_EQUAL("<testcase classname=\"groupOne\" name=\"testA\" assertions=\"456\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 734 | |
| 735 | outputFile = fileSystem.file("cpputest_groupTwo.xml"); |
| 736 | STRCMP_EQUAL("<testcase classname=\"groupTwo\" name=\"testB\" assertions=\"678\" time=\"0.000\" file=\"file\" line=\"1\">\n", outputFile->line(5)); |
| 737 | } |
| 738 | |
| 739 | TEST(JUnitOutputTest, UTPRINTOutputInJUnitOutput) |
| 740 | { |
| 741 | testCaseRunner->start() |
| 742 | .withGroup("groupname") |
| 743 | .withTest("testname").thatPrints("someoutput") |
| 744 | .end(); |
| 745 | |
| 746 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 747 | STRCMP_EQUAL("<system-out>someoutput</system-out>\n", outputFile->lineFromTheBack(3)); |
| 748 | } |
| 749 | |
| 750 | TEST(JUnitOutputTest, UTPRINTOutputInJUnitOutputWithSpecials) |
| 751 | { |
| 752 | testCaseRunner->start() |
| 753 | .withGroup("groupname") |
| 754 | .withTest("testname").thatPrints("The <rain> in \"Spain\"\nGoes \\mainly\\ down the Dr&in\n") |
| 755 | .end(); |
| 756 | |
| 757 | outputFile = fileSystem.file("cpputest_groupname.xml"); |
| 758 | STRCMP_EQUAL("<system-out>The <rain> in "Spain"{newline}Goes \\mainly\\ down the Dr&in{newline}</system-out>\n", outputFile->lineFromTheBack(3)); |
| 759 | } |