Skip to content

Commit b609087

Browse files
Add debug logging to RunBefores and RunAfters.
This change adds Log.d statements to the evaluate methods of RunBefores and RunAfters to indicate when the execution of @before and @after methods starts and ends. This allows us to know when setup and teardown sections start and end, and to isolate these in the logcat, to help error understanding. PiperOrigin-RevId: 871896270
1 parent b26cf1b commit b609087

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

runner/android_junit_runner/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
* Make perfetto trace sections for tests more identifiable by prefixing with "test:" and using fully qualified class name. (b/204992764)
1212

13+
* Add logs at the start and end of RunBefore and RunAfters sections to help bug understanding. (b/445754263)
14+
1315
**Breaking Changes**
1416

1517
**API Changes**

runner/android_junit_runner/java/androidx/test/internal/runner/junit4/statement/RunAfters.java

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

1919
package androidx.test.internal.runner.junit4.statement;
2020

21+
import android.util.Log;
2122
import java.util.List;
2223
import java.util.concurrent.CopyOnWriteArrayList;
2324
import org.junit.runners.model.FrameworkMethod;
@@ -32,6 +33,9 @@ public class RunAfters extends UiThreadStatement {
3233

3334
private final List<FrameworkMethod> afters;
3435

36+
/** Tag for logging. Set to TestRunner to match LogRunListener.TAG for easier debugging. */
37+
private static final String TAG = "TestRunner";
38+
3539
/**
3640
* Run all non-overridden {@code @After} methods on this class and superclasses before running
3741
* {@code next}; all After methods are always executed: exceptions thrown by previous steps are
@@ -62,6 +66,7 @@ public void evaluate() throws Throwable {
6266
} catch (Throwable e) {
6367
errors.add(e);
6468
} finally {
69+
Log.d(TAG, "starting @After execution");
6570
for (final FrameworkMethod each : afters) {
6671
if (shouldRunOnUiThread(each)) {
6772
runOnUiThread(
@@ -83,6 +88,8 @@ public void run() {
8388
}
8489
}
8590
}
91+
92+
Log.d(TAG, "finished @After execution");
8693
}
8794
MultipleFailureException.assertEmpty(errors);
8895
}

runner/android_junit_runner/java/androidx/test/internal/runner/junit4/statement/RunBefores.java

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

1919
package androidx.test.internal.runner.junit4.statement;
2020

21+
import android.util.Log;
2122
import java.util.List;
2223
import java.util.concurrent.atomic.AtomicReference;
2324
import org.junit.runners.model.FrameworkMethod;
@@ -31,6 +32,9 @@ public class RunBefores extends UiThreadStatement {
3132

3233
private final List<FrameworkMethod> befores;
3334

35+
/** Tag for logging. Set to TestRunner to match LogRunListener.TAG for easier debugging. */
36+
private static final String TAG = "TestRunner";
37+
3438
/**
3539
* Run all non-overridden {@code @Before} methods on this class and superclasses before running
3640
* {@code next}; if any throws an Exception, stop execution and pass the exception on.
@@ -52,6 +56,7 @@ public RunBefores(
5256

5357
@Override
5458
public void evaluate() throws Throwable {
59+
Log.d(TAG, "starting @Before execution");
5560
final AtomicReference<Throwable> exceptionRef = new AtomicReference<>();
5661
for (final FrameworkMethod before : befores) {
5762
if (shouldRunOnUiThread(before)) {
@@ -77,6 +82,7 @@ public void run() {
7782
}
7883
}
7984

85+
Log.d(TAG, "finished @Before execution");
8086
next.evaluate();
8187
}
8288
}

0 commit comments

Comments
 (0)