2012-02-21 2 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 

는 또한, 내가 이클립스에서이 테스트를 실행할 때 그들이 함께 그룹화 할 전망 - 그러나 케이스를하지 이잖아. 내가 뭘 놓치고 있니? 심지어 가능할까요?

+0

테스트 실패를 발생시키는 순서대로 출력됩니다. 당신은 무엇을 다르게 기대 했습니까? – Perception

+0

글쎄, 나는 스위트에서 함께 그룹화 된 것을 볼 것으로 예상했다. createSuiteDescription() 및 addChild()가 수행 할 작업이 아닙니까? – uzilan

답변

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

+0

물론, 그 안에있는 스위트를 만들고 싶습니다. 달리는 사람. 가능하지 않니? – uzilan

+0

여러 그룹으로 그룹화하려는 경우이 방법을 사용합니다. 하지만 이것은 내 경우가 아닙니다. 단 하나의 테스트 만 수행 할 수 있지만 여러 결과는 주자를 사용하여 그룹화하려는 것입니다. – uzilan

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

이 (이클립스)와 같은 출력을 생성 :이 시험 방법을 여러 번 실행 주어진 테스트 클래스의 경우, 내가 원하는 생각되는 매개 변수의 각 세트에 대한 Runner를 생성

enter image description here

+0

결과는 좋아 보이지만 내 문제는 단 하나의 테스트가 있다는 것입니다. 반복하지 않으려합니다. 이 테스트는 여러 그룹으로 나뉘어 결과를 반환하고이를 보여주는 여러 스위트 junit 테스트 보고서를 만듭니다. 가능하다고 생각하십니까? – uzilan

관련 문제