0

junit APIを使用して一般的なテストをグループ化する junit Runner を作成しようとしています。

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 で実行すると、それらがグループ化されることを期待していましたが、そうではありませんでした。私は何が欠けていますか?それは可能ですか?

4

2 に答える 2

0

従うべきテンプレートはParameterizedです。特定のテストクラスに対して、テストメソッドを複数回実行し、パラメーターのセットごとにランナーを作成します。これは、あなたが望むものだと思います:

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 で) 次のような出力が生成されます。

ここに画像の説明を入力

于 2012-02-22T00:32:34.663 に答える
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 {

}
于 2012-02-21T21:44:22.870 に答える