如何将一个参数传递给机器人JUnit测试(参数测试) [英] how to pass an argument to a android junit test (Parameterized tests)

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

问题描述

我正在运行使用命令行或日食取决于我的Andr​​oid JUnit测试,如果我在开发模式或设备测试。

Java应用程序有一个主(字符串[] args)方法,很容易将参数传递给它(无论是与Eclipse的运行配置部分,或通过命令行中的参数选项卡)

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

我已经读到这里有一个解决办法是使用属性文件。这是唯一的方法是什么? 如果你AVE一个快速和简单的例子,那将是非常美联社preciated。

感谢

 >>>编辑<<<
 

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

 进口android.test.ActivityInstrumentationTestCase2;
进口android.util.Log;

进口com.jayway.android.robotium.solo.Solo;

公共类Test_arg扩展ActivityInstrumentationTestCase2 {
    私有静态最后弦乐TARGET_PACKAGE_ID =com.myapp.test;
    私有静态最后弦乐LAUNCHER_ACTIVITY_FULL_CLASSNAME =com.myapp;
    私有静态类launcherActivityClass;
    私人独奏独唱;

    静态{
        尝试 {
            launcherActivityClass =的Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        }赶上(ClassNotFoundException异常E){
            抛出新的RuntimeException(E);
        }
    }

    公共Test_arg()抛出ClassNotFoundException的{
        超(TARGET_PACKAGE_ID,launcherActivityClass);

    }

    @覆盖
    保护无效设置()抛出异常{
        独奏=新梭罗(getInstrumentation(),getActivity());
    }

    @覆盖
    公共无效的teardown()抛出异常{
        尝试 {
            solo.finishOpenedActivities();
            solo.finalize();
        }赶上(的Throwable E){
            e.printStackTrace();
        }
        super.tearDown();
    }

    公共无效test_01()抛出InterruptedException的{
        Log.i(TEST,考);
    }
}
 

和Android清单

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.myapp.test
    安卓版code =1
    机器人:VERSIONNAME =1.0>
    <使用-SDK安卓的minSdkVersion =5/>

    <仪器仪表
        机器人:名称=android.test.InstrumentationTestRunner
        机器人:targetPackage =com.slacker.radio机器人:functionalTest =真/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <使用库机器人:名称=android.test.runner/>
    < /用途>
< /舱单>
 

解决方案

您可以扩展InstrumentationTestRunner并获得论点的onCreate:

 公共类MyTestRunner扩展InstrumentationTestRunner {

    私有静态最后字符串变量= NULL;
    私人字符串mArgument;

    / *(非Javadoc中)
     * @see android.test.InstrumentationTestRunner#的onCreate(android.os.Bundle)
     * /
    @覆盖
    公共无效的onCreate(包参数){
        super.onCreate(参数);

        如果(参数!= NULL){
            mArgument = arguments.getString(myarg);
        }
    }

    公共字符串getArgument(){
        返回mArgument;
    }

}
 

添加仪表的Andr​​oidManifest.xml ,你的情况:

 <仪器仪表
        机器人:名称=com.example.my.test.MyTestRunner
        机器人:targetPackage =com.slacker.radio机器人:functionalTest =真/>
 

和您的仪表(即 ActivityInstrumentationTestCase2 )的测试,你可以做这样的事情:

 公共无效testSomething(){
    MyTestRunner myTestRunner =(MyTestRunner)getInstrumentation();
    Log.d(TAG的说法=+ myTestRunner.getArgument());
}
 

和获得,你可以在命令行

指定参数

  $亚行外壳上午仪器-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

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

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