AndroidX Test include un insieme di regole JUnit da utilizzare con AndroidJUnitRunner. Le regole JUnit offrono maggiore flessibilità e riducono il codice boilerplate richiesto nei test. Ad esempio, possono essere usati per iniziare un'attività specifica.
RegoleScenarioAttività
Questa regola consente di eseguire test funzionali di una singola attività. La regola avvia
l'attività scelta prima di ogni test annotato con @Test
, nonché prima
di qualsiasi metodo annotato con @Before
. La regola termina l'attività al termine del test e tutti i metodi annotati con @After
. Per accedere all'attività specificata nella logica di test, fornisci un callback eseguibile a ActivityScenarioRule.getScenario().onActivity()
.
Il seguente snippet di codice mostra come incorporare ActivityScenarioRule
nella logica di test:
Kotlin
@RunWith(AndroidJUnit4::class.java) @LargeTest class MyClassTest { @get:Rule val activityRule = ActivityScenarioRule(MyClass::class.java) @Test fun myClassMethod_ReturnsTrue() { activityRule.scenario.onActivity { … } // Optionally, access the activity. } }
Java
public class MyClassTest { @Rule public ActivityScenarioRule<MyClass> activityRule = new ActivityScenarioRule(MyClass.class); @Test public void myClassMethod_ReturnsTrue() { ... } }
RegoleTest
Questa regola fornisce un meccanismo semplificato per avviare il servizio prima dei test e arrestarlo prima e dopo. Puoi avviare o associare il servizio
con uno dei metodi helper. Si arresta o si svincola automaticamente al termine del test e al termine di tutti i metodi annotati con @After
.
Kotlin
@RunWith(AndroidJUnit4::class.java) @MediumTest class MyServiceTest { @get:Rule val serviceRule = ServiceTestRule() @Test fun testWithStartedService() { serviceRule.startService( Intent(ApplicationProvider.getApplicationContext<Context>(), MyService::class.java)) // Add your test code here. } @Test fun testWithBoundService() { val binder = serviceRule.bindService( Intent(ApplicationProvider.getApplicationContext(), MyService::class.java)) val service = (binder as MyService.LocalBinder).service assertThat(service.doSomethingToReturnTrue()).isTrue() } }
Java
@RunWith(AndroidJUnit4.class) @MediumTest public class MyServiceTest { @Rule public final ServiceTestRule serviceRule = new ServiceTestRule(); @Test public void testWithStartedService() { serviceRule.startService( new Intent(ApplicationProvider.getApplicationContext(), MyService.class)); // Add your test code here. } @Test public void testWithBoundService() { IBinder binder = serviceRule.bindService( new Intent(ApplicationProvider.getApplicationContext(), MyService.class)); MyService service = ((MyService.LocalBinder) binder).getService(); assertThat(service.doSomethingToReturnTrue()).isTrue(); } }
Risorse aggiuntive
Per ulteriori informazioni sull'utilizzo delle regole JUnit nei test Android, consulta le risorse seguenti.
Documentazione
- Guida Testa i frammenti per testare i frammenti in modo isolato.
- È in corso il test del layout di Compose per verificare le UI create con Compose.
Samples
- BasicSample: utilizzo semplice di
ActivityScenarioRule
.