0

I'm trying to create a junit Runner that will group together common tests using the junit API:

package whatever;

import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

public class SomeTestRunner extends Runner {

    public SomeTestRunner(Class<?> testClass) {}

    @Override
    public Description getDescription() {
        return Description.EMPTY;
    }

    @Override
    public void run(RunNotifier notifier) {
        for (int i = 0; i < 3; i++) {
            Description parent = Description.createSuiteDescription("Parent_" + i);
            for (int j = 0; j < 3; j++) {
                Description child = Description.createTestDescription(Exception.class, "Child_" + j);
                parent.addChild(child);
                Failure failure = new Failure(child, new Exception());
                notifier.fireTestFailure(failure);
            }
            Failure failure = new Failure(parent, new Exception());
            notifier.fireTestFailure(failure);
        }
    }
}

The problem is, when I run a test using this Runner, I can see the failures in a row, both parents and children, instead of grouped together:

Results :

Tests in error: 
  Child_0(java.lang.Exception)
  Child_1(java.lang.Exception)
  Child_2(java.lang.Exception)
  Parent_0
  Child_0(java.lang.Exception)
  Child_1(java.lang.Exception)
  Child_2(java.lang.Exception)
  Parent_1
  Child_0(java.lang.Exception)
  Child_1(java.lang.Exception)
  Child_2(java.lang.Exception)
  Parent_2

Tests run: 12, Failures: 0, Errors: 12, Skipped: 0

Also, I expected to see them grouped together when I run this test in Eclipse - but thats not the case. What am I missing? Is it even possible?

Gangnus
  • 24,044
  • 16
  • 90
  • 149
uzilan
  • 2,554
  • 2
  • 31
  • 46
  • Its printing out in the exact order that you fire the test failures. What were you expecting differently? – Perception Feb 21 '12 at 21:36
  • Well I expected to see them grouped together in suites. Isn't that what createSuiteDescription() and addChild() are ment to do? – uzilan Feb 21 '12 at 21:46

2 Answers2

0

You can use a JUnit Test Suite, just like this:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ SomeTest.class, AnotherTest.class, YetAnotherTest.class })
public class AllTests {

}
Dmytro Chyzhykov
  • 1,814
  • 1
  • 20
  • 17
0

The template to follow is Parameterized, because it seems to do what you want. For a given test class it runs the test methods multiple times, it creates a Runner for each set of parameters, which is what I think you want:

public class GroupedTestRunner extends Suite {
  private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner {
    private String name;

    TestClassRunnerForParameters(Class<?> type, String name) throws InitializationError {
      super(type);
      this.name = name;
    }

    @Override
    public Object createTest() throws Exception {
      return getTestClass().getOnlyConstructor().newInstance();
    }

    @Override
    protected String getName() {
      return String.format("[%s]", name);
    }

    @Override
    protected String testName(final FrameworkMethod method) {
      return String.format("%s[%s]", method.getName(), name);
    }
  }

  private final ArrayList<Runner> runners = new ArrayList<Runner>();

  public GroupedTestRunner(Class<?> klass) throws Throwable {
    super(klass, Collections.<Runner> emptyList());

    // do grouping things here
    runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), "group1"));
    runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), "group2"));
  }

  @Override
  protected List<Runner> getChildren() {
    return runners;
  }
}

This produces output like (in Eclipse):

enter image description here

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • The result looks great, but my problem is that I have only one test, which I wouldn't like to repeat. The test returns multiple results which I'd like to group together and create a multiple suite junit test report which shows them. Do you think it's possible to do? – uzilan Feb 22 '12 at 08:21