Testng 简介:

Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性:

  • annotations  注释,如 @test @BeforeMethod
  • 支持多线程执行case
  • 支持数据驱动 dataProvider
  • 支持参参数
  • 能够作为eclipse的插件
  • 能够(配合reportng)生产客观的测试报告
  • 可通过testng.xml管理执行case和suite

那么好的测试框架,怎么使用?

这里我们使用eclipse插件方式 安装详见:http://testng.org/doc/eclipse.html


testng使用

首先了解一下testng 的annotations

常见的有以下:

@BeforeClass: 该annotation在class激活之前执行

@BeforeMethod: 该annotation会在每个执行的方法之前执行

@Test ,该annotation 是你要执行测试的方法

@AfterMethod,该annotation在每个测试方法执行之后运行

@AfterClass 该annotation会在所有测试方法之后运行

具体生命周期如下图:

这里是所有的annotation

@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroups
@AfterGroups
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
Configuration information for a TestNG class:

@BeforeSuite: The annotated method will be run before all tests in this suite have run. 
@AfterSuite: The annotated method will be run after all tests in this suite have run. 
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. 
@AfterClass: The annotated method will be run after all the test methods in the current class have been run. 
@BeforeMethod: The annotated method will be run before each test method. 
@AfterMethod: The annotated method will be run after each test method.

实例:我们验证一下testng annotation 执行顺序,这个case里有两个 测试  ,执行顺序为beforeClass->beforeMethod->test1->afterMethod->beforeMethod->

test2->afterMethod->afterClass.

package com.dbyl.tests;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* This is to verify testng annotation execute
* @author Young
*
*/
public class TestngExample {
private int a; @BeforeMethod(alwaysRun=true)
public void beforeMethod() {
a = 2;
System.out.println("This is beforeMethod method. The Value of a is: "
+ a);
} @BeforeClass
public void beforeClass() {
a = 1;
System.out.println("This is beforeClass method .The Value of a is: "
+ a);
} @Test(groups = "TestngExample")
public void testExample1() {
a = 3;
System.out.println("This is Test method1 .The Value of a is: " + a);
} @Test(groups = "TestngExample")
public void testExample2() {
a = 4;
System.out.println("This is Test method2 .The Value of a is: " + a);
} @AfterClass
public void afterClass() {
a = 5;
System.out.println("This is AfterClass Method .The Value of a is: " + a); } @AfterMethod
public void afterMethod()
{
a = 6;
System.out.println("This is AfterMethod Method .The Value of a is: " + a);
} }

所以执行结果为:

1
2
3
4
5
6
7
8
9
10
This is beforeClass method .The Value of a is: 1
This is beforeMethod method. The Value of a is: 2
This is Test method1 .The Value of a is: 3
This is AfterMethod Method .The Value of a is: 6
This is beforeMethod method. The Value of a is: 2
This is Test method2 .The Value of a is: 4
This is AfterMethod Method .The Value of a is: 6
This is AfterClass Method .The Value of a is: 5
PASSED: testExample1
PASSED: testExample2

  当然,还有BeforeSuite 等,不再做深入研究.

annotation后面可以加一些参数,比如alwaysRun=true/false,dependsOnMethods=""

alwaysRun控制是否每次都执行,dependsOnMethods是一种依赖,依赖某个方法执行

之前有提到testng数据驱动,使用dataProvider,dataProvider可以导入text ,excel等数据,

执行case时会从DataProvider依次拿出数据执行,同一个测试方法,会被执行相应的次数

package com.dbyl.tests;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; public class Case1 { @DataProvider
public Object[][] testData1() {
return new Object[][] { { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 },
{ -1, 3, 2 } };
} @DataProvider
public Object[][] testData2() {
return new Object[][] { { 5, 2, 3 }, { 1, 2, 4 }, { 1, -3, 4 },
{ 6, 3, 2 } };
} public static int add(int a, int b) {
return a + b;
} public static int minus(int a, int b) {
return a - b;
} @BeforeClass
public void beforeClass()
{
System.out.println("This is Before Class");
}
@Test(groups = { "add" }, dataProvider = "testData1")
public void addTest(int a, int b, int c) {
System.out.println("This is test add method. "+a+" + "+ b+" = "+c);
Assert.assertEquals(add(a, b), c);
} @Test(groups = { "minus" }, dataProvider = "testData2")
public void minusTest(int a, int b, int c) {
System.out.println("This is test minus method. "+a+" - "+ b+" = "+c);
Assert.assertEquals(minus(a, b), c);
} @BeforeMethod
public void beforeMethod()
{
System.out.println("This is Before Method");
} @AfterMethod
public void afterMethod()
{
System.out.println("This is After Method");
} @AfterClass
public void afterClass()
{
System.out.println("This is After Class");
}
}

