转载:http://www.51testing.com/html/58/n-3721258.html
由于一直忙于功能和性能测试,接口自动化测试框架改造的工作被耽搁了好久。近期闲暇一些,可以来做点有意思的事情。
  先前的自动化测试框架完全是用Java纯手工编写,核心交易接口代码、测试脚本、测试数据都进行了抽象和分离,测试报告也是自己设计的html模版输出,如果项目仅仅本地实施运行,也完全能满足目前的自动化测试需求。
  但为了自动化测试变得更加高大上,并配合公司实施持续集成的工作开展,决定将现有的接口自动化测试框架改造成Maven+TestNG方式,代码由SVN进行版本管理,项目由Jenkins构建运行。
  听说TestNG已很久,遗憾一直未尝试过,但最近学习实践了一把,这体验那叫一个字:超爽。单元测试、注解、组概念、套件、异常、参数化、依赖等等测试思想的加入,让TestNG服务于接口自动化测试大放异彩。
  本篇文章分5部分介绍:
  --1 Maven+TestNG的测试框架搭建
  --2 使用ReportNG来优化测试报告
  --3 测试案例的数据调度设计
  --4 使用Jenkins来调度构建运行
  --5 让报告更高大上—Allure报告插件使用
  由于篇幅问题,后两节会放在下一篇。
  1、Maven+TestNG的测试框架搭建
  准备条件
  1、Eclipse及其Maven插件、TestNG插件的安装请自行找度娘;
  2、本地需安装Maven,并配置好环境变量;
  3、Eclipse中的Maven-settings配置。
  创建Maven项目
  为了便于管理自动化项目中依赖的jar包、项目构建运行等,我们选择Maven来创建项目。由于对Maven研究不多,在自动化项目中遇到无法构建引入本地jar包的尴尬,但没找到解决方法,只好通过避免引入本地jar包来搞定。留待以后再研究怎么引入本地jar包的方法。
 
  Maven项目创建完后,如下目录结构:
  POM文件配置
  1、添加testNG的依赖包
  有两种方式:
  一种使用下面截图的方式,当然这种方式依赖于你的Maven插件,有时候搜索不到你想要的依赖包;
  还有一种是直接用下面的配置代码。
  <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.11</version>
  </dependency>
 
  添加完成后查看pom文件内容
  创建TestNG-Class类
  选中 src/test/java目录,右键创建包,
  然后在创建好的包上创建TestNG类, 类名:IdAuth,同时添加上@BeforeClass 和 @AfterClass:
 
 
  代码中通过@Test注解的就是一个测试案例。
  注:如果没有在pom.xml文件中配置testng的依赖jar包,代码中会有报错。
  当前对于BeforeClass和AfterClass代码为空,在后面的内容会使用到。
  此时可以使用TestNG来运行类,邮件Run As —> TestNG Test 方式运行,结果如下:
  以上就完成了TestNG类创建并使用TestNG 运行测试类。
  接下来学习使用Maven来运行项目。
  使用Maven运行项目
  首先创建testng.xml配置文件(src\test\resources目录下创建),testng.xml中配置需要运行的TestNG类名(可以配置多个),配置内容如下:
  然后用Maven运行项目很简单,选中项目然后右键Run As,会出现很多Maven ***的选项,使用Maven Test选项来运行TestNG类。
  当然也可以在DOS窗口中,进入到项目来执行 mvn test。
  运行完成后,在项目根目录中,会出现2个新文件夹:
  其中:target是maven生成一些类文件,暂时不用管
  test-output是生成testNG类执行的结果:
  提供了2种查看执行结果的方式,1个是xml查看,1个是html查看,因为xml友好性不高,咱们直接看html的方式,结果报告中会显示执行了哪些测试案例,成功数和失败数,以及每个测试案例执行的时长。
  但是,大家看到这个报告的第一感觉是什么?是不是觉得很Low?
  我的感觉是简直Low爆了!!!宇宙第二Low的报告!!!
  接下来的内容咱们就来优化报告。
