Skip to content

Commit 6ef437c

Browse files
committed
test(android, jacoco): dump coverage file after running Detox tests
This should not be necessary but in practice relying on the InstrumentationRunner to dump coverage during it's finish lifecycle transition is unreliable because of a UIAutomator bug that crashes the process before coverage is dumped Rather than rely on that, we dump it ourselves manually
1 parent 42961f2 commit 6ef437c

File tree

1 file changed

+25
-0
lines changed
  • tests/android/app/src/androidTest/java/com/invertase/testing

1 file changed

+25
-0
lines changed

tests/android/app/src/androidTest/java/com/invertase/testing/DetoxTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.invertase.testing;
22

3+
import java.io.File;
4+
import java.lang.reflect.Method;
5+
6+
import androidx.test.platform.app.InstrumentationRegistry;
37
import androidx.test.ext.junit.runners.AndroidJUnit4;
48
import androidx.test.filters.LargeTest;
59
import androidx.test.rule.ActivityTestRule;
@@ -23,5 +27,26 @@ public class DetoxTest {
2327
@Test
2428
public void runDetoxTests() {
2529
Detox.runTests(mActivityRule);
30+
dumpCoverageData();
31+
}
32+
33+
// If you send '-e coverage' as part of the instrumentation command line, it should dump coverage as the Instrumentation finishes.
34+
// However, there is a long-standing UIAutomator bug that crashes the process during Instrumentation.finish, before dumping coverage.
35+
// It is trivial to dump it ourselves though, using the same mechanism the AndroidJUnit4Runner woud use
36+
// Code reference: https://android.googlesource.com/platform/frameworks/testing/+/refs/heads/master/support/src/android/support/test/internal/runner/listener/CoverageListener.java#68
37+
// UIAutomator issue: https://github.com/android/testing-samples/issues/89
38+
private void dumpCoverageData() {
39+
String coverageFilePath = InstrumentationRegistry.getInstrumentation().getTargetContext().getFilesDir().getAbsolutePath() +
40+
File.separator + "coverage.ec";
41+
File coverageFile = new File(coverageFilePath);
42+
try {
43+
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
44+
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",
45+
coverageFile.getClass(), boolean.class, boolean.class);
46+
dumpCoverageMethod.invoke(null, coverageFile, false, false);
47+
System.out.println("Dumped code coverage data to " + coverageFilePath);
48+
} catch (Exception e) {
49+
System.err.println("Unable to dump coverage report: " + e);
50+
}
2651
}
2752
}

0 commit comments

Comments
 (0)