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);
- }
- }
- 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);
- }
- }
TestNG官方网站:http://testng.org/