• TestNG 使用入门


    TestNG 使用入门


    转载请保留作者信息:

    Author: 88250

    Blog: http:/blog.csdn.net/DL88250

    MSN & Gmail & QQ: DL88250@gmail.com


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

    pom.xml:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <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">
    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>A simple demo for Testng with maven.</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. package cn.edu.ynu.sei.test;
    2. /**
    3.  * A very simple class for demonstrate how to use Testng.
    4.  *
    5.  * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
    6.  * @version 1.0.0.0, Oct 29, 2008
    7.  */
    8. public class Calc {
    9.     /**
    10.      * Adds the specified integer operand.
    11.      * @param i operand 1
    12.      * @param j operand 2
    13.      * @return the sum of <code>i</code> and <code>j</code>
    14.      * @throws AddException if any occurs exeption, throws this
    15.      * exception
    16.      */
    17.     public int add(int i, int j) throws AddException {
    18.         if ((((i & 0x80000000) ^ (j & 0x80000000)) == 0) &&
    19.                 ((i & 0x7FFFFFFF) + (j & 0x7FFFFFFF)) < 0) {
    20.             // overflow
    21.             throw new AddException("Add overflow!");
    22.         }
    23.         return i + j;
    24.     }
    25. }

    1. package cn.edu.ynu.sei.test;
    2. /**
    3.  * If occurs a exception of addition, throws this excpetion.
    4.  *
    5.  * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
    6.  * @version 1.0.0.0, Oct 29, 2008
    7.  */
    8. public class AddException extends Exception {
    9.     /**
    10.      * generated serial version id
    11.      */
    12.     private static final long serialVersionUID = 7337399941260755583L;
    13.     /**
    14.      * Constructs a new exception with <code>null</code> as its detail message.
    15.      * The cause is not initialized, and may subsequently be initialized by a
    16.      * call to {@link #initCause}.
    17.      */
    18.     public AddException() {
    19.         super();
    20.     }
    21.     /**
    22.      * Constructs a new exception with the specified detail message.  The
    23.      * cause is not initialized, and may subsequently be initialized by
    24.      * a call to {@link #initCause}.
    25.      *
    26.      * @param   message   the detail message. The detail message is saved for
    27.      *          later retrieval by the {@link #getMessage()} method.
    28.      */
    29.     public AddException(String message) {
    30.         super(message);
    31.     }
    32. }
    test:
    1. package cn.edu.ynu.sei.test;
    2. import org.testng.annotations.AfterClass;
    3. import org.testng.annotations.AfterMethod;
    4. import org.testng.annotations.AfterTest;
    5. import org.testng.annotations.BeforeClass;
    6. import org.testng.annotations.BeforeMethod;
    7. import org.testng.annotations.BeforeTest;
    8. import org.testng.annotations.Test;
    9. import static org.testng.Assert.*;
    10. /**
    11.  * A simple unit test for <code>App</code>.
    12.  *
    13.  * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
    14.  * @version 1.0.0.0, Oct 29, 2008
    15.  * @see Calc
    16.  */
    17. public class CalcTest {
    18.     /**
    19.      * instance to test
    20.      */
    21.     private Calc instance;
    22.     /**
    23.      * This Method will be run before the test.
    24.      */
    25.     @BeforeTest
    26.     public void beforeTest() {
    27.         System.out.println("before test");
    28.     }
    29.     /**
    30.      * This method will be run after the test.
    31.      */
    32.     @AfterTest
    33.     public void afterTest() {
    34.         System.out.println("after test");
    35.     }
    36.     /**
    37.      * This method will be run before the first test method
    38.      * in the current class is invoked.
    39.      */
    40.     @BeforeClass
    41.     public void beforeClass() {
    42.         System.out.println("before class");
    43.         System.out.println("new test instance");
    44.         instance = new Calc();
    45.     }
    46.     /**
    47.      * This method will be run after all the test methods
    48.      * in the current class have been run.
    49.      */
    50.     @AfterClass
    51.     public void afterClass() {
    52.         System.out.println("after class");
    53.     }
    54.     /**
    55.      * This method will be run before each test method.
    56.      */
    57.     @BeforeMethod
    58.     public void beforeMethod() {
    59.         System.out.println("before method");
    60.     }
    61.     /**
    62.      * This method will be run after each test method. 
    63.      */
    64.     @AfterMethod
    65.     public void afterMethod() {
    66.         System.out.println("after method");
    67.     }
    68.     /**
    69.      * Test for <code>add</code> method, of class <code>Calc</code>.
    70.      * This test method should run successfully.
    71.      * @throws Exception
    72.      */
    73.     @Test
    74.     public void addSucc() throws Exception {
    75.         System.out.println("add");
    76.         int result = instance.add(11);
    77.         assertEquals(2, result);
    78.     }
    79.     /**
    80.      * Test for <code>add</code> method, of class <code>Calc</code>
    81.      * This test method should throw a <code>AddException</code>
    82.      * @throws Exception
    83.      */
    84.     @Test(expectedExceptions = AddException.class)
    85.     public void addFail() throws Exception {
    86.         System.out.println("add");
    87.         int result = instance.add(Integer.MAX_VALUE, Integer.MAX_VALUE);
    88.         System.out.println(result);
    89.     }
    90. }

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

  • 相关阅读:
    Git在AndroidStudio中的使用(一)
    自定义SiidingMenu简单实现
    圆形ImageView的简单实现
    使用PullToRefresh实现下拉刷新和上拉加载
    XMLpull解析使用
    Xutils简单使用
    友盟第三方登录和分享的使用
    ImageLoader简单使用
    Android中利用LinearLayout动态添加控件
    数据库两大神器【索引和锁】
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6469895.html
Copyright © 2020-2023  润新知