Skip to content

Instantly share code, notes, and snippets.

@MaTriXy
Created July 14, 2017 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaTriXy/60da3912d7fd6b7a5efbcf9d615370a6 to your computer and use it in GitHub Desktop.
Save MaTriXy/60da3912d7fd6b7a5efbcf9d615370a6 to your computer and use it in GitHub Desktop.
Obeying the rules ? Challenge accepted. Blog post about using TestRules and Parameterized tests in Android
public class RepeatRule implements TestRule {
@Retention(RetentionPolicy.RUNTIME)
@Target({METHOD, ANNOTATION_TYPE})
public @interface Repeat {
int value() default 1;
}
private static class RepeatStatement extends Statement {
private final int times;
private final Statement statement;
private RepeatStatement(int times, Statement statement) {
this.times = times;
this.statement = statement;
}
@Override
public void evaluate() throws Throwable {
for (int i = 0; i < times; i++) {
statement.evaluate();
}
}
}
@Override
public Statement apply(Statement statement, Description description) {
Statement result = statement;
Repeat repeat = description.getAnnotation(Repeat.class);
if (repeat != null) {
int times = repeat.value();
result = new RepeatStatement(times, statement);
}
return result;
}
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{MB_1, TIMEOUT_MB_1},
{MB_10, TIMEOUT_MB_10},
{MB_20, TIMEOUT_MB_20},
{MB_30, TIMEOUT_MB_30},
{MB_40, TIMEOUT_MB_40},
{MB_50, TIMEOUT_MB_50},
{MB_100, TIMEOUT_MB_100}
});
}
@Parameterized.Parameter(0)
public static int size;
@Parameterized.Parameter(1)
public static long threshold;
@Rule
public RepeatRule repeatRule = new RepeatRule();
@ClassRule
public static ErrorCollector collector = new ErrorCollector();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment