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

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

    If you're using ActivityUnitTestCase, you could set a custom Application object with setApplication, and have a flag in there to switch database sync on or off? There's an example of using a custom Application object on my blog:

    http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-3/

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题