简体   繁体   English

Junit4 Test Suite-从测试套件动态循环并添加测试用例

[英]Junit4 Test Suite - Looping and adding test cases dynamically from test suite

I have an XML file with many models. 我有一个包含许多模型的XML文件。 I'm parsing this XML file in Test Suite and reading each model. 我正在测试套件中解析此XML文件并读取每个模型。 For each model, I want to execute specific list of test cases. 对于每个模型,我要执行测试用例的特定列表。

That means, inside a FOR loop in test suite, I have to add test cases dynamically. 这意味着,在测试套件的FOR循环中,我必须动态添加测试用例。


public static TestSuite suite() {
TestSuite suite = new TestSuite();
for(condtion){
    if(model1){
         suite.addTest(new JUnit4TestAdapter(TestCase1.class));
         suite.addTest(new JUnit4TestAdapter(TestCase2.class));
     }
     elseif(model2){
         suite.addTest(new JUnit4TestAdapter(TestCase3.class));
         suite.addTest(new JUnit4TestAdapter(TestCase4.class));
     }
  }
return suite;
}

But in this scenario, only the test cases for last model in XML is run. 但是在这种情况下,仅运行XML中最后一个模型的测试用例。 It is not dynamically adding test cases and running it for all models. 它不是动态添加测试用例并为所有模型运​​行它。

Then I tried with 然后我尝试了

JUnitCore.runClasses(TestScoopixModelLoader.class); JUnitCore.runClasses(TestScoopixModelLoader.class);

inside the for loop. 在for循环中。 I this scenario, test cases are run dynamically. 在这种情况下,测试用例是动态运行的。 But I'm not getting that visual effect in eclipse.(ie GREEN bar for success, RED bar for failure). 但是我没有在日食中获得那种视觉效果。(例如,绿色代表成功,红色代表失败)。 This visual effect is not working for Junit TEST RUNNER CLASS 此视觉效果不适用于Junit TEST RUNNER CLASS

Please suggest me a solution if any one have faced this kind of issue. 如果有人遇到这种问题,请给我建议解决方案。

Add your own listener and let it print the results (in a textfile, sysout, etc) 添加您自己的侦听器,并让其打印结果(在文本文件,sysout等文件中)

JUnitCore jCore = new JUnitCore();
jCore.addListener(new CustomListener()) 
JUnitCore.runClasses(TestScoopixModelLoader.class);


 public class CustomListener extends RunListener {
    public void testFailure(Failure failure) {
       System.out.println(failure);
    }

// Rest of the listener methods

 }

Listener api: http://junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html 侦听器api: http : //junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM