Android

Android instrumentation test pas

2017-10-19  本文已影响5人  JaedenKil

Normally, when we want to run some tests on real android device, we can put our tests in app\src\androidTest. We can also pass in parameter values.

@RunWith(AndroidJUnit4.class)
public class TransferVar {

    String var1;
    String var2;

    private static final String TAG = TransferVar.class.getSimpleName();

    @Test
    public void testVar() {
        Bundle testBundle = InstrumentationRegistry.getArguments();

        var1 = testBundle.getString("var1");
        var2 = testBundle.getString("var2");
        Log.i(TAG, "testVar: var1 : " + var1);
        Log.i(TAG, "testVar: var2 : " + var2);
    }
}
I TransferVar: testVar: var1 : first
I TransferVar: testVar: var2 : second
android {
    compileSdkVersion XX
    buildToolsVersion "XX"
    defaultConfig {
        applicationId "myPackageName"
        minSdkVersion XX
        targetSdkVersion XX
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
...
}

It need to be declared :

 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
am instrument -w -r -e debug false -e var1 first -e var2 second -e class myPackageName.myClassName myPackageName.test/android.support.test.runner.AndroidJUnitRunner
But here is the TRICK :

You MUST put the -e var1 first -e var2 second before -e class XX, otherwise you won't pass in anything.

上一篇下一篇

猜你喜欢

热点阅读