Skip to content

Commit

Permalink
Implemented repeat attempts by default (#394)
Browse files Browse the repository at this point in the history
* Implemented repeat attempts by default

* Expose method in FlakyTestRule

* trigger GitHub actions

* Removed bad test and extracted build statement to method

* trigger GitHub actions

* Implemented PR suggestions
  • Loading branch information
danieldisu authored May 17, 2021
1 parent 1c2f532 commit d31b84d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ class FlakyTestRule : TestRule {
return this
}

/**
* Utility method to use @[Repeat] by default in all test methods.
* <br></br>
* 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.
* <br></br>
* 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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -115,4 +156,4 @@ public void evaluate() throws Throwable {
}
}
//endregion
}
}

0 comments on commit d31b84d

Please sign in to comment.