2015-06-29 94 views
1

當我運行使用Espresso的測試方法時,它會提示我選擇一個模擬器,然後選擇我的現有模擬器,並啓動應用程序。仿真器隨後自動重新啓動我的應用程序,然後顯示測試套件是空的。Android Studio Espresso空測試套件

The error

我的咖啡測試用例位於同一模塊,因爲我想測試活動的androidTest文件夾。我編寫了一個簡單的「isDisplayed()」類型的測試,並右鍵單擊該方法並單擊運行。我已經看了關於堆棧溢出和其他資源的其他問題,但我無法弄清楚是什麼導致了這個問題。 logcat不顯示任何內容,當我在測試方法(如下所示)中放置Log.d(「debug」,「hello hello」)時,沒有任何內容顯示在logcat中,當我嘗試放置System.out時也不顯示任何東西.println(「hello」)的測試方法。看來,雖然我運行該方法,但我的代碼沒有運行!

下面是我的一些build.grade。

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 17 
    buildToolsVersion "21.1.2" 
    defaultConfig { 
     applicationId "x" 
     minSdkVersion 17 
     targetSdkVersion 17 
     versionCode 1 
     versionName "1.0" 

     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 


    packagingOptions { 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/NOTICE.txt' 
     exclude 'LICENSE.txt' 
    } 
} 


configurations.all { 
    resolutionStrategy.force 'com.android.support:support-annotations:22.2.0' 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile 'com.android.support.test:rules:0.3' 
    androidTestCompile 'com.android.support.test:runner:0.3' 
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' 
} 

這裏是我試圖運行的測試用例。

@RunWith(AndroidJUnit4.class) 
public class EspressoTest1 extends ActivityInstrumentationTestCase2<P1>{ 


    private P1 mActivity; 

    public EspressoTest1() { 
     super(P1.class); 
    } 

    public void setUp() throws Exception { 

     super.setUp(); 
     injectInstrumentation(InstrumentationRegistry.getInstrumentation()); 
     mActivity = getActivity(); 

    } 

    public void Happy_test() { 

     onView(withId(R.id.Btn)).check(matches(isDisplayed())); 

    } 
} 

Test Configuration

這是測試運行配置。

回答

2

您的測試缺少@Test註釋。因此,當你的設置方法丟失@Before

+0

謝謝。 – Poptart

+0

這並沒有解決我的問題。事實上,我已經擁有'@ Test'和'@ Before'。 –

+1

@IgorGanapolsky'空測試套件'可以從字面上任何類型的問題 - 從gradle任務失敗到misconfigured @before方法。在這種情況下,這個問題很明顯。這可能與你的情況完全不同。 –

0

也許這會幫助其他人(如伊戈爾Ganapolsky)。

當您使用Espresso庫中的註釋時,必須將testInstrumenatationRunner添加到您的gradle文件中。缺少此行發生時@ Test`和`@ Before`從JUnit的進口`然後清洗和重建項目後,做了同樣的錯誤消息「空測試套件

defaultConfig { 
    applicationId "com.test.app" 
    minSdkVersion 16 
    targetSdkVersion 24 
    versionCode 1 
    versionName "1.0.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
} 
相關問題