【发布时间】: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