2012-02-21 140 views
0

我试图创建一个JUnit运行该集团将利用一起共同测试junit API如何组JUnit测试一起

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); 
     } 
    } 
} 

的问题是,当我使用这个亚军运行测试,我可以看到连续失败,父母和孩子,而不是组合在一起:

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 

而且,我期望看到他们组合在一起,当我运行这个测试在Eclipse - 但那并非如此。我错过了什么?它甚至有可能吗?

+0

它按正确顺序打印出来,以解除测试失败。你对此有什么期待? – Perception 2012-02-21 21:36:27

+0

好吧,我希望看到他们组合在一起。这不就是createSuiteDescription()和addChild()所要做的吗? – uzilan 2012-02-21 21:46:05

回答

0

您可以使用JUnit测试套件,就像这样:

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 { 

} 
+0

在这里查看更多信息 - [JUnit教程 - 套件测试](http://www.asjava.com/junit/junit-tutorial-suite-test/) – 2012-02-21 21:47:25

+0

当然,但我想创建这些套件亚军。这不可能吗? – uzilan 2012-02-21 21:48:05

+0

那么这是你如何使用套件,如果你有几个测试,你想组合在一起。但是,这不是我的情况 - 我只有一个测试,但多个结果,我想用亚军分组。 – uzilan 2012-02-21 22:06:50

0

模板,遵循的是Parameterized,因为它似乎你想要做什么。对于它运行的测试方法多次给定的测试类,它为每一组参数,一个Runner这是我想你想:

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; 
    } 
} 

这将产生输出一样(在Eclipse):

enter image description here

+0

结果看起来不错,但我的问题是我只有一个测试,我不想重复。该测试返回多个结果,我想将它们组合在一起,并创建一个显示它们的多套房junit测试报告。你认为这是可能的吗? – uzilan 2012-02-22 08:21:30