Skip to content

Commit

Permalink
Merge pull request #20517 from wordpress-mobile/andy/issue-20461
Browse files Browse the repository at this point in the history
[Test] Add custom Rule to retry failed tests if annotated appropriately.
  • Loading branch information
notandyvee authored Mar 29, 2024
2 parents c940ee4 + cd5d8d9 commit 6facb72
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.Test
import org.wordpress.android.BuildConfig
import org.wordpress.android.R
import org.wordpress.android.e2e.pages.MySitesPage
import org.wordpress.android.rules.Retry
import org.wordpress.android.support.BaseTest
import org.wordpress.android.support.ComposeEspressoLink
import org.wordpress.android.support.WPSupportUtils
Expand All @@ -37,8 +38,8 @@ class StatsGranularTabsTest : BaseTest() {
}
}

@Test
@Ignore("The 'Days' screen might occasionally not load. Disabled until tests rerun is implemented.")
@Test
fun e2eAllDayStatsLoad() {
val todayVisits = StatsVisitsData("97", "28", "14", "11")
val postsList: List<StatsKeyValueData> = StatsMocksReader().readDayTopPostsToList()
Expand Down
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)
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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.wordpress.android.editor.Utils;
import org.wordpress.android.mocks.AndroidNotifier;
import org.wordpress.android.mocks.AssetFileSource;
import org.wordpress.android.rules.RetryTestRule;
import org.wordpress.android.ui.WPLaunchActivity;
import org.wordpress.android.wiremock.WireMockStub;

Expand Down Expand Up @@ -77,6 +78,9 @@ public class BaseTest {
@Rule(order = 4)
public WireMockRule wireMockRule;

@Rule(order = 5)
public RetryTestRule retryTestRule = new RetryTestRule();

public BaseTest() {
this(null);
}
Expand Down

0 comments on commit 6facb72

Please sign in to comment.