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.
Instrumented tests in 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.
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) } }
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) } }
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.
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) } }
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.
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.
Practice
Solution
Step 1: Understand what instrumented tests do
Instrumented tests run on actual devices or emulators to test app behavior in a real environment.Step 2: Compare with other test types
Unit tests run locally on JVM and do not test device features or UI on real devices.Final Answer:
To run tests on real devices or emulators to check app behavior -> Option CQuick Check:
Instrumented tests = run on device/emulator [OK]
- Confusing instrumented tests with unit tests
- Thinking instrumented tests only test backend logic
- Assuming instrumented tests create UI layouts
Solution
Step 1: Identify the runner annotation
Instrumented tests need the @RunWith(AndroidJUnit4::class) annotation to specify the test runner.Step 2: Differentiate from other annotations
@Test marks test methods, @Before runs setup code, @Ignore skips tests; none specify the runner.Final Answer:
@RunWith(AndroidJUnit4::class) -> Option AQuick Check:
Runner annotation = @RunWith(AndroidJUnit4::class) [OK]
- Using @Test instead of @RunWith for runner
- Confusing @Before with runner annotation
- Forgetting to add any runner annotation
val appContext = InstrumentationRegistry.getInstrumentation().targetContext val packageName = appContext.packageName println(packageName)
Solution
Step 1: Understand InstrumentationRegistry usage
InstrumentationRegistry.getInstrumentation().targetContext returns the context of the app under test, not the test app.Step 2: Extract package name from app context
appContext.packageName gives the package name of the app being tested, which is printed.Final Answer:
The package name of the app under test -> Option BQuick Check:
targetContext.packageName = app package [OK]
- Confusing targetContext with testContext
- Expecting empty string instead of package name
- Assuming runtime exception without cause
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
val context = InstrumentationRegistry.getContext()
assertEquals("com.example.app", context.packageName)
}
}Solution
Step 1: Check InstrumentationRegistry usage
InstrumentationRegistry.getContext() is deprecated and does not return the app context; getInstrumentation().targetContext should be used.Step 2: Verify other parts of the code
@RunWith is present, assertEquals syntax is correct, and @Before is not required here.Final Answer:
InstrumentationRegistry.getContext() is deprecated; should use getInstrumentation().targetContext -> Option AQuick Check:
Use getInstrumentation().targetContext, not getContext() [OK]
- Using deprecated getContext() method
- Assuming @Before is mandatory for all tests
- Thinking assertEquals syntax is wrong
Solution
Step 1: Setup instrumented test with correct runner and context
Use @RunWith(AndroidJUnit4::class) and get app context via InstrumentationRegistry.getInstrumentation().targetContext to access real app environment.Step 2: Launch activity, perform UI action, and verify result
Launch the activity, simulate button click, and check if TextView text updates as expected on device/emulator.Final Answer:
Use @RunWith(AndroidJUnit4::class), get app context with InstrumentationRegistry.getInstrumentation().targetContext, launch activity, perform click, check TextView text -> Option DQuick Check:
Instrumented UI test = runner + app context + UI actions [OK]
- Using test context instead of app context
- Writing unit tests for UI behavior
- Skipping @RunWith annotation
Ready to prove it?
Pass the test and this pattern moves unknown