2、使用ReportNG插件来优化测试报告
  Pom.xml文件配置更新
  1、属性配置
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <xmlFileName>testng.xml</xmlFileName>
  </properties>
  2、依赖包管理配置
  <!-- testNG依赖管理 -->
  <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.11</version>
  </dependency>
  <dependency>
  <groupId>org.apache.maven.reporting</groupId>
  <artifactId>maven-reporting-api</artifactId>
  <version>2.0.9</version>
  <scope>test</scope>
  <exclusions>
  <exclusion>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  </exclusion>
  </exclusions>
  </dependency>
  <!-- 依赖Guice -->
  <dependency>
  <groupId>com.google.inject</groupId>
  <artifactId>guice</artifactId>
  <version>3.0</version>
  <scope>test</scope>
  </dependency>
  <dependency>
  <groupId>org.uncommons</groupId>
  <artifactId>reportng</artifactId>
  <version>1.1.4</version>
  </dependency>
  <dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.18.1</version>
  <type>maven-plugin</type>
  </dependency>
  <dependency>
  <groupId>com.beust</groupId>
  <artifactId>jcommander</artifactId>
  <version>1.64</version>
  </dependency>
  <dependency>
  <groupId>net.sourceforge.jexcelapi</groupId>
  <artifactId>jxl</artifactId>
  <version>2.6.10</version>
  </dependency>
  3、Build及插件配置
  <build>
  <plugins>
  <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.18.1</version>
  <configuration>
  <suiteXmlFiles>
  <suiteXmlFile>
  src/test/resources/${xmlFileName}
  </suiteXmlFile>
  </suiteXmlFiles>
  </configuration>
  </plugin>
  <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.18.1</version>
  <configuration>
  <properties>
  <property>
  <name>usedefaultlisteners</name>
  <value>false</value>
  </property>
  <property>
  <name>listener</name>
  <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value>
  </property>
  </properties>
  <workingDirectory>target/</workingDirectory>
  <forkMode>always</forkMode>
  <!-- 解决报告中中文乱码 -->
  <argLine>-Dfile.encoding=UTF-8</argLine>
  </configuration>
  </plugin>
  </plugins>
  </build>
  运行查看报告
  用Maven Test运行完成后,可以发现target/目录下出现新的文件夹
  进入这个目录,打开html/index.html文件可以查看测试报告,如下图:
  这样的页面是不是比刚才的美观了很多。
  如果就这样的报告,肯定不饱满,那我们需要给测试案例添加测试数据获取,业务逻辑,断言以及多个测试类等等。
  3、测试案例的参数化设计
  参数化设计逻辑图
  项目详细的目录结构
  ExcelUtils处理类
  (代码未经优化,有些地方可能写得不合理,请见谅!)
  package com.test.pub;
  import java.io.File;
  import java.util.HashMap;
  import java.util.Map;
  import jxl.Workbook;
  import jxl.Sheet;
  import jxl.Cell;
  public class ExcelUtils {
  public static Map <String, HashMap> excelToHashMapByFirstSheet(String fPath, String sheetName){
  try{
  File fExcel = getExcelFileObj(fPath);
  Workbook wb = Workbook.getWorkbook(fExcel);
  Sheet sht = wb.getSheet(sheetName);
  int rowCount = sht.getRows();
  int colCount = sht.getColumns();
  Cell cel = null;
  Map <String, HashMap> excelContents = new HashMap();
  if(rowCount<=2){
  System.out.println("无测试案例");
  return null;
  }else{
  //检查是否存在空行
  String rowContents = "";
  for(int i=2;i<rowCount;i++){
  if(sht.getCell(0, i).getContents().toString().length()==0){
  System.out.println("测试案例文件中存在空行");
  return null;
  }else{
  for(int j=0;j<colCount;j++){
  rowContents = rowContents + sht.getCell(j, i).getContents().toString();
  }
  if (rowContents.length()<20){
  System.out.println("测试案例文件中存在空行");
  return null;
  }
  }
  }
  }
  //开始读取内容
  for(int rowIndex=2;rowIndex<rowCount;rowIndex++){
  HashMap<String, String> rowMap = new HashMap();
  String testCaseCode = sht.getCell(0, rowIndex).getContents().toString();
  for(int colIndex=1;colIndex<colCount;colIndex++){
  rowMap.put(sht.getCell(colIndex, 1).getContents().toString(), sht.getCell(colIndex, rowIndex).getContents().toString());
  }
  excelContents.put(testCaseCode, rowMap);
  }
  wb.close();
  //HashMap<String, String> tmpMap = new HashMap();
  //tmpMap.put("count", "" + (rowCount-2));
  //excelContents.put("testsCount", tmpMap);
  return excelContents;
  }catch (Exception e){
  System.out.println("发生异常:" + e);
  }
  return null;
  }
  public  static File getExcelFileObj(String fPath){
  try{
  File fExl = new File(fPath);
  return fExl;
  }catch (Exception e){
  System.out.println(e);
  }
  return null;
  }
  }
  BeforeClass的处理
  (代码未经优化,有些地方可能写得不合理,请见谅!)
  //所有测试案例变量
  public Map<String, HashMap> allIdAuthTestCases;
  //测试案例文件路径--excel作为存储
  public String testCasePath = new TestDirUtils().getTestCasesDir() + "/IdAuthTestCases.xls";
  @BeforeClass
  public void beforeClass() {
  try{
  allIdAuthTestCases = ExcelUtil.excelToHashMapByFirstSheet(testCasePath, "身份证鉴权");
  }catch(Exception e){
  e.printStackTrace();
  }
  }
  @Test测试方法的处理
  IdAuth.java类代码
  (代码未经优化,有些地方可能写得不合理,请见谅!)
  package com.test.api;
  import static org.testng.Assert.assertFalse;
  import java.util.HashMap;
  import java.util.Map;
  import org.testng.Assert;
  import org.testng.annotations.Test;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.AfterClass;
  import com.test.pub.*;
  public class IdAuth {
  //所有测试案例变量
  public Map<String, HashMap> allIdAuthTestCases;
  //测试案例文件路径--excel作为存储
  public String testCasePath = new TestDirUtils().getTestCasesDir() + "/IdAuthTestCases.xls";
  @BeforeClass
  public void beforeClass() {
  try{
  allIdAuthTestCases = ExcelUtils.excelToHashMapByFirstSheet(testCasePath, "身份证鉴权");
  }catch(Exception e){
  e.printStackTrace();
  }
  System.out.println("allIdAuthTestCases:" + allIdAuthTestCases);
  }
  /*
  * *****************************  身份证鉴权   *******************************
  */
  /*
  * 姓名为空
  */
  @Test(timeOut=10000, description="姓名为空")
  public void testById_NameIsNull() {
  //测试案例编号
  String testCaseCode = "ById_NameIsNull";
  //测试数据
  HashMap<String, String> testCaseData = new HashMap();
  System.out.println("allIdAuthTestCases——>" + allIdAuthTestCases);
  System.out.println(allIdAuthTestCases.containsKey(testCaseCode));
  //判断是否存在当前案例数据
  try {
  if (allIdAuthTestCases.containsKey(testCaseCode)) {
  testCaseData = allIdAuthTestCases.get(testCaseCode);
  //执行测试案例
  //断言测试结果
  Assert.assertTrue(true);
  } else {
  Assert.fail("不存在当前测试方法的案例,请检查测试案例文件!");
  }
  } catch(Exception e) {
  e.printStackTrace();
  Assert.fail("测试案例获取失败");
  }
  }
  /*
  * 证件号为空
  */
  @Test(timeOut=10000, description="证件号为空")
  public void testById_IdNoIsNull() {
  //测试案例编号
  String testCaseCode = "ById_IdNoIsNull";
  //测试数据
  HashMap<String, String> testCaseData = new HashMap();
  //判断是否存在当前案例数据
  try {
  if (allIdAuthTestCases.containsKey(testCaseCode)) {
  testCaseData = allIdAuthTestCases.get(testCaseCode);
  //执行测试案例
  //断言测试结果
  Assert.assertTrue(false);
  } else {
  Assert.fail("不存在当前测试方法的案例,请检查测试案例文件!");
  }
  } catch(Exception e) {
  e.printStackTrace();
  Assert.fail("测试案例获取失败");
  }
  }
  /*
  * 姓名与证件号均正确
  */
  @Test(timeOut=10000, description="姓名与证件号均正确")
  public void testById_IdNoAndNameRight() {
  //测试案例编号
  String testCaseCode = "ById_IdNoAndNameRight";
  //测试数据
  HashMap<String, String> testCaseData = new HashMap();
  //判断是否存在当前案例数据
  try {
  if (allIdAuthTestCases.containsKey(testCaseCode)) {
  testCaseData = allIdAuthTestCases.get(testCaseCode);
  //执行测试案例
  //断言测试结果
  Assert.assertTrue(true);
  } else {
  Assert.fail("不存在当前测试方法的案例,请检查测试案例文件!");
  }
  } catch(Exception e) {
  e.printStackTrace();
  Assert.fail("测试案例获取失败");
  }
  }
  @AfterClass
  public void afterClass() {
  }
  }
  包com.test.pub下的TestDirUtils类代码
  (代码未经优化,有些地方可能写得不合理,请见谅!)
  package com.test.pub;
  public class TestDirUtils {
  // 获取主目录
  public String getMainDir() {
  String userDir = System.getProperty("user.dir");
  if (userDir.indexOf("target")>0) {
  userDir = userDir.split("target")[0];
  }
  int userDirLen = userDir.length();
  String subUserDir = userDir.substring(userDirLen-1, userDirLen);
  if (subUserDir.equals("/")) {
  userDir = userDir.substring(0, userDirLen-1);
  }
  return userDir;
  }
  // 获取用例目录路径
  public String getTestCasesDir() {
  String testCasesDir = getMainDir();
  if (testCasesDir.indexOf("target")>0) {
  testCasesDir = testCasesDir.split("target")[0];
  }
  int userDirLen = testCasesDir.length();
  String subUserDir = testCasesDir.substring(userDirLen-1, userDirLen);
  if (subUserDir.equals("/")) {
  testCasesDir = testCasesDir.substring(0, userDirLen-1);
  }
  return  testCasesDir + "/src/test/resources/testcases";
  }
  }
  运行结果:
  OK, 大功告成,基本实现一个简单的Maven+TestNG自动化测试框架。

