wesley tanaka

Detect if Android application is running inside a unit test

‹ Ryan saying "apple" | View HTTP requests made by Internet Explorer ›
()

I wanted to disable ACRA in an Android application while it was being unit tested by the android.test framework, so that crashes in worker threads would get detected by the test runner as failures, instead of getting reported to ACRA.

I found isRunningInTestHarness(), but didn't use it because it was only added in API level 11. It uses a non-public API android.os.SystemProperties.getBoolean("ro.test_harness", false), so there also didn't appear to be a robust way to emulate it in older android versions.

I couldn't find a way to get the current android.app.Instrumentation class from manifest.

I ended up checking the existence of one of the unit test classes, and enabling ACRA only if it didn't exist:

try
{
   Class.forName("com.wtanaka.example.test.UnitTestClass");
}
catch (ClassNotFoundException e)
{
   ACRA.init(this);
}
Syndicate content
by Wesley Tanaka