`
hereson2
  • 浏览: 458366 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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/

分享到:
评论

相关推荐

    junit和TestNG框架入门

    为了能够在Eclipse环境中使用TestNG,需要先安装相应的插件。具体步骤如下: 1. 打开Eclipse,依次点击`Help` -&gt; `Eclipse Marketplace...` 2. 在搜索框中输入`TestNG`,点击搜索。 3. 从搜索结果中找到`TestNG for...

    TestNG笔记

    如果你有软件开发和测试的背景,这将极大地促进你对TestNG的理解和使用。 TestNG相对于JUnit的优势在于: 1. **注解**:TestNG利用Java的注解机制,使得测试的定义更加简洁和灵活。 2. **面向对象和Java支持**:...

    【TestNG自动化测试框架】TestNG自动化测试框架入门到实战完整笔记

    TestNG自动测试框架是当今流行的自动化测试框架之一 它可以帮助自动化测试工程师把精力集中在编写和实施测试用例和测试脚本上,提升软件测试执行和回归测试效率 分成4大模块 第一模块java 基础知识:JDK安装以及环境...

    TestNG插件

    关于TestNG的基本入门学习,你需要掌握以下几个核心概念: - **测试套件(Suite)**:通过一个XML文件定义多个测试类或测试方法,可以包含多个测试组。 - **测试组(Group)**:将相关的测试方法分组,方便按需运行或...

    Testng自动化测试框架实战详解,全网最新最全Testng自动化测试框架技术

    ### TestNG自动化测试框架实战详解 #### 一、TestNG简介 TestNG 是一个功能强大的自动化测试框架,它汲取了 JUnit 和 ...通过深入学习 TestNG 的特性和实践案例,测试工程师可以从基础入门逐渐成长为高级测试工程师。

    终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python

    这些插件的使用方法可以通过搜索 Selenium 私房菜(新手入门教程).pdf 获得。 六、启动 Selenium RC 启动 Selenium RC 需要使用 CMD 命令行,输入 java -jar selenium-server-standalone-2.33.0.jar,或者可以将...

    Java+TestNG接口自动化入门详解-附件资源

    Java+TestNG接口自动化入门详解-附件资源

    JAVA 接口自动化测试Demo_testng

    主要对举例对国家气象局接口自动化测试进行讲解(Get请求及结果断言),以达到自动化测试入门目的,需要有一定的JAVA知识(HTTP相关)。

    Selenium+TestNG实战2

    ### Selenium与TestNG实战——实现WordPress自动化登录 ...这对于想要入门自动化测试领域的开发者来说是非常宝贵的实践经验。希望读者能够通过实际操作加深理解,并在实践中不断探索更多Selenium的强大功能。

    RealTimeReport:创建该项目是为了使用TestNG获取实时测试报告

    该项目可帮助您使用TestNG在运行时获取测试报告。 您可以使用现有项目来启动此报告,也可以将其与新的testNG项目合并。 该报告的特殊性是-无需更改现有代码库,只需在用户的testng.xml文件中添加我们的预定义列表器...

    jbehave-testng-example:BDD完整的jBehave入门专案与范例

    "jbehave-testng-example:BDD完整的jBehave入门专案与范例" 这个标题表明这是一个关于行为驱动开发(Behavior Driven Development, BDD)的项目,具体使用了jBehave工具,并结合了TestNG进行测试。jBehave是一个用...

    【基于Selenium 2 (java+jUnit+testNg)的自动化测试-从入门到精通】白洛.著

    高清扫描版,包含完整书签目录 第1章 初始selenium 第2章 牛刀小试只selenium ide 第3章 selenium玩转页面元素 第4章 初始selenium webdriver 第5章 玩转selenium webdriver 第6章 selenium玩转android ...

    selenium2初学者快速入门

    ### Selenium2初学者快速入门详解 #### 一、引言 随着软件开发的快速发展和规模的不断增大,传统的手动测试方式越来越难以满足高效且频繁的测试需求。为了解决这一问题,自动化测试成为了软件测试领域的重要发展...

    java-webtest-template:使用 TestNG 和 Selenium 进行测试的快速入门 Java 模板 - Java Selenium Maven TestNG 模板

    使用 TestNG 和 Selenium 进行测试的快速入门 Java 模板 - Java Selenium Maven TestNG 模板 快速开始: 打开 Eclipse 导入项目 运行 maven 安装 在 GoogleSearchTest 类中运行测试 GoogleSearch_Test 访问我们: ...

    TestNgProject:UI自动化-Selenium和TestNg

    入门 安装Eclipse: 在“获取Eclipse IDE 2019-12”下⇒单击“下载软件包”。 对于初学者,请选择“用于Java开发人员的Eclipse IDE”和“ Windows 64位”(例如,“ eclipse-java-2019-12-R-win32-x86_64.zip”-...

Global site tag (gtag.js) - Google Analytics