【发布时间】:2015-01-15 03:44:41
【问题描述】:
我需要对没有源代码的 Android 应用程序执行一些自动测试工作。我发现 robotium 和 espresso 都可以完成这项工作,我决定使用 espresso,因为它支持 Google。
我想用相同的签名对目标 apk 和 espresso 测试 apk 进行签名,目标 apk 与 this sample 相同。
当我开始编写 espresso 测试 apk 时,我做了以下工作。
Module:app: 中的 build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "tk.deedog.i01test"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}
我的I01Test.java的源代码:
package tk.deedog.i01test;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
public class I01Test extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "tk.tubabe.instrustmen101.Instruments101";
private static Class<?> launcherActivityClass;
Activity i101Acitvity;
static {
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public I01Test() {
super(launcherActivityClass);
}
@Override
protected void setUp() throws Exception {
super.setUp();
i101Acitvity = getActivity();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testButtonClick() {
onView(withText("Send")).perform(click());
}
}
当我尝试运行这个程序时,Android Studio 告诉我Error:(6, 44) error: package android.support.test.espresso does not exist。
【问题讨论】:
-
您发布的所有内容看起来都不错。你的 I01Test.java 是在 src/androidTest/ 还是 src/main 下?应该是前者。
-
@Baqueta 我只是将 I01Test.java 放在 src/androidTest/ 中,然后运行 TestModule,但出现错误
Caused by: java.lang.ClassNotFoundException: Didn't find class "tk.tubabe.instrustmen101.Instruments101"。测试 apk 和目标 apk 是两个不同的 apk,具有相同的签名。 -
“tk.tubabe.instrustmen101.Instruments101”应该是“tk.tubabe.instrustment101.Instruments101”吗?你可以像平常一样引用这个类,你不需要使用反射来找到它。
-
试图做同样的事情但没有成功,但我之前看到过这个 ClassNotFoundException。在我的情况下,我假设 applicationId "tk.deedog.i01test" 将是我的测试 apk 的包,但是 gradle android 插件将它作为我的应用程序的包(目标 apk),并且测试 apk 的包是 "tk .deedog.i01test.test”。而且因为“tk.deedog.i01test”实际上包含测试代码,所以启动活动不存在。我有一个关于没有源代码的 espresso 测试的问题 stackoverflow.com/questions/28020893/… ,还没有回复
-
这个问题你解决了吗?
标签: android android-testing android-espresso