TestNG 使用入門
轉載請保留作者資訊:
Author: 88250
Blog: http:/blog.csdn.net/DL88250
MSN & Gmail & QQ: DL88250@gmail.com
TestNG是一個設計用來簡化廣泛的測試需求的測試架構,從單元測試(隔離測試一個類)到整合測試(測試由有多個類多個包甚至多個外部架構組成的整個系統,例如運用伺服器),本文以一個簡單的例子展示了 TestNG 的基本運用。由於目前 NetBeans IDE 對 TestNG 目前還沒有支援(不過 NetBeans 已經 開始計劃和實現了),所以樣本工程使用 Maven 構建。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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.0 http://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>A simple demo for Testng with maven.</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:
package cn.edu.ynu.sei.test; /** * A very simple class for demonstrate how to use Testng. * * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @version 1.0.0.0, Oct 29, 2008 */ public class Calc { /** * Adds the specified integer operand. * @param i operand 1 * @param j operand 2 * @return the sum of <code>i</code> and <code>j</code> * @throws AddException if any occurs exeption, throws this * exception */ public int add(int i, int j) throws AddException { if ((((i & 0x80000000) ^ (j & 0x80000000)) == 0) && ((i & 0x7FFFFFFF) + (j & 0x7FFFFFFF)) < 0) { // overflow throw new AddException("Add overflow!"); } return i + j; } }
package cn.edu.ynu.sei.test; /** * If occurs a exception of addition, throws this excpetion. * * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @version 1.0.0.0, Oct 29, 2008 */ public class AddException extends Exception { /** * generated serial version id */ private static final long serialVersionUID = 7337399941260755583L; /** * Constructs a new exception with <code>null</code> as its detail message. * The cause is not initialized, and may subsequently be initialized by a * call to {@link #initCause}. */ public AddException() { super(); } /** * Constructs a new exception with the specified detail message. The * cause is not initialized, and may subsequently be initialized by * a call to {@link #initCause}. * * @param message the detail message. The detail message is saved for * later retrieval by the {@link #getMessage()} method. */ public AddException(String message) { super(message); } } test:
package cn.edu.ynu.sei.test; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import static org.testng.Assert.*; /** * A simple unit test for <code>App</code>. * * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @version 1.0.0.0, Oct 29, 2008 * @see Calc */ public class CalcTest { /** * instance to test */ private Calc instance; /** * This Method will be run before the test. */ @BeforeTest public void beforeTest() { System.out.println("before test"); } /** * This method will be run after the test. */ @AfterTest public void afterTest() { System.out.println("after test"); } /** * This method will be run before the first test method * in the current class is invoked. */ @BeforeClass public void beforeClass() { System.out.println("before class"); System.out.println("new test instance"); instance = new Calc(); } /** * This method will be run after all the test methods * in the current class have been run. */ @AfterClass public void afterClass() { System.out.println("after class"); } /** * This method will be run before each test method. */ @BeforeMethod public void beforeMethod() { System.out.println("before method"); } /** * This method will be run after each test method. */ @AfterMethod public void afterMethod() { System.out.println("after method"); } /** * Test for <code>add</code> method, of class <code>Calc</code>. * This test method should run successfully. * @throws Exception */ @Test public void addSucc() throws Exception { System.out.println("add"); int result = instance.add(1, 1); assertEquals(2, result); } /** * Test for <code>add</code> method, of class <code>Calc</code> * This test method should throw a <code>AddException</code> * @throws Exception */ @Test(expectedExceptions = AddException.class) public void addFail() throws Exception { System.out.println("add"); int result = instance.add(Integer.MAX_VALUE, Integer.MAX_VALUE); System.out.println(result); } }