Skip to content

Commit 90b9515

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Fix instrumentation output on assumption failures at class level.
Currently if a ClassRule or a BeforeClass method throws an assumptionFailure, InstrumentationResultPrinter does not emit testStarted and testFinished events. This change modifies the assumption failure logic so they get treated in the same way as test failures - emitting testStarted and testFinished events on class-level assumption failures. PiperOrigin-RevId: 666398526
1 parent c4525e0 commit 90b9515

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

runner/android_junit_runner/CHANGELOG.md

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

99
* Exceptions during `@AfterClass` were not being reported via `InstrumentationResultPrinter`.
1010
* Exceptions arising in AndroidJUnitRunner.buildRequest are now handled.
11+
* Assumption failures during a ClassRule or BeforeClass are now reported more consistently via `InstrumentationResultPrinter`
1112

1213
**New Features**
1314

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ public InstrumentationResultPrinter() {
113113
}
114114

115115
@Override
116-
public void testRunStarted(Description description) throws Exception {
116+
public void testRunStarted(Description description) {
117117
resultTemplate.putString(Instrumentation.REPORT_KEY_IDENTIFIER, REPORT_VALUE_ID);
118118
resultTemplate.putInt(REPORT_KEY_NUM_TOTAL, description.testCount());
119119
}
120120

121121
/** send a status for the start of a each test, so long tests can be seen as "running" */
122122
@Override
123-
public void testStarted(Description description) throws Exception {
123+
public void testStarted(Description description) {
124124
testNum.incrementAndGet();
125125
this.description = description; // cache Description in case of a crash
126126
String testClass = description.getClassName();
@@ -143,33 +143,45 @@ public void testStarted(Description description) throws Exception {
143143
}
144144

145145
@Override
146-
public void testFinished(Description description) throws Exception {
146+
public void testFinished(Description description) {
147147
if (testResultCode == REPORT_VALUE_RESULT_OK) {
148148
testResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, ".");
149149
}
150150
sendStatus(testResultCode, testResult);
151151
}
152152

153153
@Override
154-
public void testFailure(Failure failure) throws Exception {
154+
public void testFailure(Failure failure) {
155+
handleFailure(
156+
failure,
157+
() -> {
158+
testResultCode = REPORT_VALUE_RESULT_FAILURE;
159+
reportFailure(failure);
160+
});
161+
}
162+
163+
private void handleFailure(Failure failure, Runnable action) {
155164
// getMethodName() == null when an exception is thrown during @BeforeClass or @AfterClass.
156165
// No matching testStart() / testFinish() is emitted, so simulate them here for the sake of
157166
// instrumentation consumers.
158167
boolean shouldCallFinish = failure.getDescription().getMethodName() == null;
159168
if (shouldCallFinish) {
160169
testStarted(failure.getDescription());
161170
}
162-
testResultCode = REPORT_VALUE_RESULT_FAILURE;
163-
reportFailure(failure);
171+
action.run();
164172
if (shouldCallFinish) {
165173
testFinished(failure.getDescription());
166174
}
167175
}
168176

169177
@Override
170178
public void testAssumptionFailure(Failure failure) {
171-
testResultCode = REPORT_VALUE_RESULT_ASSUMPTION_FAILURE;
172-
testResult.putString(REPORT_KEY_STACK, failure.getTrace());
179+
handleFailure(
180+
failure,
181+
() -> {
182+
testResultCode = REPORT_VALUE_RESULT_ASSUMPTION_FAILURE;
183+
testResult.putString(REPORT_KEY_STACK, StackTrimmer.getTrimmedStackTrace(failure));
184+
});
173185
}
174186

175187
private void reportFailure(Failure failure) {
@@ -182,7 +194,7 @@ private void reportFailure(Failure failure) {
182194
}
183195

184196
@Override
185-
public void testIgnored(Description description) throws Exception {
197+
public void testIgnored(Description description) {
186198
testStarted(description);
187199
testResultCode = REPORT_VALUE_RESULT_IGNORED;
188200
testFinished(description);

runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ axt_android_library_test(
4545
"//runner/android_junit_runner",
4646
"//runner/android_junit_runner/javatests/androidx/test/testing/fixtures",
4747
"@androidsdk//:legacy_test-34",
48+
"@maven//:com_google_truth_truth",
4849
"@maven//:junit_junit",
4950
],
5051
)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package androidx.test.internal.runner.listener;
1818

1919
import static androidx.test.internal.runner.listener.InstrumentationResultPrinter.REPORT_KEY_STACK;
20+
import static com.google.common.truth.Truth.assertThat;
2021
import static junit.framework.Assert.assertEquals;
2122
import static junit.framework.Assert.assertTrue;
23+
import static org.junit.Assume.assumeTrue;
2224

2325
import android.os.Bundle;
2426
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -114,4 +116,29 @@ public void verifyBeforeClassExceptionsReported() throws Exception {
114116
"code=-2, name=" + className + "#null"),
115117
intrResultPrinter.resultsLog);
116118
}
119+
120+
public static class AssumptionFailureInClassTest {
121+
@BeforeClass
122+
public static void setUpClass() {
123+
assumeTrue(false);
124+
}
125+
126+
@Test
127+
public void emptyTest() {}
128+
}
129+
130+
@Test
131+
public void verifyBeforeClassAssumptionsReported() throws Exception {
132+
JUnitCore core = new JUnitCore();
133+
var intrResultPrinter = new TestInstrumentationResultPrinter();
134+
core.addListener(intrResultPrinter);
135+
Request testRequest = Request.classes(new Computer(), AssumptionFailureInClassTest.class);
136+
core.run(testRequest);
137+
138+
String className = AssumptionFailureInClassTest.class.getName();
139+
assertThat(intrResultPrinter.resultsLog)
140+
.containsExactly(
141+
"code=1, name=" + className + "#null", "code=-4, name=" + className + "#null")
142+
.inOrder();
143+
}
117144
}

0 commit comments

Comments
 (0)