Android modules

Android project test fixtures

🇺🇦 Eugen Martynov
2 min readMay 16, 2024

--

Thanks to Steven Mulder, Jov Mit, Jeroen Mols and Martin Bonnin for proofreading this article and providing comments.

The long-awaited support for test fixtures is coming to Android projects, starting from the Android Gradle plugin 8.5.0 (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.0 (Currently, 8.5.0-beta01 is available)

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
}
}

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.

Add test fixture code in 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.

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 then you can also add text fixtures as an artifact to your library!

Use test fixtures

Add as a dependency to 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 as the start for testing the test fixtures feature.

Something to explore in the nearest future — how this setup works in KMP project.

If you like the article, you can follow me on twitter.

Stand with Ukraine! 🇺🇦

Best!

--

--

🇺🇦 Eugen Martynov

Stand with Ukraine! The loving XP husband and freelance Android/Kotlin engineer.