我想運行從ParentRunner
擴展的測試。 我正在學習,而不是任何特定的場景。 我有下面和下面的類也有輸出。 我不明白幾件事: 1.爲什麼「describeChild」重複調用3次? 2.爲什麼測試沒有運行?(「doOne」和「doTwo」)?不知道這一行: // Result result = JUnitCore.runClasses(arg0.getClass()); 正在執行測試,但我不明白爲什麼它會這樣工作? 3.最重要的是 - 那條線: @SuiteClasses({ChildOne.class,ChildTwo.class})? 這對代碼的行爲沒有影響...... 非常感謝任何人誰迴應:-)在Junit中擴展ParentRunner
套房類:
@RunWith(FamilyRunner.class)
@SuiteClasses({ChildOne.class, ChildTwo.class, ChildThree.class})
public class Suite {
//nothing here
}
亞軍類:
public class FamilyRunner extends ParentRunner<ParentClass>{
public FamilyRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Rule
public TestName name = new TestName();
@Override
protected List<ParentClass> getChildren() {
List<ParentClass> list = new ArrayList<>();
System.out.println("Adding items to list");
list.add(new ChildOne());
list.add(new ChildTwo());
return list;
}
@Override
protected Description describeChild(ParentClass arg0) {
System.out.println("describeChild class: " + arg0.getClass().getSimpleName());
Description desc = Description.createTestDescription(name.getMethodName(),
name.getMethodName(), getClass().getAnnotations());
return desc;
}
@Override
protected void runChild(ParentClass arg0, RunNotifier arg1) {
System.out.println("runChild " + arg0.getClass().getSimpleName());
//Result result = JUnitCore.runClasses(arg0.getClass());
}
}
和:
public class ParentClass {
public ParentClass() {
System.out.println("created parent class");
}
}
public class ChildOne extends ParentClass {
public ChildOne() {
System.out.println("created ChildOne class");
}
@Test
public void doOne(){
System.out.println("doOne");
}
}
public class ChildTwo extends ParentClass {
public ChildTwo() {
System.out.println("created ChildTwo class");
}
@Test
public void doTwo(){
System.out.println("doTwo");
}
}
控制檯打印:
Adding items to list
created parent class
created ChildOne class
created parent class
created ChildTwo class
describeChild class: ChildOne
describeChild class: ChildTwo
describeChild class: ChildOne
describeChild class: ChildTwo
describeChild class: ChildOne
describeChild class: ChildTwo
runChild ChildOne
runChild ChildTwo