美文网首页Android
Android instrumentation test pas

Android instrumentation test pas

作者: JaedenKil | 来源:发表于2017-10-19 14:53 被阅读5次

    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.

    • Test code :
    @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);
        }
    }
    
    • Output :
    I TransferVar: testVar: var1 : first
    I TransferVar: testVar: var2 : second
    
    • Of course we need to add some code in the app\build.gradle :
    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"
    
    • And the command to run the test would be :
    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.

    相关文章

      网友评论

        本文标题:Android instrumentation test pas

        本文链接:https://www.haomeiwen.com/subject/hdbuuxtx.html