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"); } }
复制代码

 

执行结果如下: 

        

View Code

 

转载于:https://www.cnblogs.com/hua-an/p/5056965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程介绍你是否在寻找机会进入自动化测试领域? 你是否渴望学习selenium webdriver + Java以及最新的框架和技术进行web自动化测试? 你是否感兴趣学习Selenium如何用在你现有的项目里的? 这门课带你从Selenium搭建环境开始讲起,然后学习selenium,TestNG, logback, maven, jenkins。 我们假设学员没有任何自动化经验,来设计的这套课程。每个课题都从最基础的开始讲起。Selenium相关的该覆盖的课题都覆盖了。 例子都是来自于真实的web应用项目,帮助你理解不同的组件怎么用上自动化,这将展示给你一个行业层面的框架,增加自信心。 全网没有其他课程像这门课涵盖到如此之深的细节。 您将会学到什么 学完课程以后,你将拥有完整的Selenium Webdriver知识 你将具备从头开始设计Page Object、Page Factory、DATADRIVEN等搭建自动化框架的能力 用100多个实例对Selenium现实场景应用进行深入理解 全面了解TestNG, Maven, Jenkins, HTML报告,多浏览器并行测试 了解数据库测试和使用Selenium进行性能测试 你将彻底了解testNG框架 你从网上随便选择一个网站,都可以实现自动化,用所有可能的测试用例进行自动化测试 将提高你的编码技能,以编写最优化的自动化测试用例代码 你基本可以搞定任何Selenium面试,并能从设计阶段开始领导整个Selenium自动化项目 你应该能够使用应用程序的GUI来验证数据完整性 你将能够创建漂亮的报告来打动客户或领导 更深入地理解自动化指南和代码质量标准 会附带一个练习网站,可以用上所有可用的WebDriver功能,实现自动化 【适合人群】 软件手动测试人员想转为自动化测试的人员 自动化软件测试人员想加强专业技能的 刚毕业学生想从事软件行业 QA 组长或项目经理 【课程优势】 学完课程以后,你将拥有完整的Selenium Webdriver知识 【讲师介绍】 资质介绍: 12年以上软件测试工作经验,其中7年以上自动化测试开发经验 新书“Python3+Selenium3自动化测试项目实战”作者

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值