This recipe shows how to use test fixtures with Android projects.
Be Aware: we don't support writing test-fixtures in Kotlin for AGP versions up to 8.4, only Java
Recipe has the following module structure:
Module | Content |
---|---|
lib | An Android library module has UserRepository and fixture. |
app | An application that uses repository. It needs lib fixture to test code properly. |
Fixtures switched on with testFixtures.enable = true
in a library build file.
Application and library module dependencies look like following:
app -> lib
app/test -> lib/testFixtures
Test dependency must be declared in Gradle script to target the fixture artifact provided by the library:
testImplementation testFixtures(project(":lib"))
.
You can check details of logic we test in ViewModel, test itself in ViewModelTest and fixture UserRepoFixture.
To execute example and run tests you need to enter command:
./gradlew app:testDebug
This way app/.../ViewModelTest.kt will be
executed to check if ViewModel
works using fixture.