执行结果如下:

This is Before Class
This is Before Method
This is test add method. 1 + 2 = 3
This is After Method
This is Before Method
This is test add method. 1 + 2 = 4
This is After Method
This is Before Method
This is test add method. 1 + 3 = 4
This is After Method
This is Before Method
This is test add method. -1 + 3 = 2
This is After Method
This is Before Method
This is test minus method. 5 - 2 = 3
This is After Method
This is Before Method
This is test minus method. 1 - 2 = 4
This is After Method
This is Before Method
This is test minus method. 1 - -3 = 4
This is After Method
This is Before Method
This is test minus method. 6 - 3 = 2
This is After Method
This is After Class
PASSED: addTest(1, 2, 3)
PASSED: addTest(1, 3, 4)
PASSED: addTest(-1, 3, 2)
PASSED: minusTest(5, 2, 3)
PASSED: minusTest(1, -3, 4)
FAILED: addTest(1, 2, 4)

由于天色已晚,明天继续总结.

接下来:testng.xml的配置和使用,已经参数传递

testng配合reportng 生成测试报告

testng 配合ant

Testng1的更多相关文章

  1. TestNg1. 基本介绍注解介绍和如何让在maven中引用

    1.更适合测试人员,有很多的套件. maven中引用: <!-- https://mvnrepository.com/artifact/org.testng/testng --><d ...

  2. TestNG 入门教程

    原文出处:http://www.cnblogs.com/TankXiao/p/3888070.html 阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装T ...

  3. JUnit 4 与 TestNG 对比

    原文出处: 付学良的网志 原文出处2: http://www.importnew.com/16270.html -------------------------------------------- ...

  4. TestNG 与 Junit的比较

    转自 http://www.blogjava.net/fanscial/archive/2005/12/14/23780.html 1.         JDK 5 Annotations (JDK ...

  5. selenium+testng+ant+jenkins 手记

    会不会搭建测试平台是一般测试工程师和高级测试工程师分水岭 ----tobecrazy 我们项目有现成的测试平台,使用的是selenium grid+testng+ant+jenkins+VM 但是我平 ...

  6. testng 教程之使用参数的一些tricks配合使用reportng

    前两次的总结:testng annotation生命周期 http://www.cnblogs.com/tobecrazy/p/4579414.html testng.xml的使用和基本配置http: ...

  7. testng教程之testng.xml的配置和使用,以及参数传递

    昨天学习了一下testng基础教程,http://www.cnblogs.com/tobecrazy/p/4579414.html 昨天主要学习的是testng 的annotation基本用法和生命周 ...

  8. Maven 配置 Selenium + testNG + reportNG 运行环境

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(56, 58, ...

  9. 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure

    相信用过Selenium WebDriver 的朋友都应该知道如何使用WebDriver API实现Take Screenshot的功能. 在这篇文章里,我主要来介绍对failed tests实现 t ...

随机推荐

  1. buf.writeInt16BE()函数详解

    buf.writeInt16BE(value, offset[, noAssert]) buf.writeInt16LE(value, offset[, noAssert]) value {Numbe ...

  2. MySQL Connector/Python 接口 (二)

    连接数据库 本文参见这里,示例如何连接MySQL 数据库. import mysql.connector from mysql.connector import errorcode # 连接数据库需要 ...

  3. 51nod1128 正整数分组V2

    [题解] 二分一个最大值,check一下分出来的组数是否小于等于k即可. #include<cstdio> #include<algorithm> #define LL lon ...

  4. 洛谷 4172 [WC2006]水管局长

    [题解] 我们把操作倒过来做,就变成了加边而不是删边.于是用LCT维护动态加边的最小生成树就好了.同样要注意把边权变为点权. #include<cstdio> #include<al ...

  5. PAT 1134 Vertex Cover

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  6. windows开启3306端口并用可视化工具访问远程mysql(授权访问)

    开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 3306 端口,否则远程是无法通过 330 ...

  7. mysql bin-log 设置

    mysql 的事物日至为 [root@localhost mysql]# ls -ldtr mysql-bin.* -rw-rw---- mysql mysql 4月 : mysql-bin. -rw ...

  8. Eclipse Java Build Path详解

    Eclipse Java Build Path详解 1.设置"source folder"与"output folder". * source folder:存 ...

  9. MVC路由中特殊URL匹配规则

    *匹配*用来匹配URL剩余部分 贪婪匹配规则贪婪匹配会找到最后一个符合条件的“字面量”为止

  10. 从头认识java-15.1 填充容器(1)-利用Collection构造器的方式

    这一章节我们来介绍一下填充容器. 就像数组一样,Arrays.fill是填充方法,在容器里面也有. 1.Collections.nCopies 这种方法是生成某种类型多少个对象,然后我们能够把他放到容 ...