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

Make FlakyRule tests less flaky #362

Merged
merged 4 commits into from
Aug 20, 2020
Merged
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 @@ -4,20 +4,15 @@
import com.schibsted.spain.barista.rule.flaky.AllowFlaky;
import com.schibsted.spain.barista.rule.flaky.FlakyTestRule;
import com.schibsted.spain.barista.rule.flaky.Repeat;
import java.util.Random;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

public class FlakyTestRuleTest {

private final Random random = new Random();
private static boolean hasRunAllowFlakyTest = false;
private static boolean hasRunRepeatTest = false;

private ActivityTestRule<HelloWorldActivity> activityRule = new ActivityTestRule<>(HelloWorldActivity.class, true, false);
private FlakyTestRule flakyRule = new FlakyTestRule()
Expand All @@ -29,35 +24,32 @@ public class FlakyTestRuleTest {

// WARNING: this test must fail when run
@Test
@Repeat(times = 5)
@Repeat(times = 2)
@Ignore
public void someImportantFlakyTest() throws Exception {
public void repeatAnnotationTest() throws Exception {
activityRule.launchActivity(null);

onView(withId(R.id.some_view)).check(matches(isDisplayed()));

if (random.nextFloat() > 0.3) {
throw new TestException("Random test failure");
if (hasRunRepeatTest) {
throw new TestException("Test failure on second try");
} else {
hasRunRepeatTest = true;
}
}

@Test
@AllowFlaky(attempts = 10)
public void someFlakyTest() throws Exception {
@AllowFlaky(attempts = 2)
public void allowFlakyAnnotationTest() throws Exception {
activityRule.launchActivity(null);

onView(withId(R.id.some_view)).check(matches(isDisplayed()));

if (random.nextFloat() > 0.3) {
throw new TestException("Random test failure");
if (!hasRunAllowFlakyTest) {
hasRunAllowFlakyTest = true;
throw new TestException("Test failure on first try");
}
}

@Test
public void someDeterministicTest() throws Exception {
public void testWithoutAnnotations() throws Exception {
activityRule.launchActivity(null);

onView(withId(R.id.some_view)).check(matches(isDisplayed()));
}

private static class TestException extends Exception {
Expand Down