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

Evaluate TestRequestBuilder.CustomFilters also on class, not just on test. #2209

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions runner/android_junit_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

**New Features**

* Evaluate annotation with @CustomFilter on test class

**Breaking Changes**

**API Changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ static Suite createSuite(List<Runner> runners) {
}

private static class CustomFilters extends AbstractFilter {

@Override
public boolean shouldRun(Description description) {
if (description.isSuite() && !evaluateTest(description)) {
return false;
}
return super.shouldRun(description);
}

@Override
protected boolean evaluateTest(Description description) {
Collection<Annotation> allAnnotations = description.getAnnotations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,50 @@ public String describe() {
}
}

@SampleCustomAnnotationOnClass(runTests = true)
public static class SampleCustomFilterOnClassRunTestsTestClass {
@Test
public void testOneRun() {}

@Test
public void testTwoRun() {}
}

@SampleCustomAnnotationOnClass(runTests = false)
public static class SampleCustomFilterOnClassSkipTestsTestClass {
@Test
public void testOneSkip() {}

@Test
public void testTwoSkip() {}
}

public static class SampleCustomFilterOnClass extends AbstractFilter {

Choose a reason for hiding this comment

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

This test code esentially simulates consumer code. The consumer shouldn't need to override anything but evaluateTest for this PR to work on classes. And they don't, thanks to the changes in CustomFilters.shouldRun above.

This class shouldn't exist. Usages can be replaced by the existing SampleCustomFilter class.

public SampleCustomFilterOnClass() {}

@Override
public boolean shouldRun(Description description) {
return evaluateTest(description);
}

@Override
protected boolean evaluateTest(Description description) {
return description.getAnnotation(SampleCustomAnnotationOnClass.class).runTests();
}

@Override
public String describe() {
return "skip all tests in class if runTests is false";
}
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@CustomFilter(filterClass = SampleCustomFilterOnClass.class)
public @interface SampleCustomAnnotationOnClass {

Choose a reason for hiding this comment

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

This annotation shouldn't exist, it's essentially the same as SampleCustomAnnotation, just below.

boolean runTests() default true;
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@CustomFilter(filterClass = SampleCustomFilter.class)
Expand Down Expand Up @@ -971,6 +1015,24 @@ public void testCustomFilter() {
Assert.assertEquals(1, result.getRunCount());
}

/** Test that {@link CustomFilter} filters the class as appropriate */
@Test
public void testCustomFilterAnnotation_onClass_runTests() {
Request request = builder.addTestClass(SampleCustomFilterOnClassRunTestsTestClass.class.getName()).build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
Assert.assertEquals(2, result.getRunCount());
}

/** Test that {@link CustomFilter} filters the class as appropriate */
@Test
public void testCustomFilterAnnotation_onClass_skipTests() {
Request request = builder.addTestClass(SampleCustomFilterOnClassSkipTestsTestClass.class.getName()).build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
Assert.assertEquals(0, result.getRunCount());
}

/** Test that a custom RunnerBuilder is used. */
@Test
public void testCustomRunnerBuilder() {
Expand Down