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

前端 未结 8 1122
孤独总比滥情好
孤独总比滥情好 2021-01-04 11:01

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 star

8条回答
  •  礼貌的吻别
    2021-01-04 11:41

    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;
        }
    }
    

提交回复
热议问题