代码执行testng的几种方式

一、指定test class

Test class FirstTest

package cas.eric;


import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class FirstTest {
    @Test
    @Parameters({"name"})
    public void test_one(@Optional("zhangsan") String name){
        System.out.println("test one");
        //如果变量name有值则打印传入的name值,如果没有则打印默认值zhangsan
        System.out.println(name);
    }
}

 

 

执行testng的class:TestngRun

package cas.eric;


import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestngRun {

    public static void main(String[] args) {
        //新建一个XmlSuite对象
        XmlSuite xmlSuite = new XmlSuite();
        //指定xmlsuite 名称
        xmlSuite.setName("xml test suite");
        //指定name 参数值
        Map<String,String> parameters = new HashMap<>();
        parameters.put("name", "lisi");
        //xmlsuite添加parameters
        xmlSuite.setParameters(parameters);
        //新建一个XmlTest对象
        XmlTest xmlTest = new XmlTest(xmlSuite);
        //设置xmltest 名称
        xmlTest.setName("xml test");
        //新建xmlclass 对象
        List<XmlClass> classes = new ArrayList<>();
        String packageName = TestngRun.class.getPackage().getName();
        classes.add(new XmlClass(packageName+".FirstTest"));
        xmlTest.setXmlClasses(classes);
        List<XmlSuite> suites = new ArrayList<>();
        suites.add(xmlSuite);
        TestNG testNG = new TestNG();
        testNG.setXmlSuites(suites);
        testNG.run();
    }

}

 

打印结果:

test one
lisi

二、指定xml文件

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="test">
        <classes>
            <class name="cas.eric.FirstTest"></class>
        </classes>
    </test>
</suite>

 

TestngRun.java

 1 package cas.eric;
 2 
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.testng.TestNG;
 6 import java.io.File;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.util.ArrayList;
10 import java.util.List;
11 
12 public class TestngRun {
13 
14     public static void main(String[] args) {
15         try {
16             String xmlName = "testng.xml";
17             //读取文件到流
18             InputStream inputStream = TestngRun.class.getClassLoader().getResourceAsStream(xmlName);
19             //新建一个文件对象
20             File xmlFile = new File("name.xml");
21             //拷贝流的内容到新文件中
22             FileUtils.copyInputStreamToFile(inputStream,xmlFile);
23             TestNG testNG = new TestNG();
24             List<String> suites = new ArrayList<>();
25             //添加文件到suite
26             suites.add(xmlFile.getAbsolutePath());
27             testNG.setTestSuites(suites);
28             testNG.run();
29         }
30         catch (IOException e) {
31             e.printStackTrace();
32         }
33     }
34 
35 }

 

执行结果:

test one
zhangsan

打印了默认的name:zhangsan,那使用xml方式执行的时候如何传入参数呢

 

三、执行xml文件以及自定义传参

TestngRun.java

 

 1 package cas.eric;
 2 
 3 
 4 
 5 import org.testng.TestNG;
 6 import org.testng.xml.SuiteXmlParser;
 7 import org.testng.xml.XmlSuite;
 8 import java.io.InputStream;
 9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 
14 public class TestngRun {
15 
16     public static void main(String[] args) {
17         String xmlName = "testng.xml";
18         InputStream inputStream = TestngRun.class.getClassLoader().getResourceAsStream(xmlName);
19         TestNG testNG = new TestNG();
20         SuiteXmlParser suiteXmlParser = new SuiteXmlParser();
21         List<XmlSuite> suites = new ArrayList<>();
22         XmlSuite xmlSuite = suiteXmlParser.parse(xmlName, inputStream, true);
23         suites.add(xmlSuite);
24         Map<String,String> parameters = new HashMap<>();
25         parameters.put("name", "wangwu");
26         xmlSuite.setParameters(parameters);
27         testNG.setXmlSuites(suites);
28         testNG.run();
29         
30     }
31 
32 }

 

执行结果:

test one
wangwu

 

当然也可以自定义test class

SecondTest.java

 1 package cas.eric;
 2 
 3 
 4 import org.testng.annotations.Optional;
 5 import org.testng.annotations.Parameters;
 6 import org.testng.annotations.Test;
 7 
 8 public class SecondTest {
 9 
10     @Test
11     @Parameters({"name"})
12     public void test_two(@Optional("mike") String name){
13         System.out.println("test two");
14         //如果变量name有值则打印传入的name值,如果没有则打印默认值zhangsan
15         System.out.println(name);
16     }
17 }

 

TestngRun.java

 1 package cas.eric;
 2 
 3 
 4 
 5 import org.testng.TestNG;
 6 import org.testng.xml.SuiteXmlParser;
 7 import org.testng.xml.XmlClass;
 8 import org.testng.xml.XmlSuite;
 9 import org.testng.xml.XmlTest;
10 
11 import java.io.InputStream;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 
17 public class TestngRun {
18 
19     public static void main(String[] args) {
20         String xmlName = "testng.xml";
21         InputStream inputStream = TestngRun.class.getClassLoader().getResourceAsStream(xmlName);
22         TestNG testNG = new TestNG();
23         SuiteXmlParser suiteXmlParser = new SuiteXmlParser();
24         List<XmlSuite> suites = new ArrayList<>();
25         XmlSuite xmlSuite = suiteXmlParser.parse(xmlName, inputStream, true);
26         List<XmlTest> xmlTestList = xmlSuite.getTests();
27         XmlTest xmlTest = xmlTestList.get(0);
28         List<XmlClass> xmlClassList = xmlTest.getXmlClasses();
29         String packageName = TestngRun.class.getPackage().getName();
30         XmlClass xmlClass = new XmlClass(packageName+".SecondTest");
31         xmlClassList.add(xmlClass);
32         suites.add(xmlSuite);
33         Map<String,String> parameters = new HashMap<>();
34         parameters.put("name", "wangwu");
35         xmlSuite.setParameters(parameters);
36         testNG.setXmlSuites(suites);
37         testNG.run();
38 
39     }
40 
41 }

 

运行结果:

test one
wangwu
test two
wangwu

 

posted @ 2019-06-22 13:59  弘文馆校书  阅读(2346)  评论(0编辑  收藏  举报