diff --git a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt
index 635382782..335afbaab 100644
--- a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt
+++ b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt
@@ -29,10 +29,23 @@ class FlakyTestRule : TestRule {
return this
}
+ /**
+ * Utility method to use @[Repeat] by default in all test methods.
+ *
+ * Use this method when constructing the Rule to apply a default behavior of @[Repeat] without having to add the annotation to
+ * each test. This can help you to find flaky tests.
+ *
+ * The default behavior can be overridden with [Repeat] or [AllowFlaky].
+ */
+ fun repeatAttemptsByDefault(defaultAttempts: Int): FlakyTestRule {
+ flakyStatementBuilder.setRepeatAttemptsByDefault(defaultAttempts)
+ return this
+ }
+
override fun apply(base: Statement, description: Description): Statement {
return flakyStatementBuilder
.setBase(base)
.setDescription(description)
.build()
}
-}
\ No newline at end of file
+}
diff --git a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java
index 51d83759e..1fd6d3fef 100644
--- a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java
+++ b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java
@@ -11,6 +11,8 @@ public class FlakyStatementBuilder {
private Description description;
private boolean useAllowFlakyByDefault = false;
private int defaultAllowFlakyAttempts = 0;
+ private int defaultRepeatAttempts = 1;
+ private boolean useRepeatByDefault = false;
public FlakyStatementBuilder setBase(Statement base) {
this.base = base;
@@ -22,8 +24,16 @@ public FlakyStatementBuilder setDescription(Description description) {
return this;
}
+ public FlakyStatementBuilder setRepeatAttemptsByDefault(int attempts) {
+ useAllowFlakyByDefault = false;
+ useRepeatByDefault = true;
+ defaultRepeatAttempts = attempts;
+ return this;
+ }
+
public FlakyStatementBuilder allowFlakyAttemptsByDefault(int attempts) {
useAllowFlakyByDefault = true;
+ useRepeatByDefault = false;
defaultAllowFlakyAttempts = attempts;
return this;
}
@@ -46,6 +56,8 @@ public Statement build() {
return new AllowFlakyStatement(attempts, base);
} else if (useAllowFlakyByDefault) {
return new AllowFlakyStatement(defaultAllowFlakyAttempts, base);
+ } else if (useRepeatByDefault) {
+ return new RepeatStatement(defaultRepeatAttempts, base);
} else {
return base;
}
diff --git a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java
index ade35e4f5..1e7816bae 100644
--- a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java
+++ b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java
@@ -44,6 +44,16 @@ public void repeatStatementReturnedWhenRepeatAnnotationFound() throws Exception
assertTrue(resultStatement instanceof RepeatStatement);
}
+ @Test
+ public void repeatStatementReturnedWhenSettingDefaultRepeatAttempts() throws Exception {
+ Statement baseStatement = new SomeStatement();
+ Description description = Description.EMPTY;
+
+ Statement resultStatement = createStatementWithRepeatAttemptsByDefault(baseStatement, description);
+
+ assertTrue(resultStatement instanceof RepeatStatement);
+ }
+
@Test
public void allowFlakyStatementReturnedWhenAllowFlakyAnnotationFound() throws Exception {
Statement baseStatement = new SomeStatement();
@@ -59,13 +69,36 @@ public void allowFlakyStatementReturnedWhenNoAnnotationsFoundButUsesDefault() th
Statement baseStatement = new SomeStatement();
Description description = Description.EMPTY;
- Statement resultStatement = new FlakyStatementBuilder()
+ Statement resultStatement = createStatementWithAllowFlakyByDefault(baseStatement, description);
+
+ assertTrue(resultStatement instanceof AllowFlakyStatement);
+ }
+
+ @Test
+ public void lastStatementReturnedWhenDefaultRepeatAttemptsAndAllowFlakyStatementUsedAtTheSameTime() throws Exception {
+ Statement baseStatement = new SomeStatement();
+ Description description = Description.EMPTY;
+
+ Statement resultStatement = createStatementWithFlakyAndReturn(baseStatement, description);
+
+ assertTrue(resultStatement instanceof RepeatStatement);
+ }
+
+ private Statement createStatementWithFlakyAndReturn(Statement baseStatement, Description description) {
+ return new FlakyStatementBuilder()
.setBase(baseStatement)
.setDescription(description)
.allowFlakyAttemptsByDefault(5)
+ .setRepeatAttemptsByDefault(3)
.build();
+ }
- assertTrue(resultStatement instanceof AllowFlakyStatement);
+ private Statement createStatementWithAllowFlakyByDefault(Statement baseStatement, Description description) {
+ return new FlakyStatementBuilder()
+ .setBase(baseStatement)
+ .setDescription(description)
+ .allowFlakyAttemptsByDefault(5)
+ .build();
}
//region Shortcut methods
@@ -76,6 +109,14 @@ private Statement createStatement(Statement baseStatement, Description descripti
.build();
}
+ private Statement createStatementWithRepeatAttemptsByDefault(Statement baseStatement, Description description) {
+ return new FlakyStatementBuilder()
+ .setBase(baseStatement)
+ .setDescription(description)
+ .setRepeatAttemptsByDefault(3)
+ .build();
+ }
+
private Description withAnnotations(Annotation... annotations) {
return Description.createTestDescription(CLASS, TEST, annotations);
}
@@ -115,4 +156,4 @@ public void evaluate() throws Throwable {
}
}
//endregion
-}
\ No newline at end of file
+}