TestNg的使用(testng4.7 for jdk15)

1 运行环境:
使用jdk1.5,并且需要引入testng-4.7-jdk15.jar

2 编写测试类:
TestNg的测试类不需要继承任何类或实现特定接口,并且对测试方法没有任何命名限制,只需要在要测试的方法前,加上@Test标记即可,testng用assert后边加返回boolean型数据的表达式来判断是否测试通过。

例如:
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!publicclass ShinkiFukusuActionTestng
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
{
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!@Test(groups
={"all","trade"}
)
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
publicvoidtestGetDisplayData()
{
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!ShinkiFukusuDisplayInForminForm
=new
ShinkiFukusuDisplayInForm();
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!inForm.setCustCode(UserManager.getInitCustCode());
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!inForm.setSessionID(UserManager.getInitSessionId());
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!inForm.setGengetsu(
200208
);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!inForm.setShijoCode((
byte)34
);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!inForm.setShohinCode((
byte)17
);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!ShinkiFukusuActionact<wbr>ion</wbr>
=new
ShinkiFukusuAction();
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
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);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!}

TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!}

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,在每个测试方法调用之后,调用一次

例如:
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!@Configuration(beforeSuite=true,groups={"all"} )
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
protectedvoidbeforeSuite()throws
Exception
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
{
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!SysInit.initSystem();
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!}

4配置文件:
TestNG,所有必需的数据都集中在 testng.xml 文件中。不需要额外的 TestSuite 文件和构建文件。
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!<!DOCTYPEsuiteSYSTEM"http://beust.com/testng/testng-1.0.dtd">
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<suitename="cx_test">
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<testname="all">
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<groups>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<run>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<includename="all"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</run>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</groups>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<packages>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<packagename="test.com.livedoor.cx.*"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</packages>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</test>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<testname="trade">
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<groups>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<run>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<includename="trade"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</run>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</groups>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<classes>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<classname="test.fukusu.act<wbr>ion.ShinkiFukusuActionTest"</wbr>>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</class>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</classes>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</test>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</suite>

suite为根结点,里边可以包含多个test结点,每个test结点为一个测试集合,可在groups结点中run中的include中指定可以指定运行哪个分组,可以通过packages指定需要测试的哪些测试用例,或者可以通过classes指定需要测试哪个测试用例,clssses中可以包含多个class

5运行测试:
可以使用eclipse中的testng插件直接在eclipse中运行,也可以使用ant运行:编写build.xml:
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!<propertyname="test.report"value="./report/test_ng"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<taskdefclasspathref="classpath"name="testng"classname="com.beust.testng.TestNGAntTask"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<targetname="test_ng">
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<deletedir="${test.report}"quiet="true"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<mkdirdir="${test.report}"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<testngclasspathref="classpath"outputDir="${test.report}"haltOnfailure="false">
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
<xmlfilesetdir="./"includes="testng.xml"/>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</testng>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
</target>
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!

6参数传递:
TestNg,允许向被测试的方法中传递参数,需要传递的参数,需要在testng.xml 文件中声明和赋值:

<parameter name="hachuRecNum" value="10470"/>
<parameter name="testPara" value="testStr"/>

在使用时,需要在被测试的方法前使用@Parameters标记
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!@Test(groups={"test"} )
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!@Parameters(
{"hachuRecNum","testPara"}
)
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
publicvoidtestGetDisplayData(int
arg1,Stringarg2)
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
{
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!System.out.println(
"hachuRecNum:"+
arg1);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!System.out.println(
"testPara:"+
arg2);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
assert(0==0
);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!}

({ "hachuRecNum","testPara" })就是在testng.xml中定义的name,被测试的方法,有多少个参数,@Parameters后就要有多少个参数,@Parameters后的参数,依次赋值给方法中的参数。


运行上边的代码,就会看见以下结果:
[testng] hachuRecNum:10470
[testng] testPara:testStr

7异常处理
TestNg,可以使用@ExpectedExceptions忽略指定的异常:
例如:
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!@Test(groups={"test"} )
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!@ExpectedExceptions(IllegalArgumentException.
class
)
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
publicvoid
testGetDisplayData()
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
{
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
if(1==1)
{
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
thrownewIllegalArgumentException("testexception"
);
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!}

TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!
asserttrue;
TestNg的使用(testng4.7 for jdk15)【转载】 - kuku - Fly to the Sky!}

运行上边的代码,虽然代码中抛出了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" })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值