testng.xml文件有两种生成方法:
- 快速生成:选择项目,右击TestNG,选择convert to TestNG,勾选Generate testng.xml,testng.xml中包括TestNG中的类,右键Run As 1 TestNG Suite即可。
testng.xml代码如下:
<?xml version = "1.0" encoding = "UTF-8">
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite">
<test name = "Test">
<classes>
<class name = "Selenium_Java.NewTest"/>(此处是包名+类名)
</classes>
</test>
</suite>
- 创建testng.xml
(1)首先要声明一个suite的名字,用于描述将要运行的测试脚本集。
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name = "SuiteName" verbose = "1">(verbose是命令行信息的打印等级)<test name = "TestName">
(2)选择测试脚本的几种分类:
包:
<package>
<package name = "packageName" />
</package>
类:
<classes>
<class name = "className" />
</classes>
方法:
<classes>
<class name = "className" />
<methods>
<include name = "methodName" />
</methods>
</class>
</classes>
一个测试脚本可以属于很多组,只要一个组被include标签标注,那么它就会被运行。
exclude标注的不会运行。
使用方式如下:
<groups>
<run>
<include name = "includedGroupName" />
<exclude name = "excludedGroupName" />
</run>
</groups>
xml文件样例
(1) 选择一个包中的全部测试脚本(包含子包)
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="First suite" verbose="1" >
<test name = "allTestsInAPackage" ><packages><package name = "whole.path.to.package.* /></packages>
</test>
</suite>
(2) 选择一个类中的全部测试脚本
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Second suite" verbose="1" >
<test name = "allTestsInAClass" ><classes><class name="whole.path.to.package.className /></classes>
</test>
</suite>
3)选择一个类中的部分测试脚本
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Third suite" verbose="1" >
<test name = "aFewTestsFromAClass" ><classes><class name="whole.path.to.package.className ><methods><include name = "firstMethod" /><include name = "secondMethod" /><include name = "thirdMethod" /></methods></class></classes>
</test>
</suite>
4)选择一个包中的某些组
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Fourth suite" verbose="1" >
<test name = "includedGroupsInAPackage" ><groups><run><include name = "includedGroup" /></run></groups><packages><package name = "whole.path.to.package.* /></packages>
</test>
</suite>
5)排除一个包中的某些组
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Fifth suite" verbose="1" >
<test name = "excludedGroupsInAPackage" ><groups><run><exclude name = "excludedGroup" /></run></groups><packages><package name = "whole.path.to.package.* /></packages>
</test>
</suite>
参考博客:https://www.cnblogs.com/wangyinghao/p/10133109.html