Running Tests In Android Studio



In this chapter, let us see how to run tests using Android studio.

Every android application has two type of tests −

  • Functional / Unit tests

  • Instrumentation tests

Functional test does not need the actual android application to be installed and launched in the device or emulator and test the functionality. It can be launched in the console itself without invoking the actual application. However, instrumentation tests need the actual application to be launched to test the functionality like user interface and user interaction. By default, Unit tests are written in src/test/java/ folder and Instrumentation tests are written in src/androidTest/java/ folder. Android studio provides Run context menu for the test classes to run the test written in the selected test classes. By default, an Android application has two classes − ExampleUnitTest in src/test folder and ExampleInstrumentedTest in src/androidTest folder.

To run the default unit test, select ExampleUnitTest in the Android studio, right-click on it and then click the Run 'ExampleUnitTest' as shown below,

Android Studio

Run Unit Test

This will run the unit test and show the result in the console as in the following screenshot −

Test And Show

Unit Test Success

To run the default instrumentation test, select ExampleInstrumentationTest in the android studio, right-click it and then click the Run 'ExampleInstrumentationTest' as shown below,

Instrumentation Test

Run Instrumentation Test

This will run the unit test by launching the application in either device or emulator and show the result in the console as in the following screenshot −

Unit Test

The instrumentation test ran successful.

Advertisements