我試圖將我們的應用程序遷移到使用Espresso進行UI測試,但是我無法讓運行Android 4.4(API 19,我們的最低部署目標)的設備找到我的測試。 Android 6.0(API 23)上的測試運行良好。我根據this和this加入JUnit運行和依賴關係app/build.gradle
(I排除,因爲在模塊之間版本衝突的註解):在Android 4.4上運行Espresso測試?
android {
...
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
...
androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2") {
exclude module: 'support-annotations'
}
androidTestCompile("com.android.support.test:runner:0.5") {
exclude module: 'support-annotations'
}
androidTestCompile("com.android.support.test:rules:0.5") {
exclude module: 'support-annotations'
}
}
然後創建所需的目錄結構app/src/androidTest/java/
和封裝com.companyname.appname
和Java類EspressoTest.java
一些模擬測試代碼:
@RunWith(AndroidJUnit4.class)
public class EspressoTest {
@Rule
public ActivityTestRule<TermsOfUse> termsOfUseActivityTestRule = new ActivityTestRule<>(TermsOfUse.class);
@Test
public void iAmAtTouView() {
onView(withId(R.id.terms_of_use_content)).check(matches(isDisplayed()));
}
}
如果我在測試類EspressoTest
單擊鼠標右鍵,選擇「運行‘EspressoTest’」我得到一個錯誤:
$ adb shell am instrument -w -r -e package com.companyname.appname -e debug false com.companyname.appname.qa.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.
另外,如果我在命令行中輸入./gradlew connectedAndroidTest
我得到:
Starting 0 tests on GT-I9305 - 4.4.4
Tests on GT-I9305 - 4.4.4 failed: Instrumentation run failed due to 'Process crashed.'
com.android.builder.testing.ConnectedDevice > No tests found.[GT-I9305 - 4.4.4] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
:app:connectedAuditDebugAndroidTest FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:connectedAuditDebugAndroidTest'.
> There were failing tests. See the report at:
...
所以對我來說似乎是試圖使用Android設備來執行測試任務時,搖籃不承認我的測試類。我怎樣才能解決這個問題?
你能解決問題嗎? –