TestNG introductory tutorial -12-Java code execution failed testng.xml and re-run

  We are right in front of testng.xml file to run testng use cases in the IDEA, in this process is to write test cases can do so, however, if the test finished, but also to do it? Is there any way, such as automation to achieve. Post-maintenance test scripts, we will consider continuous integration testing, how to automate test cases to run it? Online search, many of which are testng in cmd command line input, but this is flawed, first you need javac command to compile test cases before using this command, if you use a lot of cases, you realize how batch in cmd compile it? This, we are to solve this problem, we do not care about TestNG environment variables and compiler of things to prepare for the future of continuous integration.

Prepare a use case TestNG

package demo2;
 
import org.testng.Assert;
import org.testng.annotations.Test;
 
/**
 * create by Anthony on 2017/11/18
 */
public class ReRunFailedTestCaseDemo {
 
    @Test
    public void test01(){
        System.out.println("test01");
    }
 
    @Test(dependsOnMethods = "test03", alwaysRun = true)
    public void test02(){
        System.out.println("test02");
    }
 
    @Test
    public void test03(){
        Assert.assertTrue(10==11);
        System.out.println("test03");
    }
 
    @Test(dependsOnMethods = "test05", alwaysRun = true)
    public void test04(){
        System.out.println("test04");
    }
 
    @Test
    public void test05(){
        Assert.assertTrue(12==13);
        System.out.println("test05");
    }
 
}

   Notice above I deliberately wrote two use cases will fail to run for re-run following the failure to prepare. Here we testng.xml follows in the project root directory.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">

    <test name="Testng_learn" >

        <classes>
            <class name="demo2.ReRunFailedTestCaseDemo"/>
        </classes>

    </test>
</suite>

1. How to display the report output after running testng IDEA tool

       in the tool IDEA, run a complete use case or testng.xml testng default file in the current project is no testng the report output, this eclipse is in default open automatically, so how, we must first learn to open this under IDEA.

Select a TestNG use case file, right-click the Run-Edit Configuration ...

 

 
运行步骤1中的testng.xml,你会发现当前项目根目录下会生成test-output文件夹

       这个文件夹下有我们上一次运行的html格式报告,还有跑失败的用例的xml文件,本文重点是testng-failed.xml文件。找到这个文件,看看是不是步骤1中我设置的两个运行失败的用例。

        在TestNG中会自动记录你上一次运行情况,在test-output下会生成html格式报告,和把失败的用例都放在testng-failed.xml,这个功能很强大。由于有了这个,我们就想,可不可以实现失败用例从新跑呢。我们手动执行testng-failed.xml当然可以去实现失败用例重新跑的需求,所以,我们重点在如何实现自动化手段去失败重新跑。把这个问题再细化,就是如何用Java代码是加载执行testng.xml文件。

2. 准备一个TestNG用例Java编程去实现运行相关testng.xml文件

       这里,我们来解决如何利用Java代码来实现运行我们想要的testng.xml文件。在testng官网教程上解释了如何java代码运行用例,但是没有介绍如何运行testng.xml。我们,写一个java类文件,第一个先执行项目根目录下的testng.xml文件,然后等执行结束,然后去执行test-output文件下testng-failed.xml文件,这样就达到了自动运行失败的用例的需求。

    package demo2;
     
    import org.testng.TestNG;
    import java.util.ArrayList;
    import java.util.List;
     
    /**
     * create by Anthony on 2017/11/18
     */
    public class RunTestNG {
     
        public static void main(String[] args) throws InterruptedException {
     
              TestNG testNG = new TestNG();
              List<String> suites = new ArrayList<String>();
              suites.add(".\\testng.xml");
              //suites.add(".\\test-output\\testng-failed.xml");
              testNG.setTestSuites(suites);
              testNG.run();
     
              // 等待执行结束,然后去执行失败用例
              TestNG testNG1 = new TestNG();
              List<String> suites1 = new ArrayList<String>();
              Thread.sleep(5000);
              suites1.add(".\\test-output\\testng-failed.xml");
              testNG1.setTestSuites(suites1);
              testNG1.run();
     
     
            }
    }



看控制台运行结果:

[TestNG] Running:

 C:\Users\Administrator\IdeaProjects\Java_Learn\testng.xml

 

test01

test02

test04

 

===============================================

Default Suite

Total tests run: 5, Failures: 2, Skips: 0

===============================================

 

[TestNG] Running:

 C:\Users\Administrator\IdeaProjects\Java_Learn\test-output\testng-failed.xml

 

===============================================

Failed suite [Default Suite]

Total tests run: 2, Failures: 2, Skips: 0

===============================================

 

 

Process finished with exit code 0

       看控制台日志,显示先运行了testng.xml,然后在运行testng-failed.xml,所以,达到了我想要的目的。以上,重点是如何Java代码是实现testng.xml文件的运行用例,至于失败重跑,上面是我个人的方法,网上还有一种方法,是修改Testng监听类然后写一个重跑的方法来实现,我个人认为没有这个必要,如果后面我们介绍了监听,然后在来看看这第二个方法的实现过程,到时候我们再和这么内容对比,然后选择你认为适合你项目的失败重跑方案。

       提示:知道了如何运行一个testng.xml文件,那么就知道如何运行几百条测试用例。举例,我们有一个webui自动化项目,有几百条测试用例。一般我们都一个模块用一个testng.xml文件来进行管理或者一个testng.xml文件管理多个模块或者全量测试用例。然后写一个main方法,然执行测试的人员选择不同模块的代号,通过case语句,我们就可以实现去调用执行不同模块下的testng.xml文件,这个过程完全可以放到Jenkins上做或者cmd命令行。写过很多脚本的人,应该明白我的思路,有不明白的也没关系,慢慢去学习,去积累。


原文链接:https://blog.csdn.net/u011541946/article/details/78597258

Guess you like

Origin www.cnblogs.com/peachh/p/12071113.html