我尝试使用Espresso测试框架来测试我的应用。
ENV:
Android Studio RC1(来自金丝雀) 最新的Android Gradle插件,0.14 +
我按照此处所述添加了https://code.google.com/p/android-test-kit/wiki/Espresso#Getting_Started)
dependencies {
//espresso - test framework
androidTestCompile files('libs/espresso-1.1.jar',
'libs/testrunner-1.1.jar',
'libs/testrunner-runtime-1.1.jar')
androidTestCompile 'com.google.guava:guava:14.0.1',
'com.squareup.dagger:dagger:1.1.0',
'org.hamcrest:hamcrest-core:1.1',
'org.hamcrest:hamcrest-integration:1.1',
'org.hamcrest:hamcrest-library:1.1'
}
packagingOptions {
exclude 'LICENSE.txt'
}
defaultConfig {
//espresso - test framework
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
我也创建了简单的测试:
public class StartActivityTest extends ActivityInstrumentationTestCase2<StartActivity> {
public StartActivityTest(Class<StartActivity> activityClass) {
super(activityClass);
}
@Override
public void setUp() throws Exception {
super.setUp();
// Espresso will not launch our activity for us, we must launch it via getActivity().
getActivity();
}
public void testStartVisibility() {
onView(withId(R.id.app_title))
.check(matches(isDisplayed()));
onView(withId(R.id.app_thumb))
.check(matches(isDisplayed()));
onView(withId(R.id.label))
.check(matches(isDisplayed())
);
onView(withId(R.id.label)).check(matches(withText("Hello Espresso!")));
}
}
但是我收到了一个错误:
com.android.builder.testing.ConnectedDevice > hasTests[Nexus 5 - 5.0] FAILED
No tests found.
:app:connectedAndroidTest FAILED
FAILURE: Build failed with an exception.
答案 0 :(得分:1)
好的,我明白了。
我必须使用默认构造函数,如下所示:
public StartActivityTest() {
super(StartActivity.class);
}
解决了这个问题。