我想在我的Android應用程序中測試我的MainActivity
。所以,我創建了一個測試按鈕功能的第一個測試用例。如果用戶點擊此特定按鈕,則應打開一個新的活動。在使用Espresso進行測試時,在<package>未找到測試
這裏是我的代碼:
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void startNewActivityTest() {
Intents.init();
onView(withId(R.id.main_new)).perform(click());
intended(hasComponent(NewActivity.class.getName()));
Intents.release();
}
@Before
public void setup() {
closeSoftKeyboard();
}
}
不幸的是,我得到以下異常:
junit.framework.AssertionFailedError: No tests found in package.MainActivityTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
這裏是我的搖籃文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "23.0.1"
lintOptions {
abortOnError false
}
defaultConfig {
applicationId "package"
minSdkVersion 14
targetSdkVersion 19
}
signingConfigs {
debug {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
release {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile 'com.google.android.gms:play-services:4.2.+'
androidTestCompile 'com.android.support:support-annotations:19.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Set this dependency if you want to use Hamcrest matching
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}
您可以發佈您的某些環境的詳細信息(如搖籃版本,gradle這個文件,等等)?你有沒有考慮過使用[Robolectric](http://robolectric.org/)? – Schrieveslaach
也許https://github.com/googlesamples/android-testing/blob/master/ui/espresso/IntentsBasicSample/app/build.gradle#L13 – albodelu