如何将参数传递给 android junit 测试(参数化测试) [英] how to pass an argument to a android junit test (Parameterized tests)

查看:21
本文介绍了如何将参数传递给 android junit 测试(参数化测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

谢谢

<预><代码>>>>编辑<<<

我使用的是 Robotium,junit 扩展了 ActivityInstrumentationTestCase2请参阅下面非常基本的 junit 测试:

import android.test.ActivityInstrumentationTestCase2;导入 android.util.Log;导入 com.jayway.android.robotium.solo.Solo;公共类 Test_arg 扩展 ActivityInstrumentationTestCase2 {private static final String TARGET_PACKAGE_ID = "com.myapp.test";私有静态最终字符串 LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.myapp";私有静态类launcherActivityClass;私人独奏;静止的 {尝试 {launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);} catch (ClassNotFoundException e) {抛出新的运行时异常(e);}}公共 Test_arg() 抛出 ClassNotFoundException {超级(TARGET_PACKAGE_ID,launcherActivityClass);}@覆盖protected void setUp() 抛出异常 {solo = new Solo(getInstrumentation(), getActivity());}@覆盖公共无效撕裂()抛出异常{尝试 {solo.finishOpenedActivity();solo.finalize();} catch (Throwable e) {e.printStackTrace();}super.tearDown();}public void test_01() 抛出 InterruptedException {Log.i("测试", "测试");}}

和安卓清单

<申请android:icon="@drawable/ic_launcher"android:label="@string/app_name" ><uses-library android:name="android.test.runner"/></应用程序></清单>

解决方案

您可以扩展 InstrumentationTestRunner 并在 onCreate 中获取参数:

public class MyTestRunner extends InstrumentationTestRunner {私有静态最终字符串标记 = 空;私有字符串参数;/* (非Javadoc)* @see android.test.InstrumentationTestRunner#onCreate(android.os.Bundle)*/@覆盖公共无效onCreate(捆绑参数){super.onCreate(参数);如果(参数!= null){mArgument = arguments.getString("myarg");}}公共字符串 getArgument() {返回参数;}}

在您的情况下,将检测添加到 AndroidManifest.xml:

并且在您的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

I am running my android junit tests using command line or eclipse depending if I am in dev mode or in device testing.

A java application has a Main (String[] args) method and it is easy to pass an argument to it (either with eclipse in the Arguments tab in the run configuration section, or by command line)

for an android junit test it is different, there is no Main method and there is nowhere I can set some arguments.

I have read here and there a solution would be to use a properties file. Is that the only way? If you ave a quick and easy example, it would be very appreciated.

Thanks

>>> Edit <<<

I am using Robotium, and the junit extends ActivityInstrumentationTestCase2 See below the very basic junit test:

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");   
    }    
}

and the android manifest

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

解决方案

You can extend InstrumentationTestRunner and get the arguments in 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;
    }

}

Add the instrumentation to AndroidManifest.xml, in your case:

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

and in your Instrumentation (i.e ActivityInstrumentationTestCase2) tests you can do something like this:

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

and get the arguments that you can specify in the command line as

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

这篇关于如何将参数传递给 android junit 测试(参数化测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