2015-12-19 16 views
9

Voglio testare il mio MainActivity nella mia applicazione Android. Così, ho creato un primo test che verifica la funzionalità di un pulsante. Se l'utente fa clic su questo particolare pulsante, dovrebbe aprirsi una nuova attività.Nessun test trovato in <package> durante il test con Espresso

Ecco il mio codice:

@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(); 
    } 

} 

Purtroppo, ottengo la seguente eccezione:

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) 

Ecco il mio file Gradle:

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' 
} 
+0

Puoi pubblicare qualche informazione in più del vostro ambiente (come la versione Gradle, file di Gradle, ecc)? Hai preso in considerazione l'utilizzo di [Robolectric] (http://robolectric.org/)? – Schrieveslaach

+0

forse https://github.com/googlesamples/android-testing/blob/master/ui/espresso/IntentsBasicSample/app/build.gradle#L13 – albodelu

risposta

21

Set the instrumentation runner

aggiungerà allo stesso build.gradle presentare la seguente riga nel android.defaultConfig: testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

apply plugin: 'com.android.application' 

android { 
    ... 

    defaultConfig { 
     ... 

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

dependencies { 
    // App's dependencies, including test 
    compile 'com.android.support:support-annotations:23.0.1' 

    ... 
} 

ho mai usato Espresso Intenti, ma forse è necessario this come here:

Uso IntentsTestRule invece di ActivityTestRule quando si utilizza Espresso-Intenti. IntentsTestRule semplifica l'utilizzo delle API Espresso- nei test funzionali dell'interfaccia utente. Questa classe è un'estensione di ActivityTestRule, che inizializza Espresso-Intents prima di ogni prova annotata con @Test e rilascia Espresso-Intents dopo ogni esecuzione di test. L'attività verrà terminata dopo ogni prova e questa regola può essere utilizzata allo stesso modo di ActivityTestRule.

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

mancavano

Problemi correlati