如何将参数传递给AndroidTestCase? [英] How to pass an argument to an AndroidTestCase?

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

问题描述

我已经实现了一个Instrumentation和一个AndroidTestCase.

I've implemented an Instrumentation and an AndroidTestCase.

对于我的测试,我需要连接到外部WIFI设备.我希望测试人员能够指定要使用的SSID.

For my tests I need to connect to an external WIFI device. I want the testers to be able to specify an SSID for the test to use.

让命令行(adb shell am instrument ...)运行测试不是问题,但是如何将SSID添加到命令行并将其提取到代码中呢?

Giving the the command line (adb shell am instrument ...) to run the test is not a problem, but how can I add the SSID to the command line and extract it in the code?

推荐答案

找到了解决方案.

我使测试运行程序从InstrumentationTestRunner继承,并在onCreate()中获取了额外的数据:

I made my test-runner inherit from InstrumentationTestRunner and took the extra data in onCreate():

public class MyTestRunner extends InstrumentationTestRunner {

    public static String BAR;

    public void onCreate(Bundle arguments) {

        if (null != arguments) {    
            BAR = (String) arguments.get("foo"));
        }    
        super.onCreate(arguments);
    }
}

我已添加到Android.mk:

I added to Android.mk:

LOCAL_JAVA_LIBRARIES := android.test.runner

并转到AndroidManifest.xml:

And to AndroidManifest.xml:

<instrumentation 
    android:name="com.example.MyTestRunner"
    android:targetPackage="com.example" />

使用以下命令行运行它:

Ran it using this command line:

adb shell am instrument -w -e foo the_value_of_bar com.example/com.example.MyTestRunner

我能够从命令行获取'foo'参数,并在我的AndroidTestCase中使用BAR.

I was able to get the 'foo' parameter from the command line and use BAR in my AndroidTestCase.

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

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