|
18 | 18 |
|
19 | 19 | import static androidx.test.internal.runner.listener.InstrumentationResultPrinter.REPORT_KEY_STACK; |
20 | 20 | import static com.google.common.truth.Truth.assertThat; |
| 21 | +import static com.google.common.truth.Truth.assertWithMessage; |
21 | 22 | import static junit.framework.Assert.assertEquals; |
22 | 23 | import static junit.framework.Assert.assertTrue; |
23 | 24 | import static org.junit.Assume.assumeTrue; |
|
30 | 31 | import junit.framework.Assert; |
31 | 32 | import org.junit.AfterClass; |
32 | 33 | import org.junit.BeforeClass; |
| 34 | +import org.junit.Ignore; |
33 | 35 | import org.junit.Test; |
34 | 36 | import org.junit.runner.Computer; |
35 | 37 | import org.junit.runner.Description; |
@@ -141,4 +143,162 @@ public void verifyBeforeClassAssumptionsReported() throws Exception { |
141 | 143 | "code=1, name=" + className + "#null", "code=-4, name=" + className + "#null") |
142 | 144 | .inOrder(); |
143 | 145 | } |
| 146 | + |
| 147 | + public static final class IgnoredMethodTest { |
| 148 | + @Ignore("Flaky test") |
| 149 | + @Test |
| 150 | + public void ignoredTest() {} |
| 151 | + } |
| 152 | + |
| 153 | + @Ignore("Class ignore reason") |
| 154 | + public static final class IgnoredClassTest { |
| 155 | + @Test |
| 156 | + public void ignoredTest() {} |
| 157 | + } |
| 158 | + |
| 159 | + public static final class IgnoredNoReasonMethodTest { |
| 160 | + @Ignore |
| 161 | + @Test |
| 162 | + public void ignoredTest() {} |
| 163 | + } |
| 164 | + |
| 165 | + @Ignore |
| 166 | + public static final class IgnoredNoReasonClassTest { |
| 167 | + @Test |
| 168 | + public void ignoredTest() {} |
| 169 | + } |
| 170 | + |
| 171 | + @Ignore("Class ignore reason") |
| 172 | + public static final class IgnoredMethodInIgnoredClassTest { |
| 173 | + @Ignore("Method ignore reason") |
| 174 | + @Test |
| 175 | + public void ignoredWithMethodReason() {} |
| 176 | + |
| 177 | + @Ignore |
| 178 | + @Test |
| 179 | + public void ignoredWithoutMethodReason() {} |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testIgnored_populatesStackWithIgnoreReason() throws Exception { |
| 184 | + List<String> stacks = new ArrayList<>(); |
| 185 | + InstrumentationResultPrinter intrResultPrinter = |
| 186 | + new InstrumentationResultPrinter() { |
| 187 | + @Override |
| 188 | + public void sendStatus(int code, Bundle bundle) { |
| 189 | + if (code == REPORT_VALUE_RESULT_IGNORED) { |
| 190 | + stacks.add(bundle.getString(REPORT_KEY_STACK)); |
| 191 | + } |
| 192 | + } |
| 193 | + }; |
| 194 | + JUnitCore core = new JUnitCore(); |
| 195 | + core.addListener(intrResultPrinter); |
| 196 | + |
| 197 | + core.run(Request.classes(new Computer(), IgnoredMethodTest.class)); |
| 198 | + |
| 199 | + assertWithMessage("ignored test stacks").that(stacks).containsExactly("Flaky test"); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + public void testIgnored_classLevelIgnore_populatesStackWithIgnoreReason() throws Exception { |
| 204 | + List<String> stacks = new ArrayList<>(); |
| 205 | + InstrumentationResultPrinter intrResultPrinter = |
| 206 | + new InstrumentationResultPrinter() { |
| 207 | + @Override |
| 208 | + public void sendStatus(int code, Bundle bundle) { |
| 209 | + if (code == REPORT_VALUE_RESULT_IGNORED) { |
| 210 | + stacks.add(bundle.getString(REPORT_KEY_STACK)); |
| 211 | + } |
| 212 | + } |
| 213 | + }; |
| 214 | + JUnitCore core = new JUnitCore(); |
| 215 | + core.addListener(intrResultPrinter); |
| 216 | + |
| 217 | + core.run(Request.classes(new Computer(), IgnoredClassTest.class)); |
| 218 | + |
| 219 | + assertWithMessage("ignored test stacks").that(stacks).containsExactly("Class ignore reason"); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void testIgnored_withoutReason_doesNotPopulateStack() throws Exception { |
| 224 | + List<Bundle> ignoredBundles = new ArrayList<>(); |
| 225 | + InstrumentationResultPrinter intrResultPrinter = |
| 226 | + new InstrumentationResultPrinter() { |
| 227 | + @Override |
| 228 | + public void sendStatus(int code, Bundle bundle) { |
| 229 | + if (code == REPORT_VALUE_RESULT_IGNORED) { |
| 230 | + ignoredBundles.add(new Bundle(bundle)); |
| 231 | + } |
| 232 | + } |
| 233 | + }; |
| 234 | + JUnitCore core = new JUnitCore(); |
| 235 | + core.addListener(intrResultPrinter); |
| 236 | + |
| 237 | + core.run(Request.classes(new Computer(), IgnoredNoReasonMethodTest.class)); |
| 238 | + |
| 239 | + assertWithMessage("ignored test bundles").that(ignoredBundles).hasSize(1); |
| 240 | + assertWithMessage("bundle keys for ignored method without reason") |
| 241 | + .that(ignoredBundles.get(0).keySet()) |
| 242 | + .doesNotContain(REPORT_KEY_STACK); |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + public void testIgnored_classLevelIgnoreWithoutReason_doesNotPopulateStack() throws Exception { |
| 247 | + List<Bundle> ignoredBundles = new ArrayList<>(); |
| 248 | + InstrumentationResultPrinter intrResultPrinter = |
| 249 | + new InstrumentationResultPrinter() { |
| 250 | + @Override |
| 251 | + public void sendStatus(int code, Bundle bundle) { |
| 252 | + if (code == REPORT_VALUE_RESULT_IGNORED) { |
| 253 | + ignoredBundles.add(new Bundle(bundle)); |
| 254 | + } |
| 255 | + } |
| 256 | + }; |
| 257 | + JUnitCore core = new JUnitCore(); |
| 258 | + core.addListener(intrResultPrinter); |
| 259 | + |
| 260 | + core.run(Request.classes(new Computer(), IgnoredNoReasonClassTest.class)); |
| 261 | + |
| 262 | + assertWithMessage("ignored test bundles").that(ignoredBundles).hasSize(1); |
| 263 | + assertWithMessage("bundle keys for ignored class without reason") |
| 264 | + .that(ignoredBundles.get(0).keySet()) |
| 265 | + .doesNotContain(REPORT_KEY_STACK); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void testIgnored_methodAndClassIgnore_prefersMethodReasonAndFallsBackToClassReason() |
| 270 | + throws Exception { |
| 271 | + List<String> stacks = new ArrayList<>(); |
| 272 | + InstrumentationResultPrinter intrResultPrinter = |
| 273 | + new InstrumentationResultPrinter() { |
| 274 | + @Override |
| 275 | + public void sendStatus(int code, Bundle bundle) { |
| 276 | + if (code == REPORT_VALUE_RESULT_IGNORED) { |
| 277 | + stacks.add(bundle.getString(REPORT_KEY_STACK)); |
| 278 | + } |
| 279 | + } |
| 280 | + }; |
| 281 | + Description methodWithReason = |
| 282 | + Description.createTestDescription( |
| 283 | + IgnoredMethodInIgnoredClassTest.class, |
| 284 | + "ignoredWithMethodReason", |
| 285 | + IgnoredMethodInIgnoredClassTest.class |
| 286 | + .getMethod("ignoredWithMethodReason") |
| 287 | + .getAnnotations()); |
| 288 | + Description methodWithoutReason = |
| 289 | + Description.createTestDescription( |
| 290 | + IgnoredMethodInIgnoredClassTest.class, |
| 291 | + "ignoredWithoutMethodReason", |
| 292 | + IgnoredMethodInIgnoredClassTest.class |
| 293 | + .getMethod("ignoredWithoutMethodReason") |
| 294 | + .getAnnotations()); |
| 295 | + |
| 296 | + intrResultPrinter.testIgnored(methodWithReason); |
| 297 | + intrResultPrinter.testIgnored(methodWithoutReason); |
| 298 | + |
| 299 | + assertWithMessage("ignored test stacks") |
| 300 | + .that(stacks) |
| 301 | + .containsExactly("Method ignore reason", "Class ignore reason") |
| 302 | + .inOrder(); |
| 303 | + } |
144 | 304 | } |
0 commit comments