2016-07-11 22 views
16

Utworzono domyślny pusty projekt na Android Studio 2.1.2 z API 24. W projekcie próbki, Google oferuje zamortyzowanego klasa ApplicationTestCase:ApplicationTestCase przestarzałe w poziomie API 24

This class was deprecated in API level 24. Use ActivityTestRule instead. New tests should be written using the Android Testing Support Library.

Próbka:

import android.app.Application; 
import android.test.ApplicationTestCase; 

/** 
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> 
*/ 
public class ApplicationTest extends ApplicationTestCase<Application> { 
    public ApplicationTest() { 
     super(Application.class); 
    } 
} 

Moje pytanie: Dlaczego Android Przypadek Testowy jest teraz przestarzała? Jak zastąpić ApplicationTestCase przez ActivityTestRule?


EDIT:

próbuję z Expresso, ale na API 24 (compileSdkVersion 24) Mam ten błąd:

Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 
Error:Conflict with dependency 'com.android.support:design'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 
Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 
Error:Conflict with dependency 'com.android.support:recyclerview-v7'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 

Gdy próbuję dodać ten lib w moim build.gradle:

// Android JUnit Runner 
androidTestCompile 'com.android.support.test:runner:0.5' 
// JUnit4 Rules 
androidTestCompile 'com.android.support.test:rules:0.5' 
// Espresso core 
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' 
// Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource 
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' 
// Espresso-web for WebView support 
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2' 
// Espresso-idling-resource for synchronization with background jobs 
androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2' 

My conclusion is that for the moment neither Android Test Case nor Expresso works on Android API 24. Is this right?


EDIT: 2016-08-05

naprawić poprzedni błąd na Expresso tak:

def espressoVersion = '2.2.2' 
def testRunnerVersion = '0.5' 
androidTestCompile "com.android.support.test:rules:${testRunnerVersion}" 
androidTestCompile "com.android.support.test.espresso:espresso-core:${espressoVersion}" 
configurations.androidTestCompile.dependencies.each { androidTestCompileDependency -> 
    androidTestCompileDependency.exclude group: 'com.android.support' 
} 
+0

googlowania "Android espresso" – hanbumpark

+0

@HanbumBak nie mogę znaleźć tutorial Api 24 –

+0

Odnośnie błędu konfliktu zależność patrz: [Ta odpowiedź] (http://stackoverflow.com/questions/38181587/android-version-24-0-0-and-espresso-2-0-gradle-exception/38182270#38182270) – thaussma

Odpowiedz

11

Nowy androidTest przykład, że beta wersja Androida Studio 2.2 jest generowana w następujący sposób:

@RunWith(AndroidJUnit4.class) 
public class ExampleInstrumentedTest { 
    @Test 
    public void useAppContext() throws Exception { 
     // Context of the app under test. 
     Context appContext = InstrumentationRegistry.getTargetContext(); 

     assertEquals("org.mypackage", appContext.getPackageName()); 
    } 
} 

Podobnie jak ostrzeżenie deprecation sugeruje, że nowe testy oprzyrządowania powinien używać InstrumentationRegistry zamiast rozciągający się od AndroidTestCase. Uruchom je pod numerem AndroidJUnit4.

Odpowiedni dependencies sekcja w build.gradle wygląda następująco:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
+0

OK, ale jak zastąpić 'ApplicationTestCase' ? Zajmuje się konfiguracją i rozłączaniem aplikacji między poszczególnymi przypadkami testowymi. – TWiStErRob

+0

@TWiStErRob Po prostu dodaj metody do sprawy testowej i opatrz je adnotacjami za pomocą '@ Before' i' @ After' od JUnit, i wykonaj tam swoje ustawienia/zaniechania. – friederbluemle

+3

Wiem, że moje pytanie było bardziej ukierunkowane na: jaka jest zawartość tych metod, aby ** prawidłowo ** utworzyć nową instancję "Application" za każdym razem bez przecieków, konfliktu i posiadania instancji utworzonej w tym teście dostępnym przez kontekst. getApplicationContext() 'gdzie jest potrzebne wewnątrz aplikacji? – TWiStErRob

Powiązane problemy