0
0
Android Kotlinmobile~5 mins

Instrumented tests in Android Kotlin

Choose your learning style9 modes available
Introduction

Instrumented tests check how your app works on a real device or emulator. They help make sure your app behaves correctly when users interact with it.

To test if buttons and screens respond correctly when tapped.
To check if your app saves data properly on a device.
To verify navigation between different screens works as expected.
To ensure your app handles device features like camera or GPS correctly.
To catch bugs that only appear when running on a real device or emulator.
Syntax
Android Kotlin
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
  @Test
  fun useAppContext() {
    val appContext = InstrumentationRegistry.getInstrumentation().targetContext
    assertEquals("com.example.myapp", appContext.packageName)
  }
}

Use @RunWith(AndroidJUnit4::class) to tell Android to run the test on a device or emulator.

Use InstrumentationRegistry.getInstrumentation().targetContext to get the app context during the test.

Examples
This test checks if the app package name is correct on the device.
Android Kotlin
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*

@RunWith(AndroidJUnit4::class)
class SimpleInstrumentedTest {
  @Test
  fun checkPackageName() {
    val context = InstrumentationRegistry.getInstrumentation().targetContext
    assertEquals("com.example.app", context.packageName)
  }
}
This test checks if the device has a camera feature.
Android Kotlin
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*

@RunWith(AndroidJUnit4::class)
class DeviceFeatureTest {
  @Test
  fun hasCamera() {
    val context = InstrumentationRegistry.getInstrumentation().targetContext
    val pm = context.packageManager
    val hasCamera = pm.hasSystemFeature("android.hardware.camera")
    assertTrue(hasCamera)
  }
}
Sample App

This is a simple instrumented test that runs on a device or emulator. It checks if the app's package name is "com.example.myapp". If the package name matches, the test passes. This helps confirm the app is installed and running correctly.

Android Kotlin
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
  @Test
  fun useAppContext() {
    val appContext = InstrumentationRegistry.getInstrumentation().targetContext
    assertEquals("com.example.myapp", appContext.packageName)
  }
}
OutputSuccess
Important Notes

Instrumented tests run on a real device or emulator, so they take longer than simple unit tests.

Make sure your device or emulator is connected and ready before running instrumented tests.

Use Android Studio's Run or Debug options to execute instrumented tests easily.

Summary

Instrumented tests check app behavior on real devices or emulators.

They help test UI, device features, and app context.

Use @RunWith(AndroidJUnit4::class) and InstrumentationRegistry to write these tests.