【问题标题】:How to programmatically run all test cases forever?如何以编程方式永远运行所有测试用例?
【发布时间】:2018-05-06 14:09:12
【问题描述】:

如何以编程方式永远运行所有测试用例?下面是我的设置......现在我必须无限运行这段代码......另一个要求是我需要在@aftersuite之后再次重新启动应用程序......我需要拆除测试用例,因为我需要在每个aftersuite之后生成报告......

例如:

public class SimpleTest extends TestBase
{
    AppiumDriver driver;

    @BeforeSuite
    public void setUp() throws MalformedURLException {

       // DesiredCapabilities and all setup
    }
    @Test(priority = 1)
    public void testcase1()throws InterruptedException {
        login();
    }

    @Test(priority = 2)
    public void testcase2() throws InterruptedException {
        //something
    }

    @Test(priority = 3)
    public void testcase3() throws InterruptedException {
      //something
    }

    @Test(priority = 4)
    public void testcase4 throws InterruptedException {
     //something
    }

    @Test(priority = 5)
    public void testcase() throws InterruptedException{
      //something
    }

    @Test(priority = 6)
    public void testcase6() throws InterruptedException{
     //something
    }

    @Test(priority = 7)
    public void testcase_logout() throws InterruptedException {
        logout();
    }

    @AfterSuite
    public void testCaseTearDown()
    {
        driver.quit();
    }
}

【问题讨论】:

  • ...使用无限循环?
  • 你能用代码给我建议吗?以上是我的设置配置。
  • 无限循环调用testng类中的run()方法。请参阅文档中的示例 - testng.org/doc/…

标签: java selenium testing testng appium


【解决方案1】:

创建另一个调用这些测试的类...

public SimpleTest {
    public static void main(String[] args) {
         while (true) {
             org.junit.runner.JUnitCore.main("SimpleTest");
         }
    }
}

【讨论】:

  • Line with JUnitCore 在代码中现在将调用测试。您可以像示例中那样将其放在 main 中,或者将其放在带有 @Before 标记的方法内的循环中,以便首先调用它或在您需要调用它的任何地方调用它。希望这会有所帮助。
  • 我没明白你的意思.. 我如何在@BeforeSuite 方法中使用它?
【解决方案2】:

在 testNG.xml 中安排你的测试。

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Regression suite" verbose="1" >
  <test name="SampleTests" >
    <classes>
      <class name="com.first.example.SampleTest"/>
    </classes>
 </test>
</suite>

然后使用 TestNG 对象以编程方式运行它们。

import org.testng.TestNG;

import java.util.List;

public class RunTestNGXML {

    public static void main(String[] args) {
        TestNG testng = new TestNG();
        List<String> suites = Lists.newArrayList();
        suites.add("testng.xml");
        testng.setTestSuites(suites);
        String outputDir ="src"+File.separator+"reports";
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");

        while(true){
        testng.setOutputDirectory(outputDir+File.separator+formater.format(calendar.getTime()));
        testng.run();
         }
    }
}

无限循环的警告是,您的电子邮件报告将在每次迭代中被覆盖。因此,使用时间戳配置测试输出目录路径。否则 log4j appender 可以轻松完成连续记录的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多