这一篇我们主要学习如下几个知识点:
⑴关于testng.xml
⑵创建一个测试套件
⑶执行testng.xml
⑷在测试套件中创建多个测试用例
⑸在用例中增加class,packages, method
⑹用例中包含/不包含class,packages, method
⑺⑻⑼⑽
在开始学习这些知识点之前,需要在eclipse中先创建一个java 项目,结构如下:
在每个class文件中增加如下代码
import org.testng.annotations.Test;
public class FirstTestClass {
@Test
public void firstTest(){
System.out.println("First test method");
}
@Test
public void secondTest(){
System.out.println("Second test method");
}
}
其中方法test.thirdpackage.FirstTestClass.firstTest()的注解修改为 @Test (groups={"test-one"}) 【关注testng的注解在后面的文章中会有解释】
1、class-testng.xml 文件用来展示如何只执行class,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Class Suite" verbose="1">
<test name="Test">
<classes>
<class name="test.firstpackage.FirstTestClass"/>
<class name="test.secondpackage.FirstTestClass"/>
<class name="test.thirdpackage.FirstTestClass"/>
</classes>
</test>
</suite>
2、combine-testng.xml文件用来展示如何执行firstpackage,secondpackage.FirstTestClass,thirdpackage.FirstTestClass.firstTest,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Combine Suite" verbose="1">
<test name="Combine Test">
<packages>
<package name="test.firstpackage" />
</packages>
<classes>
<class name="test.secondpackage.FirstTestClass" />
<class name="test.thirdpackage.FirstTestClass" >
<methods>
<include name="firstTest"/>
</methods>
</class>
</classes>
</test>
</suite>
3、exclude-method-testng.xml用来展示如何不包含methods,内容如下;
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Exclude Method Suite" verbose="1">
<test name="Exclude Method Test">
<classes>
<class name="test.firstpackage.FirstTestClass">
<methods>
<exclude name="firstTest" />
</methods>
</class>
</classes>
</test>
</suite>
4、exclude-package-testng.xml用例展示如何不执行某个packages,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Exclude Method Suite" verbose="1">
<test name="Exclude Method Test">
<classes>
<class name="test.firstpackage.FirstTestClass">
<methods>
<exclude name="firstTest" />
</methods>
</class>
</classes>
</test>
</suite>
5、include-package-testng.xml用例展示如何只执行packages,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Include Package Suite" verbose="1">
<test name="Include Package Test">
<packages>
<package name="test.*">
<include name="test.secondpackage"/>
</package>
</packages>
</test>
</suite>
6、method-testng.xml用例展示如何只执行某个方法
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Method Suite" verbose="1">
<test name="Method Test">
<classes>
<class name="test.firstpackage.FirstTestClass">
<methods>
<include name="firstTest"/>
</methods>
</class>
<!-- <class name="test.thirdpackage.FirstTestClass"/> -->
</classes>
</test>
</suite>
7、group-testng.xml 展示了如何执行某个组中的用例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Group Suite" verbose="1">
<test name="Group Test">
<groups>
<run>
<include name="test-one" />
</run>
</groups>
<classes>
<class name="test.thirdpackage.FirstTestClass" />
</classes>
</test>
</suite>
准备好之后,右击xml文件,【Run As】——【TestNG Suite】然后在 eclipse底部的【Result of running suite】中查看每个xml执行的结果吧