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);
} }

所以执行结果为:

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. + =
This is After Method
This is Before Method
This is test add method. + =
This is After Method
This is Before Method
This is test add method. + =
This is After Method
This is Before Method
This is test add method. - + =
This is After Method
This is Before Method
This is test minus method. - =
This is After Method
This is Before Method
This is test minus method. - =
This is After Method
This is Before Method
This is test minus method. - - =
This is After Method
This is Before Method
This is test minus method. - =
This is After Method
This is After Class
PASSED: addTest(, , )
PASSED: addTest(, , )
PASSED: addTest(-, , )
PASSED: minusTest(, , )
PASSED: minusTest(, -, )
FAILED: addTest(, , )

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

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

testng配合reportng 生成测试报告

testng 配合ant

testng 教程的更多相关文章

  1. TestNG教程

    TestNG教程 http://www.yiibai.com/testng/2013/0916311.html TestNG,3种执行方式: 1.ant(build.xml) 2.Eclipse(安装 ...

  2. TestNG教程网站

    比较简明的一些TestNG教程网站 : https://www.jianshu.com/p/74816a200221 http://www.yiibai.com/testng/parameterize ...

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

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

  4. TestNG超详细教程

    testNG官网:http://testng.org/doc/download.html howtodoinjava.com里的testNG教程,简单详细:http://howtodoinjava.c ...

  5. Java自动化测试框架-01 - TestNG之入门篇 - 大佬的鸡肋,菜鸟的盛宴(详细教程)

    TestNG是什么? TestNG按照官方的定义: TestNG是一个测试框架,其灵感来自JUnit和NUnit,但引入了一些新的功能,使其功能更强大,使用更方便. TestNG是一个开源自动化测试框 ...

  6. TestNG的简单使用

    TestNG的简单使用 TestNG(Test Next Generation)是一个测试框架,其灵感来自JUnit和NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便. TestNG是 ...

  7. testng的使用

    TestNG教程 TestNG是一个测试框架,其灵感来自JUnit和NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便. TestNG设计涵盖所有类型的测试:单元,功能,端到端,集成等, ...

  8. testng.xml 配置大全

    1.TestNG的运行方式如下: 1 With a testng.xml file 直接run as test suite 2 With ant 使用ant 3 From the command li ...

  9. Self20171218_Eclipse+TestNg HelloWorld

    作为一个经典的入门例子,这里展示如何开始使用TestNG单元测试框架. 使用的工具 : TestNG 6.8.7 Maven 3 Eclipse IDE TestNG下载并安装 从这里 http:// ...

随机推荐

  1. 《Java程序设计》 课程教学

    <Java程序设计> 课程教学 给学生 考核方式 100分构成 翻转课堂考核12次(5*12 = 60):每次考试20-30道题目,考试成绩规格化成5分(比如总分20分就除以4) 注意:不 ...

  2. knockoutJS学习笔记06:ko数组与模板绑定

    前面已经介绍了基本的绑定和模板相关知识,接下来就看ko里的数组和模板绑定,数组和模板绑定应该是实际项目中用得比较多的,ko提供了很好的支持. 一.observaleArray 前面的监控属性都是单个对 ...

  3. JS组件系列——使用HTML标签的data属性初始化JS组件

    前言:最近使用bootstrap组件的时候发现一个易用性问题,很多简单的组件初始化都需要在JS里面写很多的初始化代码,比如一个简单的select标签,因为仅仅只是需要从后台获取数据填充到option里 ...

  4. C#错过的10年

    不知不觉,c#已经诞生n年了,人生有几个十年?c#就浪费了整整一个十年. 这十年里面,电脑发展缓慢,而服务端和手机发展迅速,这是一个移动和后端化的十年,而这个方向,正正是c#没有关注到的,c#把注意力 ...

  5. sql where and or优先级 待验证

    where 后面如果有and,or的条件,则or自动会把左右的查询条件分开,即先执行and,再执行or.原因就是:and的执行优先级最高! 关系型运算符优先级高到低为:not and or 问题的解决 ...

  6. MySQL架构优化实战系列1:数据类型与索引调优全解析

    一.数据类型优化 数据类型 整数   数字类型:整数和实数 tinyint(8).smallint(16).mediuint(24).int(32).bigint(64) 数字表示对应最大存储位数,如 ...

  7. 前端ps常用的小技巧

    一些很简单的例子,知道的就当看乐子. 1.T 是文字的  可以从矢量图中查看文字的大小 字体 颜色,具体就是T  选择一段文字,点确定,点击属性栏最后一个可以看详细信息.又字体,行高,颜色.如果要选取 ...

  8. Ubuntu16.04/LinuxMint18安装openjdk-7-jdk

    LinuxMint18的安装源已经默认没有openjdk7了,所以要自己手动添加仓库,如下: sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-ge ...

  9. UIlabel的字体自适应属性

    有时候我们需要UIlabel根据字数多少来减小字体大小,使得UIlabel能够显示全所有的文字.你需要做的就是设置minimumScaleFactor.minimumScaleFactor默认值是0, ...

  10. spoj 371 Boxes

    N个盒子围成一圈,第i个盒子初始时有Ai个小球,每次可以把一个小球从一个盒子移到相邻的两个盒子之一里.问最少移动多少次使得每个盒子中小球的个数不超过1. ΣAi<=N.1<=N<=1 ...