Skip to content

Commit

Permalink
[#354] Set up running UI tests with Robolectric as default in the tem…
Browse files Browse the repository at this point in the history
…plate
  • Loading branch information
luongvo committed Feb 13, 2023
1 parent e307a70 commit e569f73
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 2 additions & 3 deletions sample-compose/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,13 @@ dependencies {
testImplementation("io.mockk:mockk:${Versions.TEST_MOCKK_VERSION}")
testImplementation("app.cash.turbine:turbine:${Versions.TEST_TURBINE_VERSION}")

// Instrument test with Robolectric
// Need to have BOM for testImplementation https://github.com/gradle/gradle/issues/23347
// UI test with Robolectric
testImplementation(platform("androidx.compose:compose-bom:${Versions.COMPOSE_BOM_VERSION}"))
testImplementation("androidx.compose.ui:ui-test-junit4")
testImplementation("androidx.test:rules:${Versions.TEST_RULES_VERSION}")
testImplementation("org.robolectric:robolectric:${Versions.TEST_ROBOLECTRIC_VERSION}")

// Instrument test
// UI test
androidTestImplementation(platform("androidx.compose:compose-bom:${Versions.COMPOSE_BOM_VERSION}"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
androidTestImplementation("androidx.test:rules:${Versions.TEST_RULES_VERSION}")
Expand Down
11 changes: 8 additions & 3 deletions template-compose/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ android {
}

testOptions {
unitTests {
// Robolectric resource processing/loading https://github.com/robolectric/robolectric/pull/4736
isIncludeAndroidResources = true
}
unitTests.all {
if (it.name != "testStagingDebugUnitTest") {
it.extensions.configure(kotlinx.kover.api.KoverTaskExtension::class) {
Expand Down Expand Up @@ -151,7 +155,8 @@ dependencies {
testImplementation("io.mockk:mockk:${Versions.TEST_MOCKK_VERSION}")
testImplementation("app.cash.turbine:turbine:${Versions.TEST_TURBINE_VERSION}")

// Instrument test
androidTestImplementation(platform("androidx.compose:compose-bom:${Versions.COMPOSE_BOM_VERSION}"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
// UI test with Robolectric
testImplementation(platform("androidx.compose:compose-bom:${Versions.COMPOSE_BOM_VERSION}"))
testImplementation("androidx.compose.ui:ui-test-junit4")
testImplementation("org.robolectric:robolectric:${Versions.TEST_ROBOLECTRIC_VERSION}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package co.nimblehq.template.compose.ui.screens.home
import androidx.activity.compose.setContent
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import co.nimblehq.template.compose.R
import co.nimblehq.template.compose.ui.AppDestination
import co.nimblehq.template.compose.ui.screens.MainActivity
import org.junit.*
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class HomeScreenTest {

@get:Rule
Expand All @@ -27,7 +28,7 @@ class HomeScreenTest {
}

@Test
fun when_entering_the_Home_screen__it_shows_UI_correctly() {
fun `When entering the Home screen, it shows UI correctly`() {
composeRule.run {
onNodeWithText(activity.getString(R.string.app_name)).assertIsDisplayed()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Workaround for issue https://github.com/robolectric/robolectric/issues/6593
instrumentedPackages=androidx.loader.content
sdk=30
1 change: 1 addition & 0 deletions template-compose/buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ object Versions {
const val TEST_JUNIT_VERSION = "4.13.2"
const val TEST_KOTEST_VERSION = "4.6.3"
const val TEST_MOCKK_VERSION = "1.13.3"
const val TEST_ROBOLECTRIC_VERSION = "4.9.2"
const val TEST_TURBINE_VERSION = "0.12.1"
}

0 comments on commit e569f73

Please sign in to comment.