2012-02-21 5 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でこのテストを実行するときにそれらを一緒にグループ化が見込ま - しかしケースはないthatsの。私は何が欠けていますか?それも可能ですか?

+0

テストの失敗を起こすのと同じ順序で印刷されます。あなたは何を期待していましたか? – Perception

+0

私はそれらをスイートで一緒にグループ化することを期待しました。それはcreateSuiteDescription()とaddChild()が何をするためのものなのでしょうか? – uzilan

答えて

0

あなたはこのように、JUnitのテストスイートを使用することができます:あなたがやりたいように見えるので、フォローするテンプレートは、Parameterizedある

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 Tutorial - Suite Test](http://www.asjava.com/junit/junit-tutorial-suite-test/) –

+0

確かに、私はこれらのスイートをランナー。それは可能ではありませんか? – uzilan

+0

これは、一緒にグループ化するいくつかのテストがある場合にスイートを使用する方法です。しかし、これは私の場合ではありません - 私は1つだけのテストを持っていますが、ランナーを使ってグループ化したい複数の結果があります。 – uzilan

0

。それがテストメソッドを複数回実行します与えられたテストクラスの場合、それは私がしたいと思うものですパラメータの各セットのための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

結果は素晴らしいように見えますが、私の問題は、私は繰り返したくないというテストが1つしかないことです。このテストでは複数の結果が返されます。それらをまとめて、それらを示す複数のスイートのjunitテストレポートを作成します。それができると思いますか? – uzilan

関連する問題