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

前端 未结 8 1176
孤独总比滥情好
孤独总比滥情好 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:48

    You can pass an intent extra to your activity indicating it's under test.

    1) In your test, pass "testMode" extra to your activity:

    public void setUp() throws Exception {
        super.setUp();
    
        Intent activityIntent = new Intent();
        activityIntent.putExtra("testMode", true);
        setActivityIntent(activityIntent);
    }
    

    2) In your activity, check for testMode:

    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.getBoolean("testMode")) {
        // disable your database sync
    }
    

提交回复
热议问题