A gradle plugin and testing library that lets you quickly and easily switch between JVM and Emulator/Device for running Android Instrumentation tests.
🚧 This project is in alpha, use with caution as the API is still under active development and may change
Create a new module to hold your Instrumentation tests
Apply the nitrogen plugin and the kotlin android plugin:
plugins {
id("tech.sarahgallitz.nitrogen-plugin") version "0.0.1"
id("org.jetbrains.kotlin.android")
}
Set the test module applicationId:
android {
defaultConfig {
testApplicationId = "demo.uiTests"
}
}
Add a dependency on the android-application module which the tests should run against, and add the nitrogen test library:
dependencies {
implementation(project(":demo:app"))
implementation("tech.sarahgallitz:nitrogen:0.0.1")
}
Don't forget to add androidx and JUnit dependencies as appopriate to your project. Nitrogen works with the AndroidX testing framework.
Add a nitrogen.config
file at the top level of your project, or at the top level of the
instrumentation module. The nitrogen.config
file should define the following properties:
isRunningOnJVM = true
targetProjectPath = :demo:app
Note: When changing the value of isRunningOnJVM
you should run a gradle sync afterwards.
Add your first test:
@RunWith(NitrogenTestRunner::class)
class ExampleNitrogenTest {
@Test
fun exampleTest() {
assertTrue(1 == 1)
}
@Test
fun exampleJVMOnlyTest() {
skipOnDevice()
assertTrue(1 == 1)
}
@Test
fun exampleDeviceOnlyTest() {
skipOnJVM()
assertTrue(1 == 1)
}
}
Nitrogen is built on top of:
- AndroidX
- Robolectric
At gradle configuration time it applies either the android-library
plugin (for JVM target) or
the android-test
plugin (for device target).
Robolectric requires activities to be added to the AndroidManifest of the test module. Currently the nitrogen plugin attempts to autogenerate the AndroidManifest for you, but this has some issues:
- any existing manifest will be overwritten
- the manifest generation is pretty dodgy and may not work for all applications
It is currently not possible to test real activity to activity navigation when targeting the JVM,
please mark these tests with skipOnJVM()
.