| 1 | #include "CppUTest/TestHarness.h" |
| 2 | #include "CppUTest/TeamCityTestOutput.h" |
| 3 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 4 | |
| 5 | class TeamCityOutputToBuffer : public TeamCityTestOutput |
| 6 | { |
| 7 | public: |
| 8 | explicit TeamCityOutputToBuffer() |
| 9 | { |
| 10 | } |
| 11 | |
| 12 | virtual ~TeamCityOutputToBuffer() _destructor_override |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | void printBuffer(const char* s) _override |
| 17 | { |
| 18 | output += s; |
| 19 | } |
| 20 | |
| 21 | void flush() _override |
| 22 | { |
| 23 | output = ""; |
| 24 | } |
| 25 | |
| 26 | const SimpleString& getOutput() |
| 27 | { |
| 28 | return output; |
| 29 | } |
| 30 | |
| 31 | private: |
| 32 | SimpleString output; |
| 33 | }; |
| 34 | |
| 35 | static long millisTime; |
| 36 | |
| 37 | extern "C" { |
| 38 | |
| 39 | static long MockGetPlatformSpecificTimeInMillis() |
| 40 | { |
| 41 | return millisTime; |
| 42 | } |
| 43 | |
| 44 | } |
| 45 | |
| 46 | TEST_GROUP(TeamCityOutputTest) |
| 47 | { |
| 48 | TeamCityTestOutput* tcout; |
| 49 | TeamCityOutputToBuffer* mock; |
| 50 | UtestShell* tst; |
| 51 | TestFailure *f, *f2, *f3; |
| 52 | TestResult* result; |
| 53 | |
| 54 | void setup() |
| 55 | { |
| 56 | mock = new TeamCityOutputToBuffer(); |
| 57 | tcout = mock; |
| 58 | tst = new UtestShell("group", "test", "file", 10); |
| 59 | f = new TestFailure(tst, "failfile", 20, "failure message"); |
| 60 | f2 = new TestFailure(tst, "file", 20, "message"); |
| 61 | f3 = new TestFailure(tst, "file", 30, "apos' pipe| [brackets]\r\nCRLF"); |
| 62 | result = new TestResult(*mock); |
| 63 | result->setTotalExecutionTime(10); |
| 64 | millisTime = 0; |
| 65 | UT_PTR_SET(GetPlatformSpecificTimeInMillis, MockGetPlatformSpecificTimeInMillis); |
| 66 | } |
| 67 | void teardown() |
| 68 | { |
| 69 | delete tcout; |
| 70 | delete tst; |
| 71 | delete f; |
| 72 | delete f2; |
| 73 | delete f3; |
| 74 | delete result; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | TEST(TeamCityOutputTest, PrintGroupStarted) |
| 79 | { |
| 80 | result->currentGroupStarted(tst); |
| 81 | STRCMP_EQUAL("##teamcity[testSuiteStarted name='group']\n", mock->getOutput().asCharString()); |
| 82 | } |
| 83 | |
| 84 | TEST(TeamCityOutputTest, PrintGroupStartedAndEnded) |
| 85 | { |
| 86 | const char* expected = "##teamcity[testSuiteStarted name='group']\n" |
| 87 | "##teamcity[testSuiteFinished name='group']\n"; |
| 88 | result->currentGroupStarted(tst); |
| 89 | result->currentGroupEnded(tst); |
| 90 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 91 | } |
| 92 | |
| 93 | TEST(TeamCityOutputTest, PrintGroupEndedButNotStarted) |
| 94 | { |
| 95 | result->currentGroupEnded(tst); |
| 96 | STRCMP_EQUAL("", mock->getOutput().asCharString()); |
| 97 | } |
| 98 | |
| 99 | TEST(TeamCityOutputTest, PrintTestStarted) |
| 100 | { |
| 101 | tcout->printCurrentTestStarted(*tst); |
| 102 | STRCMP_EQUAL("##teamcity[testStarted name='test']\n", mock->getOutput().asCharString()); |
| 103 | } |
| 104 | |
| 105 | TEST(TeamCityOutputTest, PrintTestStartedAndEnded) |
| 106 | { |
| 107 | result->currentTestStarted(tst); |
| 108 | millisTime = 42; |
| 109 | result->currentTestEnded(tst); |
| 110 | STRCMP_EQUAL("##teamcity[testStarted name='test']\n##teamcity[testFinished name='test' duration='42']\n", |
| 111 | mock->getOutput().asCharString()); |
| 112 | } |
| 113 | |
| 114 | TEST(TeamCityOutputTest, PrintTestEndedButNotStarted) |
| 115 | { |
| 116 | result->currentTestEnded(tst); |
| 117 | STRCMP_EQUAL("", mock->getOutput().asCharString()); |
| 118 | } |
| 119 | |
| 120 | TEST(TeamCityOutputTest, PrintTestIgnored) |
| 121 | { |
| 122 | const char* expected = |
| 123 | "##teamcity[testStarted name='test']\n" |
| 124 | "##teamcity[testIgnored name='test']\n" |
| 125 | "##teamcity[testFinished name='test' duration='41']\n"; |
| 126 | |
| 127 | IgnoredUtestShell* itst = new IgnoredUtestShell("group", "test", "file", 10); |
| 128 | result->currentTestStarted(itst); |
| 129 | millisTime = 41; |
| 130 | result->currentTestEnded(itst); |
| 131 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 132 | delete itst; |
| 133 | } |
| 134 | |
| 135 | TEST(TeamCityOutputTest, PrintWithFailureInSameFile) |
| 136 | { |
| 137 | tcout->printFailure(*f2); |
| 138 | const char* expected = |
| 139 | "##teamcity[testFailed name='test' message='file:20' " |
| 140 | "details='message']\n"; |
| 141 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 142 | } |
| 143 | |
| 144 | TEST(TeamCityOutputTest, PrintWithEscapedCharacters) |
| 145 | { |
| 146 | tcout->printFailure(*f3); |
| 147 | const char* expected = |
| 148 | "##teamcity[testFailed name='test' message='file:30' " |
| 149 | "details='apos|' pipe|| |[brackets|]" |
| 150 | "|r|nCRLF']\n"; |
| 151 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 152 | } |
| 153 | |
| 154 | TEST(TeamCityOutputTest, PrintFailureWithFailInDifferentFile) |
| 155 | { |
| 156 | tcout->printFailure(*f); |
| 157 | const char* expected = |
| 158 | "##teamcity[testFailed name='test' message='TEST failed (file:10): failfile:20' " |
| 159 | "details='failure message']\n"; |
| 160 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 161 | } |
| 162 | |
| 163 | TEST(TeamCityOutputTest, TestGroupEscaped_Start) |
| 164 | { |
| 165 | tst->setGroupName("'[]\n\r"); |
| 166 | result->currentGroupStarted(tst); |
| 167 | const char* expected = |
| 168 | "##teamcity[testSuiteStarted name='|'|[|]|n|r']\n"; |
| 169 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 170 | } |
| 171 | |
| 172 | TEST(TeamCityOutputTest, TestGroupEscaped_End) |
| 173 | { |
| 174 | tst->setGroupName("'[]\n\r"); |
| 175 | result->currentGroupStarted(tst); |
| 176 | result->currentGroupEnded(tst); |
| 177 | const char* expected = |
| 178 | "##teamcity[testSuiteStarted name='|'|[|]|n|r']\n" |
| 179 | "##teamcity[testSuiteFinished name='|'|[|]|n|r']\n"; |
| 180 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 181 | } |
| 182 | |
| 183 | TEST(TeamCityOutputTest, TestNameEscaped_Start) |
| 184 | { |
| 185 | tst->setTestName("'[]\n\r"); |
| 186 | result->currentTestStarted(tst); |
| 187 | const char* expected = |
| 188 | "##teamcity[testStarted name='|'|[|]|n|r']\n"; |
| 189 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 190 | } |
| 191 | |
| 192 | TEST(TeamCityOutputTest, TestNameEscaped_End) |
| 193 | { |
| 194 | tst->setTestName("'[]\n\r"); |
| 195 | result->currentTestStarted(tst); |
| 196 | result->currentTestEnded(tst); |
| 197 | const char* expected = |
| 198 | "##teamcity[testStarted name='|'|[|]|n|r']\n" |
| 199 | "##teamcity[testFinished name='|'|[|]|n|r' duration='0']\n"; |
| 200 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 201 | } |
| 202 | |
| 203 | TEST(TeamCityOutputTest, TestNameEscaped_Ignore) |
| 204 | { |
| 205 | IgnoredUtestShell itst("group", "'[]\n\r", "file", 10); |
| 206 | result->currentTestStarted(&itst); |
| 207 | const char* expected = |
| 208 | "##teamcity[testStarted name='|'|[|]|n|r']\n" |
| 209 | "##teamcity[testIgnored name='|'|[|]|n|r']\n"; |
| 210 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 211 | } |
| 212 | |
| 213 | TEST(TeamCityOutputTest, TestNameEscaped_Fail) |
| 214 | { |
| 215 | tst->setTestName("'[]\n\r"); |
| 216 | TestFailure fail(tst, "failfile", 20, "failure message"); |
| 217 | tcout->printFailure(fail); |
| 218 | const char* expected = |
| 219 | "##teamcity[testFailed name='|'|[|]|n|r' message='TEST failed (file:10): failfile:20' " |
| 220 | "details='failure message']\n"; |
| 221 | STRCMP_EQUAL(expected, mock->getOutput().asCharString()); |
| 222 | } |
| 223 | |
| 224 | /* Todo: |
| 225 | * -Detect when running in TeamCity and switch output to -o teamcity automatically |
| 226 | */ |