【问题标题】:how to pass an argument to a android junit test (Parameterized tests)如何将参数传递给 android junit 测试(参数化测试)
【发布时间】:2013-02-11 20:22:25
【问题描述】:

我正在使用命令行或 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>

【问题讨论】:

  • 从命令行向测试传递参数是不好的做法。你想通过什么?为什么不创建不同的测试套件或在测试中构建参数。
  • Ray,正如我的问题中提到的,我知道系统属性的方式,我只是在寻找一种更直接的方式(如命令参数)。 _ 列奥尼多斯。我知道为单元测试传递参数并不是最佳实践。就我而言,在服务器端进行了一些更改,它们在其中打开和关闭事物。我的测试是通过命令行自动运行的,所以当服务器发生变化时不必再次编译测试会节省我的时间
  • 如果目标是支持参数化 Android 测试,请考虑使用 Burst

标签: java android eclipse testing junit


【解决方案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"/>

在您的 Instrumentation(即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

【讨论】:

  • 感谢您的回复,我已经使用代码示例编辑了我的初始问题,因为我使用的是 Robotium 并且它扩展了 ActivityInstrumentationTestCase2。我不知道如何在我的情况下使用您的答案
【解决方案2】:

在扩展ActivityInstrumentationTestCase2的Class的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2016-06-16
    • 1970-01-01
    • 2021-05-25
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多