Maven+TestNG+ReportNG/Allure接口自动化测试框架初探(上)的更多相关文章

  1. 基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport的接口自动化测试框架

    接口自动化框架 项目说明 本框架是一套基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport而设计的数据驱动接口自动化测试框架,TestNG ...

  2. API 接口自动化测试框架

    转自: https://testerhome.com/topics/3455 前言 接口自动化逐渐成为各大公司投入产出最高的测试技术.但是如何在版本迅速迭代过程中提高接口自动化的测试效率,仍然是大部分 ...

  3. 接口自动化测试框架【windows版】:jmeter + ant + jenkins

    为了提高回归效率及保证版本质量,很多公司都在做自动化测试,特别是接口自动化.接口自动化测试框架很多,有写代码的,也有不写代码的,我觉得没有谁比谁好,谁比谁高级之说,只要适用就好. 今天给大家分享一个不 ...

  4. 接口测试入门(4)--接口自动化测试框架 / list和map用法 / 随机选取新闻 (随机数生成) / 接口相关id映射

    一.接口自动化测试框架 为了更好的组织测试方法,测试用例并且持续集成,我们选择了  java+testNG(测试用例组织)+gitlab(代码版本管理)+Jenkins(持续集成工具) 作为一整套的自 ...

  5. 性能测试学习之路 (四)jmeter 脚本开发实战(JDBC &JMS &接口脚本 & 轻量级接口自动化测试框架)

    1.业务级脚本开发 登录脚本->思路:在线程组下新建两个HTTP请求,一个是完成访问登录页,一个是完成登录的数据提交.   步骤如下: 1) 访问登录页 2) 提交登录数据的HTTP PS:对于 ...

  6. ApiTesting全链路接口自动化测试框架 - 新增数据库校验(二)

    在这之前我完成了对于接口上的自动化测试:ApiTesting全链路接口自动化测试框架 - 初版(一) 但是对于很多公司而言,数据库的数据校验也尤为重要,另外也有小伙伴给我反馈希望支持. 所以最近几天我 ...

  7. python版接口自动化测试框架源码完整版(requests + unittest)

    python版接口自动化测试框架:https://gitee.com/UncleYong/my_rf [框架目录结构介绍] bin: 可执行文件,程序入口 conf: 配置文件 core: 核心文件 ...

  8. 接口自动化 [授客]基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0

    基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0   by:授客 QQ:1033553122     博客:http://blog.sina.com.cn/ishou ...

  9. 接口自动化 基于python+Testlink+Jenkins实现的接口自动化测试框架[V2.0改进版]

    基于python+Testlink+Jenkins实现的接口自动化测试框架[V2.0改进版]   by:授客 QQ:1033553122 由于篇幅问题,,暂且采用网盘分享的形式: 下载地址: [授客] ...

