1 运行环境:
使用jdk1.5,并且需要引入testng-4.7-jdk15.jar
2 编写测试类:
TestNg的测试类不需要继承任何类或实现特定接口,并且对测试方法没有任何命名限制,只需要在要测试的方法前,加上@Test标记即可,testng用assert后边加返回boolean型数据的表达式来判断是否测试通过。
例如:
2定义分组:
在@Test中有一个groups的属性,该属性可以将测试分组,testng支持将一个测试方法同时分到多个组中,例如上边代码中的@Test(groups = { "all","trade" }),就分别分入all和trade组中
3初始化和释放资源:
可以使用@Configuration标记,来定义初始化和释放资源,该标记的属性有:
beforeSuite=true,所修饰的方法将在测试套件(也就是配置文件中的Suite Tag)中任何一个方法调用之前,调用一次
afterSuite=true,所修饰的方法将在测试套件中所有方法都调用过后,调用一次
beforeTest=true,在测试用例(配置文件中Test Tag)中任何一个测试方法调用之前,调用一次
afterTest=true, 在测试用例中任何所有方法都调用之后,调用一次
beforeTestClass=true,在测试类中任何测试方法调用之前,调用一次
afterTestClass=true,在这个测试类中所有方法都调用过后,调用一次
beforeTestMethod=true,在每个测试方法调用之前,调用一次
afterTestMethod=true,在每个测试方法调用之后,调用一次
例如:
4配置文件:
TestNG,所有必需的数据都集中在 testng.xml 文件中。不需要额外的 TestSuite 文件和构建文件。
suite为根结点,里边可以包含多个test结点,每个test结点为一个测试集合,可在groups结点中run中的include中指定可以指定运行哪个分组,可以通过packages指定需要测试的哪些测试用例,或者可以通过classes指定需要测试哪个测试用例,clssses中可以包含多个class
5运行测试:
可以使用eclipse中的testng插件直接在eclipse中运行,也可以使用ant运行:编写build.xml:
6参数传递:
TestNg,允许向被测试的方法中传递参数,需要传递的参数,需要在testng.xml 文件中声明和赋值:
<parameter name="hachuRecNum" value="10470"/>
<parameter name="testPara" value="testStr"/>
在使用时,需要在被测试的方法前使用@Parameters标记
({ "hachuRecNum","testPara" })就是在testng.xml中定义的name,被测试的方法,有多少个参数,@Parameters后就要有多少个参数,@Parameters后的参数,依次赋值给方法中的参数。
运行上边的代码,就会看见以下结果:
[testng] hachuRecNum:10470
[testng] testPara:testStr
7异常处理
TestNg,可以使用@ExpectedExceptions忽略指定的异常:
例如:
运行上边的代码,虽然代码中抛出了IllegalArgumentException,但是测试结果依然通过:
[testng] ===============================================
[testng] cx_test
[testng] Total tests run: 1, Failures: 0, Skips: 0
[testng] ===============================================
如果去掉,第二行的@ExpectedExceptions(IllegalArgumentException.class),运行后则得到以下结果
[testng] ===============================================
[testng] cx_test
[testng] Total tests run: 1, Failures: 1, Skips: 0
[testng] ===============================================
8执行顺序
可以使用dependsOnMethods = { "方法名" },来指定在什么方法之后运行
例如: @Test(groups = { Constant.ALL, Constant.TRADE, Constant.ZHANGYI }, dependsOnMethods = { "saveToBasket" })
使用jdk1.5,并且需要引入testng-4.7-jdk15.jar
2 编写测试类:
TestNg的测试类不需要继承任何类或实现特定接口,并且对测试方法没有任何命名限制,只需要在要测试的方法前,加上@Test标记即可,testng用assert后边加返回boolean型数据的表达式来判断是否测试通过。
例如:
publicclass
ShinkiFukusuActionTestng
{
@Test(groups={"all","trade"})
publicvoidtestGetDisplayData(){
ShinkiFukusuDisplayInForminForm=newShinkiFukusuDisplayInForm();
inForm.setCustCode(UserManager.getInitCustCode());
inForm.setSessionID(UserManager.getInitSessionId());
inForm.setGengetsu(200208);
inForm.setShijoCode((byte)34);
inForm.setShohinCode((byte)17);
ShinkiFukusuActionact<wbr>ion</wbr>=newShinkiFukusuAction();
intreturnCode=act<wbr>ion.getDisplayData(inForm).getReturnCode();<br><img alt="TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!" align="top" src="https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif"></wbr>assert(returnCode==0);
}
}
{
@Test(groups={"all","trade"})
publicvoidtestGetDisplayData(){
ShinkiFukusuDisplayInForminForm=newShinkiFukusuDisplayInForm();
inForm.setCustCode(UserManager.getInitCustCode());
inForm.setSessionID(UserManager.getInitSessionId());
inForm.setGengetsu(200208);
inForm.setShijoCode((byte)34);
inForm.setShohinCode((byte)17);
ShinkiFukusuActionact<wbr>ion</wbr>=newShinkiFukusuAction();
intreturnCode=act<wbr>ion.getDisplayData(inForm).getReturnCode();<br><img alt="TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!" align="top" src="https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif"></wbr>assert(returnCode==0);
}
}
2定义分组:
在@Test中有一个groups的属性,该属性可以将测试分组,testng支持将一个测试方法同时分到多个组中,例如上边代码中的@Test(groups = { "all","trade" }),就分别分入all和trade组中
3初始化和释放资源:
可以使用@Configuration标记,来定义初始化和释放资源,该标记的属性有:
beforeSuite=true,所修饰的方法将在测试套件(也就是配置文件中的Suite Tag)中任何一个方法调用之前,调用一次
afterSuite=true,所修饰的方法将在测试套件中所有方法都调用过后,调用一次
beforeTest=true,在测试用例(配置文件中Test Tag)中任何一个测试方法调用之前,调用一次
afterTest=true, 在测试用例中任何所有方法都调用之后,调用一次
beforeTestClass=true,在测试类中任何测试方法调用之前,调用一次
afterTestClass=true,在这个测试类中所有方法都调用过后,调用一次
beforeTestMethod=true,在每个测试方法调用之前,调用一次
afterTestMethod=true,在每个测试方法调用之后,调用一次
例如:
@Configuration(beforeSuite=true,groups={"all"}
)
protectedvoidbeforeSuite()throws Exception
{
SysInit.initSystem();
}
protectedvoidbeforeSuite()throws Exception
{
SysInit.initSystem();
}
4配置文件:
TestNG,所有必需的数据都集中在 testng.xml 文件中。不需要额外的 TestSuite 文件和构建文件。
<!DOCTYPEsuiteSYSTEM"http://beust.com/testng/testng-1.0.dtd">
<suitename="cx_test">
<testname="all">
<groups>
<run>
<includename="all"/>
</run>
</groups>
<packages>
<packagename="test.com.livedoor.cx.*"/>
</packages>
</test>
<testname="trade">
<groups>
<run>
<includename="trade"/>
</run>
</groups>
<classes>
<classname="test.fukusu.act<wbr>ion.ShinkiFukusuActionTest"</wbr>>
</class>
</classes>
</test>
</suite>
<suitename="cx_test">
<testname="all">
<groups>
<run>
<includename="all"/>
</run>
</groups>
<packages>
<packagename="test.com.livedoor.cx.*"/>
</packages>
</test>
<testname="trade">
<groups>
<run>
<includename="trade"/>
</run>
</groups>
<classes>
<classname="test.fukusu.act<wbr>ion.ShinkiFukusuActionTest"</wbr>>
</class>
</classes>
</test>
</suite>
suite为根结点,里边可以包含多个test结点,每个test结点为一个测试集合,可在groups结点中run中的include中指定可以指定运行哪个分组,可以通过packages指定需要测试的哪些测试用例,或者可以通过classes指定需要测试哪个测试用例,clssses中可以包含多个class
5运行测试:
可以使用eclipse中的testng插件直接在eclipse中运行,也可以使用ant运行:编写build.xml:
<propertyname="test.report"value="./report/test_ng"/>
<taskdefclasspathref="classpath"name="testng"classname="com.beust.testng.TestNGAntTask"/>
<targetname="test_ng">
<deletedir="${test.report}"quiet="true"/>
<mkdirdir="${test.report}"/>
<testngclasspathref="classpath"outputDir="${test.report}"haltOnfailure="false">
<xmlfilesetdir="./"includes="testng.xml"/>
</testng>
</target>
<taskdefclasspathref="classpath"name="testng"classname="com.beust.testng.TestNGAntTask"/>
<targetname="test_ng">
<deletedir="${test.report}"quiet="true"/>
<mkdirdir="${test.report}"/>
<testngclasspathref="classpath"outputDir="${test.report}"haltOnfailure="false">
<xmlfilesetdir="./"includes="testng.xml"/>
</testng>
</target>
6参数传递:
TestNg,允许向被测试的方法中传递参数,需要传递的参数,需要在testng.xml 文件中声明和赋值:
<parameter name="hachuRecNum" value="10470"/>
<parameter name="testPara" value="testStr"/>
在使用时,需要在被测试的方法前使用@Parameters标记
@Test(groups={"test"}
)
@Parameters({"hachuRecNum","testPara"} )
publicvoidtestGetDisplayData(int arg1,Stringarg2)
{
System.out.println("hachuRecNum:"+arg1);
System.out.println("testPara:"+arg2);
assert(0==0);
}
@Parameters({"hachuRecNum","testPara"} )
publicvoidtestGetDisplayData(int arg1,Stringarg2)
{
System.out.println("hachuRecNum:"+arg1);
System.out.println("testPara:"+arg2);
assert(0==0);
}
({ "hachuRecNum","testPara" })就是在testng.xml中定义的name,被测试的方法,有多少个参数,@Parameters后就要有多少个参数,@Parameters后的参数,依次赋值给方法中的参数。
运行上边的代码,就会看见以下结果:
[testng] hachuRecNum:10470
[testng] testPara:testStr
7异常处理
TestNg,可以使用@ExpectedExceptions忽略指定的异常:
例如:
@Test(groups={"test"}
)
@ExpectedExceptions(IllegalArgumentException.class )
publicvoid testGetDisplayData()
{
if(1==1){
thrownewIllegalArgumentException("testexception");
}
asserttrue;
}
@ExpectedExceptions(IllegalArgumentException.class )
publicvoid testGetDisplayData()
{
if(1==1){
thrownewIllegalArgumentException("testexception");
}
asserttrue;
}
运行上边的代码,虽然代码中抛出了IllegalArgumentException,但是测试结果依然通过:
[testng] ===============================================
[testng] cx_test
[testng] Total tests run: 1, Failures: 0, Skips: 0
[testng] ===============================================
如果去掉,第二行的@ExpectedExceptions(IllegalArgumentException.class),运行后则得到以下结果
[testng] ===============================================
[testng] cx_test
[testng] Total tests run: 1, Failures: 1, Skips: 0
[testng] ===============================================
8执行顺序
可以使用dependsOnMethods = { "方法名" },来指定在什么方法之后运行
例如: @Test(groups = { Constant.ALL, Constant.TRADE, Constant.ZHANGYI }, dependsOnMethods = { "saveToBasket" })