TestNG 使用入门

TestNG 使用入门


转载请保留作者信息:

Author: 88250

Blog: http:/blog.youkuaiyun.com/DL88250

MSN & Gmail & QQ: DL88250@gmail.com


TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器),本文以一个简单的例子展示了 TestNG 的基本运用。由于目前 NetBeans IDE 对 TestNG 目前还没有支持(不过 NetBeans 已经 开始计划和实现了),所以示例工程使用 Maven 构建。

pom.xml:
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn.edu.ynu.sei</groupId>
  5. <artifactId>HelloTestng</artifactId>
  6. <packaging>jar</packaging>
  7. <version>1.0.0.0</version>
  8. <name>HelloTestng</name>
  9. <description>AsimpledemoforTestngwithmaven.</description>
  10. <url>http://maven.apache.org</url>
  11. <build>
  12. <plugins>
  13. <plugin>
  14. <artifactId>maven-compiler-plugin</artifactId>
  15. <version>2.0.2</version>
  16. <configuration>
  17. <source>1.6</source>
  18. <target>1.6</target>
  19. </configuration>
  20. </plugin>
  21. </plugins>
  22. </build>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.testng</groupId>
  26. <artifactId>testng</artifactId>
  27. <version>5.8</version>
  28. <scope>test</scope>
  29. <classifier>jdk15</classifier>
  30. <exclusions>
  31. <exclusion>
  32. <artifactId>junit</artifactId>
  33. <groupId>junit</groupId>
  34. </exclusion>
  35. </exclusions>
  36. </dependency>
  37. </dependencies>
  38. </project>

scr:
  1. packagecn.edu.ynu.sei.test;
  2. /**
  3. *AverysimpleclassfordemonstratehowtouseTestng.
  4. *
  5. *@author<ahref="mailto:DL88250@gmail.com">LiangDing</a>
  6. *@version1.0.0.0,Oct29,2008
  7. */
  8. publicclassCalc{
  9. /**
  10. *Addsthespecifiedintegeroperand.
  11. *@paramioperand1
  12. *@paramjoperand2
  13. *@returnthesumof<code>i</code>and<code>j</code>
  14. *@throwsAddExceptionifanyoccursexeption,throwsthis
  15. *exception
  16. */
  17. publicintadd(inti,intj)throwsAddException{
  18. if((((i&0x80000000)^(j&0x80000000))==0)&&
  19. ((i&0x7FFFFFFF)+(j&0x7FFFFFFF))<0){
  20. //overflow
  21. thrownewAddException("Addoverflow!");
  22. }
  23. returni+j;
  24. }
  25. }

  1. packagecn.edu.ynu.sei.test;
  2. /**
  3. *Ifoccursaexceptionofaddition,throwsthisexcpetion.
  4. *
  5. *@author<ahref="mailto:DL88250@gmail.com">LiangDing</a>
  6. *@version1.0.0.0,Oct29,2008
  7. */
  8. publicclassAddExceptionextendsException{
  9. /**
  10. *generatedserialversionid
  11. */
  12. privatestaticfinallongserialVersionUID=7337399941260755583L;
  13. /**
  14. *Constructsanewexceptionwith<code>null</code>asitsdetailmessage.
  15. *Thecauseisnotinitialized,andmaysubsequentlybeinitializedbya
  16. *callto{@link#initCause}.
  17. */
  18. publicAddException(){
  19. super();
  20. }
  21. /**
  22. *Constructsanewexceptionwiththespecifieddetailmessage.The
  23. *causeisnotinitialized,andmaysubsequentlybeinitializedby
  24. *acallto{@link#initCause}.
  25. *
  26. *@parammessagethedetailmessage.Thedetailmessageissavedfor
  27. *laterretrievalbythe{@link#getMessage()}method.
  28. */
  29. publicAddException(Stringmessage){
  30. super(message);
  31. }
  32. }
test:
  1. packagecn.edu.ynu.sei.test;
  2. importorg.testng.annotations.AfterClass;
  3. importorg.testng.annotations.AfterMethod;
  4. importorg.testng.annotations.AfterTest;
  5. importorg.testng.annotations.BeforeClass;
  6. importorg.testng.annotations.BeforeMethod;
  7. importorg.testng.annotations.BeforeTest;
  8. importorg.testng.annotations.Test;
  9. importstaticorg.testng.Assert.*;
  10. /**
  11. *Asimpleunittestfor<code>App</code>.
  12. *
  13. *@author<ahref="mailto:DL88250@gmail.com">LiangDing</a>
  14. *@version1.0.0.0,Oct29,2008
  15. *@seeCalc
  16. */
  17. publicclassCalcTest{
  18. /**
  19. *instancetotest
  20. */
  21. privateCalcinstance;
  22. /**
  23. *ThisMethodwillberunbeforethetest.
  24. */
  25. @BeforeTest
  26. publicvoidbeforeTest(){
  27. System.out.println("beforetest");
  28. }
  29. /**
  30. *Thismethodwillberunafterthetest.
  31. */
  32. @AfterTest
  33. publicvoidafterTest(){
  34. System.out.println("aftertest");
  35. }
  36. /**
  37. *Thismethodwillberunbeforethefirsttestmethod
  38. *inthecurrentclassisinvoked.
  39. */
  40. @BeforeClass
  41. publicvoidbeforeClass(){
  42. System.out.println("beforeclass");
  43. System.out.println("newtestinstance");
  44. instance=newCalc();
  45. }
  46. /**
  47. *Thismethodwillberunafterallthetestmethods
  48. *inthecurrentclasshavebeenrun.
  49. */
  50. @AfterClass
  51. publicvoidafterClass(){
  52. System.out.println("afterclass");
  53. }
  54. /**
  55. *Thismethodwillberunbeforeeachtestmethod.
  56. */
  57. @BeforeMethod
  58. publicvoidbeforeMethod(){
  59. System.out.println("beforemethod");
  60. }
  61. /**
  62. *Thismethodwillberunaftereachtestmethod.
  63. */
  64. @AfterMethod
  65. publicvoidafterMethod(){
  66. System.out.println("aftermethod");
  67. }
  68. /**
  69. *Testfor<code>add</code>method,ofclass<code>Calc</code>.
  70. *Thistestmethodshouldrunsuccessfully.
  71. *@throwsException
  72. */
  73. @Test
  74. publicvoidaddSucc()throwsException{
  75. System.out.println("add");
  76. intresult=instance.add(1,1);
  77. assertEquals(2,result);
  78. }
  79. /**
  80. *Testfor<code>add</code>method,ofclass<code>Calc</code>
  81. *Thistestmethodshouldthrowa<code>AddException</code>
  82. *@throwsException
  83. */
  84. @Test(expectedExceptions=AddException.class)
  85. publicvoidaddFail()throwsException{
  86. System.out.println("add");
  87. intresult=instance.add(Integer.MAX_VALUE,Integer.MAX_VALUE);
  88. System.out.println(result);
  89. }
  90. }

TestNG官方网站: http://testng.org/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值