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

Implemented repeat attempts by default #394

Merged
merged 6 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}