TestNG 使用入门
转载请保留作者信息:
Author: 88250
Blog: http:/blog.youkuaiyun.com/DL88250
MSN & Gmail & QQ: DL88250@gmail.com
TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器),本文以一个简单的例子展示了 TestNG 的基本运用。由于目前 NetBeans IDE 对 TestNG 目前还没有支持(不过 NetBeans 已经 开始计划和实现了),所以示例工程使用 Maven 构建。
pom.xml:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>cn.edu.ynu.sei</groupId>
- <artifactId>HelloTestng</artifactId>
- <packaging>jar</packaging>
- <version>1.0.0.0</version>
- <name>HelloTestng</name>
- <description>AsimpledemoforTestngwithmaven.</description>
- <url>http://maven.apache.org</url>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.0.2</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>5.8</version>
- <scope>test</scope>
- <classifier>jdk15</classifier>
- <exclusions>
- <exclusion>
- <artifactId>junit</artifactId>
- <groupId>junit</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- </project>
scr:
- packagecn.edu.ynu.sei.test;
- /**
- *AverysimpleclassfordemonstratehowtouseTestng.
- *
- *@author<ahref="mailto:DL88250@gmail.com">LiangDing</a>
- *@version1.0.0.0,Oct29,2008
- */
- publicclassCalc{
- /**
- *Addsthespecifiedintegeroperand.
- *@paramioperand1
- *@paramjoperand2
- *@returnthesumof<code>i</code>and<code>j</code>
- *@throwsAddExceptionifanyoccursexeption,throwsthis
- *exception
- */
- publicintadd(inti,intj)throwsAddException{
- if((((i&0x80000000)^(j&0x80000000))==0)&&
- ((i&0x7FFFFFFF)+(j&0x7FFFFFFF))<0){
- //overflow
- thrownewAddException("Addoverflow!");
- }
- returni+j;
- }
- }
- packagecn.edu.ynu.sei.test;
- /**
- *Ifoccursaexceptionofaddition,throwsthisexcpetion.
- *
- *@author<ahref="mailto:DL88250@gmail.com">LiangDing</a>
- *@version1.0.0.0,Oct29,2008
- */
- publicclassAddExceptionextendsException{
- /**
- *generatedserialversionid
- */
- privatestaticfinallongserialVersionUID=7337399941260755583L;
- /**
- *Constructsanewexceptionwith<code>null</code>asitsdetailmessage.
- *Thecauseisnotinitialized,andmaysubsequentlybeinitializedbya
- *callto{@link#initCause}.
- */
- publicAddException(){
- super();
- }
- /**
- *Constructsanewexceptionwiththespecifieddetailmessage.The
- *causeisnotinitialized,andmaysubsequentlybeinitializedby
- *acallto{@link#initCause}.
- *
- *@parammessagethedetailmessage.Thedetailmessageissavedfor
- *laterretrievalbythe{@link#getMessage()}method.
- */
- publicAddException(Stringmessage){
- super(message);
- }
- }
- packagecn.edu.ynu.sei.test;
- importorg.testng.annotations.AfterClass;
- importorg.testng.annotations.AfterMethod;
- importorg.testng.annotations.AfterTest;
- importorg.testng.annotations.BeforeClass;
- importorg.testng.annotations.BeforeMethod;
- importorg.testng.annotations.BeforeTest;
- importorg.testng.annotations.Test;
- importstaticorg.testng.Assert.*;
- /**
- *Asimpleunittestfor<code>App</code>.
- *
- *@author<ahref="mailto:DL88250@gmail.com">LiangDing</a>
- *@version1.0.0.0,Oct29,2008
- *@seeCalc
- */
- publicclassCalcTest{
- /**
- *instancetotest
- */
- privateCalcinstance;
- /**
- *ThisMethodwillberunbeforethetest.
- */
- @BeforeTest
- publicvoidbeforeTest(){
- System.out.println("beforetest");
- }
- /**
- *Thismethodwillberunafterthetest.
- */
- @AfterTest
- publicvoidafterTest(){
- System.out.println("aftertest");
- }
- /**
- *Thismethodwillberunbeforethefirsttestmethod
- *inthecurrentclassisinvoked.
- */
- @BeforeClass
- publicvoidbeforeClass(){
- System.out.println("beforeclass");
- System.out.println("newtestinstance");
- instance=newCalc();
- }
- /**
- *Thismethodwillberunafterallthetestmethods
- *inthecurrentclasshavebeenrun.
- */
- @AfterClass
- publicvoidafterClass(){
- System.out.println("afterclass");
- }
- /**
- *Thismethodwillberunbeforeeachtestmethod.
- */
- @BeforeMethod
- publicvoidbeforeMethod(){
- System.out.println("beforemethod");
- }
- /**
- *Thismethodwillberunaftereachtestmethod.
- */
- @AfterMethod
- publicvoidafterMethod(){
- System.out.println("aftermethod");
- }
- /**
- *Testfor<code>add</code>method,ofclass<code>Calc</code>.
- *Thistestmethodshouldrunsuccessfully.
- *@throwsException
- */
- @Test
- publicvoidaddSucc()throwsException{
- System.out.println("add");
- intresult=instance.add(1,1);
- assertEquals(2,result);
- }
- /**
- *Testfor<code>add</code>method,ofclass<code>Calc</code>
- *Thistestmethodshouldthrowa<code>AddException</code>
- *@throwsException
- */
- @Test(expectedExceptions=AddException.class)
- publicvoidaddFail()throwsException{
- System.out.println("add");
- intresult=instance.add(Integer.MAX_VALUE,Integer.MAX_VALUE);
- System.out.println(result);
- }
- }
TestNG官方网站: http://testng.org/