Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to find out if an Android application runs as part of an instrumentation test

Is there a runtime check for an application to find out if it runs as part of an instrumentation test?

Background: Our application performs a database sync when starting. But that should happen only when started regularly. It especially interferes with the instrumentation tests testing the db sync. Not surprisingly.

And with all the other tests it's just a waste of CPU cycles.

like image 275
Martin Avatar asked Dec 14 '25 16:12

Martin


2 Answers

A much simpler solution is check for a class that would only be present in a test classpath, works with JUnit 4 (unlike the solution using ActivityUnitTestCase) and doesn't require to send custom intents to your Activities / Services (which might not even be possible in some cases)

private boolean isTesting() {
    try {
        Class.forName("com.company.SomeTestClass");
        return true;
    } catch (ClassNotFoundException e) {
        return false;
    }
}
like image 105
Yannick Menager Avatar answered Dec 16 '25 07:12

Yannick Menager


Since API Level 11, the ActivityManager.isRunningInTestHarness() method is available. This might do what you want.

like image 45
Edward Dale Avatar answered Dec 16 '25 05:12

Edward Dale