随机推荐

  1. CodeForces 767B The Queue

    模拟. 情况有点多,需要仔细.另外感觉题目的$tf$有点不太对......而且数据水了. $0$ $5$ $2$ $2$ $0$ $5$ 这组数据按照题意的话答案可以是$2$和$4$,但是好多错的答案 ...

  2. php通过mysqli链接mysql数据库

    首先,我们先来了解一下mysqli是什么,和mysql有什么区别? 1.mysqli是一个扩展库,是允许用户访问mysql4.1或更高版本所提供的功能: 1)mysqli连接是永久连接,而MySQL是 ...

  3. 【LA 3641】 Leonardo's Notebook (置换群)

    [题意] 给出26个大写字母组成 字符串B问是否存在一个置换A使得A^2 = B [分析] 置换前面已经说了,做了这题之后有了更深的了解. 再说说置换群.   首先是群. 置换群的元素是置换,运算时是 ...

  4. android 内存分哪些区

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com android 内存分哪些区 内存分哪些区 ============ 内存分为的5大区 1.栈区 ...

  5. 洛谷P2782 友好城市

    题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.没对友好城市都向政府申请在河上开辟一条直线航 ...

  6. [SDOI2005]反素数ant

    题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4,6 ...

  7. xcoj 1103 插线板(树链刨分求最大子段和)

    1103: 插线板 时间限制: 1 Sec  内存限制: 128 MB提交: 14  解决: 7 标签提交统计讨论版EditTestData 题目描述 从前有一堆古老的插线板,任意两个插线板之间只有一 ...

  8. 【最大团转最大点独立集(匈牙利算法+时间戳优化)】BZOJ2744-[HEOI2012]朋友圈

    [题目大意] 有两个国家A和B.存在以下朋友关系: 1.A国:每个人都有一个友善值,当两个A国人的友善值a.b,如果a xor b mod 2=1,那么这两个人都是朋友,否则不是: 2.B国:每个人都 ...

  9. cookie和localStorage、sessionStorage的区别

    先来讲讲localStorage吧,我最初接触localStorage,是听一个同学说他在做项目的过程中用到过这个.但是我自己也用到过的,就是在学习React的时候,在做一个小demo,这个demo简 ...

  10. Manthan, Codefest 16 A. Ebony and Ivory 水题

    A. Ebony and Ivory 题目连接: http://www.codeforces.com/contest/633/problem/A Description Dante is engage ...