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 3 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 [Repeat].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Repeat] or [Repeat]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was [Repeat] or [AllowFlaky], fixed!

*/
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,20 @@ public void repeatStatementReturnedWhenRepeatAnnotationFound() throws Exception
assertTrue(resultStatement instanceof RepeatStatement);
}

@Test
public void repeatStatementReturnedWhenRepeatSetRepeatAttemptsByDefault() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't get the repeat set repeat 😅

Maybe repeatStatementReturnedWhenSettingDefaultRepeatAttempts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it make sense 👍 , changed

Statement baseStatement = new SomeStatement();
Description description = Description.EMPTY;

Statement resultStatement = new FlakyStatementBuilder()
.setBase(baseStatement)
.setDescription(description)
.setRepeatAttemptsByDefault(3)
.build();

assertTrue(resultStatement instanceof RepeatStatement);
}

@Test
public void allowFlakyStatementReturnedWhenAllowFlakyAnnotationFound() throws Exception {
Statement baseStatement = new SomeStatement();
Expand All @@ -68,6 +82,36 @@ public void allowFlakyStatementReturnedWhenNoAnnotationsFoundButUsesDefault() th
assertTrue(resultStatement instanceof AllowFlakyStatement);
}

@Test
public void lastStatementReturnedWhenRepeatSetRepeatAttemptsByDefaultAndAllowFlakyStatementAtTheSameTime() throws Exception {
Statement baseStatement = new SomeStatement();
Description description = Description.EMPTY;

Statement resultStatement = new FlakyStatementBuilder()
.setBase(baseStatement)
.setDescription(description)
.allowFlakyAttemptsByDefault(5)
.setRepeatAttemptsByDefault(3)
.build();

assertTrue(resultStatement instanceof RepeatStatement);
}

@Test
public void allowFlakyStatementReturnedWhenAllowFlakyAnnotationFoundEvenRepeatStatement() throws Exception {
Statement baseStatement = new SomeStatement();
Description description = Description.EMPTY;

Statement resultStatement = new FlakyStatementBuilder()
.setBase(baseStatement)
.setDescription(description)
.allowFlakyAttemptsByDefault(5)
.setRepeatAttemptsByDefault(3)
.build();

assertTrue(resultStatement instanceof RepeatStatement);
}

//region Shortcut methods
private Statement createStatement(Statement baseStatement, Description description) {
return new FlakyStatementBuilder()
Expand Down Expand Up @@ -115,4 +159,4 @@ public void evaluate() throws Throwable {
}
}
//endregion
}
}