Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds retryable compose test rule #574

Open
wants to merge 1 commit into
base: androidx-main
Choose a base branch
from

Conversation

dabrosch
Copy link

Proposed Changes

  • This solves the "Only a single call to runTest can be performed during one test." error when you are trying to retry a test that has uses a ComposeRule.
  • Achieved by creating a RetryableComposeRule class.
  • This is needed because the environment that gets created from a ComposeRule is not supposed to be reused in any other test runs per discussion here: Retrying of test not fully supported with AndroidComposeTestRule Kotlin/kotlinx.coroutines#3718 (comment) so this new rule creates a new environment after each test has ran.
  • This should help keep people from following the incorrect workaround such as here: https://issuetracker.google.com/issues/235383900 where they re-use the ComposeRule for subsequent retries by ordering their rules specifically to do so.
  • This rule should be ordered after any retry rule.

Testing

Test: I setup an intentionally failing compose jUnit test with a retry rule outside of my RetryableComposeRule and observed that upon the retry there was NO error of "Only a single call to runTest can be performed during one test."

Issues Fixed

Fixes: https://issuetracker.google.com/issues/235383900 and Kotlin/kotlinx.coroutines#3718 (comment)

@AShunevych
Copy link

Can you explain how exactly your rule should be implemented ? For example, we have rule chain :

//
val basicRule = createAndroidComposeRule<MyActivity>()  //  basic rule 
val retryRule = RetryableComposeTestRule  { basicRule } // retry rule as you propose 
val flakyRule = FlakytestRule().allowFlakyAttemptsByDefault(2) // simple test rule from Barista lib 

// So we have flaky rule as outerRule and all rules goes around
@get:Rule
val rulechain:RuleChain = run {
 RuleChain.outerRule(flakyRule).around(basicRule).around(retryRule) 
}

Test will restart, but again IllegalStateException("Only a single call to runTest can be performed during one test.") will be thrown exception will be thrown.
Thank you.

@dabrosch
Copy link
Author

Can you explain how exactly your rule should be implemented ? For example, we have rule chain :

//
val basicRule = createAndroidComposeRule<MyActivity>()  //  basic rule 
val retryRule = RetryableComposeTestRule  { basicRule } // retry rule as you propose 
val flakyRule = FlakytestRule().allowFlakyAttemptsByDefault(2) // simple test rule from Barista lib 

// So we have flaky rule as outerRule and all rules goes around
@get:Rule
val rulechain:RuleChain = run {
 RuleChain.outerRule(flakyRule).around(basicRule).around(retryRule) 
}

Test will restart, but again IllegalStateException("Only a single call to runTest can be performed during one test.") will be thrown exception will be thrown. Thank you.

The problem is that you have two composeRules in the RuleChain, you have the basicRule AND the retryRule, you should only have the retryRule.

So I suggest removing the entire val basicRule line and instead change the next to this:

val retryRule = RetryableComposeTestRule  { createAndroidComposeRule<MyActivity>() } // retry rule as you propose 

@dabrosch
Copy link
Author

dabrosch commented Sep 21, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants