教你解决为什么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 的数据源 驱动測试 代码与配置
假设我忍让。
别觉得我退缩。由于我明确。忍一忍风平浪静,让一让天高海阔。