如何将参数传递到机器人JUnit测试(参数化测试)(how to pass an argument

2019-07-19 17:30发布

我正在使用取决于如果我在开发模式或设备测试命令行或月食我的Android JUnit测试。

Java应用程序具有主(字符串[] args)方法,很容易将参数传递给它(具有在运行配置部Arguments标签蚀,或通过命令行)

对于一个Android JUnit测试它是不同的,不存在主要方法有无处我可以设置一些参数。

我看过这里有一个解决办法是使用属性文件。 这是唯一的办法? 如果你AVE一个快速和简单的例子,这将是非常赞赏。

谢谢

>>> Edit <<<

我使用Robotium,和JUnit延伸ActivityInstrumentationTestCase2请参见下面的非常基本的JUnit测试:

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.jayway.android.robotium.solo.Solo;

public class Test_arg extends ActivityInstrumentationTestCase2 {    
    private static final String TARGET_PACKAGE_ID                = "com.myapp.test";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.myapp";
    private static Class        launcherActivityClass;
    private Solo solo;

    static {
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public Test_arg() throws ClassNotFoundException {
        super(TARGET_PACKAGE_ID, launcherActivityClass);

    }

    @Override
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    @Override
    public void tearDown() throws Exception {
        try {
            solo.finishOpenedActivities();
            solo.finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        super.tearDown();
    }

    public void test_01() throws InterruptedException { 
        Log.i("TEST", "test");   
    }    
}

和Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="5" />

    <instrumentation        
        android:name="android.test.InstrumentationTestRunner"                      
        android:targetPackage="com.slacker.radio" android:functionalTest="true"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>
</manifest>

Answer 1:

您可以扩展InstrumentationTestRunner并获得的onCreate的参数:

public class MyTestRunner extends InstrumentationTestRunner {

    private static final String TAG = null;
    private String mArgument;

    /* (non-Javadoc)
     * @see android.test.InstrumentationTestRunner#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle arguments) {
        super.onCreate(arguments);

        if (arguments != null) {
            mArgument = arguments.getString("myarg");
        }
    }

    public String getArgument() {
        return mArgument;
    }

}

仪表加入AndroidManifest.xml ,你的情况:

<instrumentation        
        android:name="com.example.my.test.MyTestRunner"                      
        android:targetPackage="com.slacker.radio" android:functionalTest="true"/>

并在仪表 (即ActivityInstrumentationTestCase2 )测试,你可以这样做:

public void testSomething() {
    MyTestRunner myTestRunner = (MyTestRunner)getInstrumentation();
    Log.d(TAG, "argument=" + myTestRunner.getArgument());
}

并得到你可以在命令行中指定参数

$ adb shell am instrument -e myarg MYARG com.example.my.test/.MyTestRunner -w


Answer 2:

在扩展ActivityInstrumentationTestCase2类的设置功能,添加以下代码:

MyTestRunner myTestRunner = (MyTestRunner)getInstrumentation();
String argument = myTestRunner.getArgument();
solo = new Solo(myTestRunner, getActivity()); 

目录结构应该是:

src
  package.x.y.z.test
      MainTest.java
      CustomInstrumentationRunner.java

然后在AndroidManifest.xml中,设置

<manifest package="package.x.y.z" ...

<instrumentation
        android:name="package.x.y.z.test.CustomInstrumentationRunner"

调用上述包与所述命令行:

adb shell am instrumentation -w package.x.y.z/package.x.y.z.test.CustomInstrumentationRunner


文章来源: how to pass an argument to a android junit test (Parameterized tests)