Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix and tests for two problems when filtering

Junit3 test suites: 

1. NoTestsRemainException is not thrown when
the test suite has no tests after filtering.

2. Filter does not work for classes added to the
test suite.
  • Loading branch information...
commit 18179f198f986517f81d002bee96f2580cf69594 1 parent b4180bf
David Schuler authored
View
28 src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java
@@ -125,20 +125,36 @@ private static String createSuiteDescription(TestSuite ts) {
}
public void filter(Filter filter) throws NoTestsRemainException {
- if (getTest() instanceof Filterable) {
- Filterable adapter= (Filterable) getTest();
+ Test test= getTest();
+ Test filteredTest= filter(filter, test);
+ int testCases= filteredTest.countTestCases();
+ if (testCases == 0) {
+ throw new NoTestsRemainException();
+ }
+ setTest(filteredTest);
+ }
+
+ private Test filter(Filter filter, Test testToFilter)
+ throws NoTestsRemainException {
+ if (testToFilter instanceof Filterable) {
+ Filterable adapter= (Filterable) testToFilter;
adapter.filter(filter);
- } else if (getTest() instanceof TestSuite) {
- TestSuite suite= (TestSuite) getTest();
+ } else if (testToFilter instanceof TestSuite) {
+ TestSuite suite= (TestSuite) testToFilter;
TestSuite filtered= new TestSuite(suite.getName());
int n= suite.testCount();
for (int i= 0; i < n; i++) {
Test test= suite.testAt(i);
- if (filter.shouldRun(makeDescription(test)))
+ if (test instanceof TestSuite) {
+ test= filter(filter, test);
+ if (test.countTestCases() > 0)
+ filtered.addTest(test);
+ } else if (filter.shouldRun(makeDescription(test)))
filtered.addTest(test);
}
- setTest(filtered);
+ return filtered;
}
+ return testToFilter;
}
public void sort(Sorter sorter) {
View
63 src/test/java/org/junit/tests/junit3compatibility/SuiteMethodTest.java
@@ -9,10 +9,17 @@
import junit.framework.TestSuite;
import org.junit.Ignore;
import org.junit.Test;
+import org.junit.internal.runners.SuiteMethod;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
+import org.junit.runner.RunWith;
+import org.junit.runner.manipulation.Filter;
+import org.junit.runner.manipulation.NoTestsRemainException;
+import org.junit.runners.AllTests;
+import org.junit.tests.junit3compatibility.AllTestsTest.All;
+import org.junit.tests.junit3compatibility.AllTestsTest.OneTest;
public class SuiteMethodTest {
public static boolean wasRun;
@@ -120,4 +127,60 @@ public void descriptionAndRunNotificationsAreConsistent() {
assertEquals(1, res.getRunCount());
assertEquals(0, res.getIgnoreCount());
}
+
+
+ @org.junit.Test(expected= NoTestsRemainException.class) public void testNoTestsRemainException() throws Throwable {
+ SuiteMethod m= new SuiteMethod(All.class);
+ m.filter(new Filter() {
+
+ @Override
+ public boolean shouldRun(Description description) {
+ return false;
+ }
+
+ @Override
+ public String describe() {
+ return null;
+ }
+ });
+ }
+
+ public static class ThreeTest extends TestCase {
+ public void test1() {
+ }
+ public void test2() {
+ }
+
+ public void test3() {
+ }
+ }
+
+ public static class All2 {
+ static public junit.framework.Test suite() {
+ TestSuite suite= new TestSuite();
+ suite.addTestSuite(OneTest.class);
+ suite.addTestSuite(ThreeTest.class);
+ return suite;
+ }
+ }
+
+ @org.junit.Test public void testFilter() throws Throwable {
+ SuiteMethod m= new SuiteMethod(All2.class);
+ m.filter(new Filter() {
+ @Override
+ public boolean shouldRun(Description description) {
+ String className= description.getClassName();
+ String methodName= description.getMethodName();
+ return className != null
+ && className.contains(ThreeTest.class.getName())
+ && methodName != null && methodName.startsWith("test");
+ }
+ @Override
+ public String describe() {
+ return null;
+ }
+ });
+ int testCount= m.testCount();
+ assertEquals(3, testCount);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.