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