8

我正在使用命令行或 eclipse 运行我的 android junit 测试,具体取决于我是处于开发模式还是设备测试。

Java 应用程序有一个 Main (String[] args) 方法,并且很容易将参数传递给它(在运行配置部分的参数选项卡中使用 eclipse,或通过命令行)

对于 android junit 测试它是不同的,没有 Main 方法,也没有我可以设置一些参数的地方。

我在这里读过,有一个解决方案是使用属性文件。这是唯一的方法吗?如果您有一个快速简单的示例,将不胜感激。

谢谢

>>> 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>
4

2 回答 2

14

您可以扩展 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
于 2013-02-11T22:18:04.483 回答
0

在扩展ActivityInstrumentationTestCase2的类的Setup函数中,添加如下代码:

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
于 2014-04-25T14:39:14.447 回答