https://kotlinlang.org logo
e

escodro

02/24/2021, 11:50 AM
Hello, everyone! 😊 Is there a way to run repeat/flaky tests in Jetpack Compose? If I add any rule to do it, it fails with
Cannot call setContent twice per test!
Is it possible to “clearContent” before starting it again? Some of my tests are flaky because I need to mix it up with Espresso. ☹️ Thanks in advance!
p

ppvi

02/24/2021, 1:00 PM
have you tried
createEmptyComposeRule
?
e

escodro

02/24/2021, 1:24 PM
No, I didn’t know that! Thanks a lot! 😊 So basically I use this rule and in each test set the ActivityScenario to launch my Composable?
p

ppvi

02/24/2021, 1:51 PM
yes that's the idea I think but I haven't had the chance to use it, let me know if it works please
❤️ 1
e

escodro

02/24/2021, 2:00 PM
Thanks a lot again, José. It worked! I don’t know if I did the best implementation, but it is working without any issues so far. 😊
p

ppvi

02/24/2021, 2:33 PM
wait but you're not using the compose test rule at all? you're not going to be able to target compose elements
e

escodro

02/24/2021, 2:37 PM
I’m sorry. The image does not represent the full context.
Copy code
@get:Rule
val composeTestRule = createEmptyComposeRule()

@get:Rule
val flakyRule = FlakyTestRule().allowFlakyAttemptsByDefault(defaultAttempts = 10)

private lateinit var scenario: ActivityScenario<ComponentActivity>

@Before
fun setup() {
    scenario = ActivityScenario.launch(ComponentActivity::class.java)
}

@After
fun tearDown(){
    scenario.close()
}

@Test
@Repeat(times = 10)
fun test() {
        scenario.onActivity {
            it.setContent { ... }
        }
    }
20 Views