Skip to content

Commit ce06a1a

Browse files
Report @ignore reason in InstrumentationResultPrinter#testIgnored
When a test is ignored via `@Ignore("reason")`, JUnit provides the reason in `Description.getAnnotation(Ignore.class).value()`, but `InstrumentationResultPrinter#testIgnored` previously did not populate `REPORT_KEY_STACK` in the status bundle. Populate `REPORT_KEY_STACK` with the non-empty `@Ignore` value in `testIgnored(Description)` so instrumentation listeners (such as TradeFed and Atest) can display the ignore reason. PiperOrigin-RevId: 1013244028
1 parent ec03f84 commit ce06a1a

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

‎runner/android_junit_runner/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
**Bug Fixes**
88

99
* Inspect class-level annotations in `TestRequestBuilder.CustomFilters` so `@CustomFilter` annotations applied at the test class level are evaluated. (b/573501234)
10+
* Report `@Ignore` reason in `InstrumentationResultPrinter#testIgnored`. (b/559750770)
1011

1112
**New Features**
1213

‎runner/android_junit_runner/java/androidx/test/internal/runner/listener/InstrumentationResultPrinter.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import androidx.test.services.events.internal.StackTrimmer;
2323
import java.io.PrintStream;
2424
import java.util.concurrent.atomic.AtomicInteger;
25+
import org.junit.Ignore;
2526
import org.junit.internal.TextListener;
2627
import org.junit.runner.Description;
2728
import org.junit.runner.Result;
@@ -197,6 +198,13 @@ private void reportFailure(Failure failure) {
197198
public void testIgnored(Description description) {
198199
testStarted(description);
199200
testResultCode = REPORT_VALUE_RESULT_IGNORED;
201+
Ignore ignore = description.getAnnotation(Ignore.class);
202+
if ((ignore == null || ignore.value().isEmpty()) && description.getTestClass() != null) {
203+
ignore = description.getTestClass().getAnnotation(Ignore.class);
204+
}
205+
if (ignore != null && !ignore.value().isEmpty()) {
206+
testResult.putString(REPORT_KEY_STACK, ignore.value());
207+
}
200208
testFinished(description);
201209
}
202210

‎runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/InstrumentationResultPrinterTest.java‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static androidx.test.internal.runner.listener.InstrumentationResultPrinter.REPORT_KEY_STACK;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static com.google.common.truth.Truth.assertWithMessage;
2122
import static junit.framework.Assert.assertEquals;
2223
import static junit.framework.Assert.assertTrue;
2324
import static org.junit.Assume.assumeTrue;
@@ -30,6 +31,7 @@
3031
import junit.framework.Assert;
3132
import org.junit.AfterClass;
3233
import org.junit.BeforeClass;
34+
import org.junit.Ignore;
3335
import org.junit.Test;
3436
import org.junit.runner.Computer;
3537
import org.junit.runner.Description;
@@ -141,4 +143,162 @@ public void verifyBeforeClassAssumptionsReported() throws Exception {
141143
"code=1, name=" + className + "#null", "code=-4, name=" + className + "#null")
142144
.inOrder();
143145
}
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+
}
144304
}

0 commit comments

Comments
 (0)