testng.xml文件结构组成及节点属性说明

TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.php

更多testng配置及说明,请移步http://testdoc.org/docmaster?pid=111

testng.xml文件结构:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suitename" junit="false" verbose="3" parallel="false" thread-count="5" configfailurepolicy="<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;">skip</span></span>" annotations="javadoc" time-out="10000" skipfailedinvocationcounts="true" data-provider-thread-count="5" object-factory="classname" allow-return-values="true">  <!-- name参数为必须 -->
	<suite-files>
		<suite-file path="/path/to/suitefile1"></suite-file>  <!-- path参数为必须 -->
		<suite-file path="/path/to/suitefile2"></suite-file>
	</suite-files>  
	<parameter name="par1" value="value1"></parameter>  <!-- name, value参数为必须 -->
	<parameter name="par2" value="value2"></parameter>
	<method-selectors>
		<method-selector>
			<selector-class name="classname" priority="1"></selector-class> <!-- name参数为必须 -->
			<script language="java"></script>  <!-- language参数为必须 -->
		</method-selector>
	</method-selectors>
	<test name="testename" junit="false" verbose="3" parallel="false" thread-count="5" annotations="javadoc" time-out="10000" enabled="true" skipfailedinvocationcounts="true" preserve-order="true" allow-return-values="true"> <!-- name参数为必须 -->
		<parameter name="par1" value="value1"></parameter>  <!-- name, value参数为必须 -->
		<parameter name="par2" value="value2"></parameter>
		<groups>
			<define name="xxx"> <!-- name参数为必须 -->
				<include name="" description="" invocation-numbers="" />  <!-- name参数为必须 -->
				<include name="" description="" invocation-numbers="" />
			</define>
			<run>
				<include name="" />  <!-- name参数为必须 -->
				<exclude name="" />  <!-- name参数为必须 -->
			</run>
			<dependencies>
				<group name="" depends-on=""></group> <!-- name,depends-on均为参数为必须 -->
				<group name="" depends-on=""></group>
			</dependencies>
		</groups>
		<classes>
			<class name="classname"> <!-- name参数为必须 -->
				<methods>
					<parameter name="par3" value="value3"></parameter>
					<include name="" description="" invocation-numbers=""></include>
					<exclude name=""></exclude>
				</methods>
				<methods></methods>
			</class>
		</classes>	
		<packages>
			<package name="" />  <!-- name参数为必须 -->
			<package name="">
				<include name="" description="" invocation-numbers=""></include>
				<exclude name=""></exclude>
			</package>
		</packages>
		<listeners>
			<listener class-name="classname1" />  <!-- name参数为必须 -->
			<listener class-name="classname2" />
		</listeners>
	</test>
	<test></test>
</suite> 

testng.xml文件节点属性说明:

suite属性说明:
@name: suite的名称,必须参数    
@junit:是否以Junit模式运行,可选值(true | false),默认"false"
@verbose:命令行信息打印等级,不会影响测试报告输出内容;可选值(1|2|3|4|5) 
@parallel:是否多线程并发运行测试;可选值(false | methods | tests | classes | instances),默认 "false"
@thread-count:当为并发执行时的线程池数量,默认为"5"
@configfailurepolicy:一旦Before/After Class/Methods这些方法失败后,是继续执行测试还是跳过测试;可选值 (skip | continue),默认"skip"
@annotations:获取注解的位置,如果为"javadoc", 则使用javadoc注解,否则使用jdk注解
@time-out:为具体执行单元设定一个超时时间,具体参照parallel的执行单元设置;单位为毫秒
@skipfailedinvocationcounts:是否跳过失败的调用,可选值(true | false),默认"false"
@data-provider-thread-count:并发执行时data-provider的线程池数量,默认为"10"
@object-factory:一个实现IObjectFactory接口的类,用来实例测试对象
@allow-return-values:是否允许返回函数值,可选值(true | false),默认"false"
@preserve-order:顺序执行开关,可选值(true | false) "true"
@group-by-instances:是否按实例分组,可选值(true | false) "false"

test属性说明:
@name:test的名字,必选参数;测试报告中会有体现
@junit:是否以Junit模式运行,可选值(true | false),默认"false"
@verbose:命令行信息打印等级,不会影响测试报告输出内容;可选值(1|2|3|4|5) 
@parallel:是否多线程并发运行测试;可选值(false | methods | tests | classes | instances),默认 "false"
@thread-count:当为并发执行时的线程池数量,默认为"5"
@annotations:获取注解的位置,如果为"javadoc", 则使用javadoc注解,否则使用jdk5注解
@time-out:为具体执行单元设定一个超时时间,具体参照parallel的执行单元设置;单位为毫秒
@enabled:设置当前test是否生效,可选值(true | false),默认"true" 
@skipfailedinvocationcounts:是否跳过失败的调用,可选值(true | false),默认"false"
@preserve-order:顺序执行开关,可选值(true | false) "true"
@group-by-instances:是否按实例分组,可选值(true | false) "false"
@allow-return-values:是否允许返回函数值,可选值(true | false),默认"false"


TestNG 是一个广泛使用的测试框架,支持多种运行方式。如果在使用过程中发现无法运行 `testng.xml` 文件,并且在 IDE 或构建工具中没有看到 "Run" 选项,可能是由于以下几个原因和解决方案: 1. **确保 TestNG 插件已安装** 在 IntelliJ IDEA 中,需要确认已经安装了 TestNG 插件。可以通过以下路径检查:`File > Settings > Plugins`,然后搜索 "TestNG" 并安装插件 [^2]。 2. **正确保存 XML 文件** 在某些情况下,特别是使用 IntelliJ IDEA 时,如果 `testng.xml` 文件未被正确保存,可能不会显示 "Run" 选项。尝试按下 `Ctrl + S` 快捷键保存文件,这通常会触发 IDE 更新其状态并显示 "Run" 选项 。 3. **检查 XML 文件的格式** 确保 `testng.xml` 文件的内容是有效的,即没有语法错误或结构问题。例如,一个基本的 `testng.xml` 文件应该如下所示: ```xml <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Suite1"> <test name="Test1"> <classes> <class name="com.example.TestClass"/> </classes> </test> </suite> ``` 如果 XML 文件存在格式错误,IDE 可能无法识别它为可运行的测试套件 [^1]。 4. **使用 Maven 或 Gradle 构建工具** 如果直接通过 IDE 运行 `testng.xml` 文件仍然存在问题,可以考虑使用构建工具如 Maven 或 Gradle 来执行测试。对于 Maven 项目,可以在 `pom.xml` 中配置 TestNG 插件,并通过命令行运行测试: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> <configuration> <suiteXmlFiles> <file>src/test/resources/testng.xml</file> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> ``` 然后在命令行中执行: ```sh mvn test ``` 5. **从命令行运行 TestNG** 如果上述方法均不可行,可以直接从命令行运行 `testng.xml` 文件。假设 TestNG 已经添加到项目的依赖管理中,可以使用以下命令: ```sh java -cp "path/to/testng.jar:path/to/your/classes" org.testng.TestNG testng.xml ``` 6. **重新导入项目** 有时候,IDE 的缓存可能会导致一些问题。尝试重新导入项目,或者清除缓存后重启 IDE,这有助于解决此类问题。 7. **检查项目结构** 确保 `testng.xml` 文件位于正确的目录下,通常是 `src/test/resources`。如果文件位置不正确,IDE 可能不会将其识别为测试资源 [^1]。 通过以上步骤,应该能够解决 TestNG XML 文件无法运行的问题。如果仍有疑问或遇到其他问题,建议查阅官方文档或社区支持资源。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值