2017-05-11 63 views
0

我試圖將我們的應用程序遷移到使用Espresso進行UI測試,但是我無法讓運行Android 4.4(API 19,我們的最低部署目標)的設備找到我的測試。 Android 6.0(API 23)上的測試運行良好。我根據thisthis加入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設備來執行測試任務時,搖籃不承認我的測試類。我怎樣才能解決這個問題?

+0

你能解決問題嗎? –

回答

0

首先檢查什麼任務是avaliable對gradle這個由運行:

gradle <app-module>:tasks 

gradle --gui 

看看你是否正在運行的任務在任務列表中。您還試圖運行哪臺機器:MacOS或Windows?如果在運行測試之前至少不要同步你的項目從android studio始終是個好主意,那麼總是用'sudo'運行gradle

+0

該任務位於列表中,並且您可以從我發佈的控制檯輸出中看到它由Gradle運行。否則,我會得到一個異常說沒有找到任務。我在Mac OS上,除非接口明確要求,否則我從來不會使用sudo運行任何操作。 – Julsteri

+0

殺死你的Android Studio,然後重新打開它並嘗試運行它。試一試 – testsingh

+0

我編輯了原帖,以闡明在Android 4.4上測試失敗,而不是在6.0上。 – Julsteri

相關問題