-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20517 from wordpress-mobile/andy/issue-20461
[Test] Add custom Rule to retry failed tests if annotated appropriately.
- Loading branch information
Showing
4 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
WordPress/src/androidTest/java/org/wordpress/android/rules/Retry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.wordpress.android.rules | ||
|
||
/** | ||
* Annotation used to denote you want to retry a UI test function. | ||
* | ||
* @property numberOfTimes the number of times you want to retry the function, with a default of 1. | ||
*/ | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Target(AnnotationTarget.FUNCTION) | ||
annotation class Retry(val numberOfTimes: Int = 1) |
40 changes: 40 additions & 0 deletions
40
WordPress/src/androidTest/java/org/wordpress/android/rules/RetryTestRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.wordpress.android.rules | ||
|
||
import android.util.Log | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
const val TAG = "RetryTestRule" | ||
|
||
/** | ||
* Custom rule used to retry running a test if a problem occurs. | ||
*/ | ||
class RetryTestRule : TestRule { | ||
override fun apply(base: Statement?, description: Description?): Statement { | ||
return object : Statement() { | ||
override fun evaluate() { | ||
// We only retry functions that are annotated with @Retry. | ||
val retry = description?.getAnnotation(Retry::class.java) | ||
if (retry != null) { | ||
var lastThrown: Throwable? = null | ||
for (i in 0..retry.numberOfTimes) { | ||
try { | ||
base?.evaluate() | ||
return | ||
} catch (t: Throwable) { | ||
Log.e(TAG, "Test failed to run due to problem on run $i", t) | ||
lastThrown = t | ||
} | ||
} | ||
Log.e(TAG, "Could not pass test.") | ||
if (lastThrown != null) { | ||
throw lastThrown | ||
} | ||
} | ||
// If test function does not have @Retry, run as normal. | ||
base?.evaluate() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters