
Android project test fixtures
Thanks to Steven Mulder, Jov Mit, Jeroen Mols and Martin Bonnin for proofreading this article and providing comments.
(1 Mar 2025) Update article with the latest state of things:
- Mention AGP (8.9) version where stdlib is added automatically
- Correct min AGP to 8.5.1 version that has fix for test fixtures
The long-awaited support for test fixtures is coming to Android projects, starting from the Android Gradle plugin 8.5.1 (I use the AGP acronym in the article below).
I have been chasing the opened ticket and ticket in the article here for three years.
If you are not sure what test fixtures are. Then, consider the next example.
Your module exposes neat LoginRepository
like:
interface LoginRepository {
suspend fun login(username: Username: password: Password): Result<Unit>
}
And you have a fake for it to use in unit tests:
class FakeLoginRepository(
var result: Result<Unit> = Unit.asSuccessResult()
): Repository {
override suspend fun login(username: Username: password: Password) = result
}
To not copy/paste it to every module that uses the LoginRepository
in tests, you can write it once in your library module and share fake in the test fixtures for that module.
Here is the full setup and some small things to watch out for.
Preparation
Migrate your project to use AGP 8.5.1.
Add the following lines to your gradle.properties
file in the project root folder:
# Test fixtures support
android.experimental.enableTestFixturesKotlinSupport=true
Create test fixtures
Enable test fixtures in the build.gradle
file of the lib module:
android {
testFixtures {
enable = true
}
}
or shorter
android {
testFixtures.enable = true
}
Add Kotlin standard library dependency:
dependencies {
testFixturesCompileOnly "org.jetbrains.kotlin:kotlin-stdlib:<kotlin-version>"
}
Adding stdlib dependency is unexpected because Kotlin Gradle plugin does it for you automatically. But it might be changed in future. It is planned for AGP 8.9.
Add test fixture code in the right folder:
lib-module-with-test-fixtures
/src
/testFixtures
/<package>
TestFixtures.kt
The test fixtures code belongs to the module, so it has access to the internal module API.
Fixed: There is a bug in Android Studio that shows an error in the code when you try to access the internal module code in test fixtures.
After these steps, you can see a new build artifact it-debug-testFixtures-testFixtures.aab
if you run next gradle
./gradlew :<lib-module-with-text-fixtures>:assembleDebugTestFixtures
If you’re a library author, you can also add text fixtures as an artifact!
Use test fixtures
Add as a dependency to the module where you want to use it:
dependencies {
testImplementation testFixtures(project(":lib-module-with-test-fixtures"))
}
Use test fixtures in your tests now!
You can find my test project with fixture usage here (test
branch). The database
module provides test fixtures, and the core
is consuming them.
Credits to Lukáš Sztefek for his multi-module
project that I used to start testing the test fixtures feature.
Something to explore shortly — how this setup works in the KMP project.