TestNG方法測试及注意要点 代码及配置具体解释(解决testng方法不运行问题)

教你解决为什么TestNG中方法加了@Test注解,也在配置文件里配置了,可是方法就是不运行!


在使用TestNG进行測试时,使用配置文件的方式更easy于维护。可是常常遇到明明方法写了也配置运行了,可是run的时候代码就没有运行

看代码:(细致看凝视!

/**
 * 
 * <p>
 * Title: TestngMethods
 * </p>
 * 
 * <p>
 * 相应配置文件testng-methods.xml
 * Description: Testng的methods測试及配置,參考testng-methods.xml,假设不设置
 * exclude和include,默认运行当前測试类时,带有返回值的方法不会被运行
 * 
 * 假设想运行多个同类型或者命名方式相似的多个方法时,能够用法组測试,
 * '.*'表示一个或多个字符,假设方法命名方式不同,那么能够採用组測试方法进行測试,參考TestGroups
 * 
 * 注:文档错误!
 * 
 * 5.1 - Test methods Test methods are annotated with @Test. Methods annotated
 * with @Test that happen to return a value will be ignored, unless you set
 * allow-return-values to true in your testng.xml:
 * 
 * <suite allow-return-values="true">
 * 
 * or
 * 
 * <test allow-return-values="true">
 * 
 * 此处在<test >中配置allow-return-values属性无效,測试依然不会被运行
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author : Dragon
 * 
 * @date : 2014年10月11日
 */
public class TestngMethods {
	/**
	 * 默认情况下这种方法将被忽略,假设须要运行,须要在xml中配置allow-return-values="true"
	 * 
	 * @return
	 */
	@Test
	public String getName() {
		System.err.println("return name.... getName()");
		return "name";
	}

	@Test
	public void funtest() {
		System.err.println("this is funtest......");
	}

	@Test
	public void saveMethod1() {
		System.err.println("this is saveMethod1......");
	}

	@Test
	public void saveMethod2() {
		System.err.println("this is saveMethod2......");
	}

	@Test
	public void saveMethod3() {
		System.err.println("this is saveMethod3......");
	}

}

配置文件:testng-methods.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!-- allow-return-values 默认值为FALSE,表示返回值将被忽略 -->
<suite name="framework_testng" allow-return-values="true">
	<test verbose="2" name="TestMethods">
		<classes>
			<class name="com.dragon.testng.annotation.TestngMethods">
				<methods>
					<exclude name="funtest"></exclude>
					<include name="getName"></include>
					<include name="save.*"></include>
				</methods>
			</class>
		</classes>
	</test>
</suite>


执行结果:

return name.... getName()
this is saveMethod1......
this is saveMethod2......
this is saveMethod3......
PASSED: getName
PASSED: saveMethod1
PASSED: saveMethod2
PASSED: saveMethod3

===============================================
    TestMethods
    Tests run: 4, Failures: 0, Skips: 0
===============================================


Testng 的数据源 驱动測试 代码与配置



假设我忍让。
别觉得我退缩。由于我明确。忍一忍风平浪静,让一让天高海阔。

转载于:https://www.cnblogs.com/yxysuanfa/p/7337597.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值