呕心沥血-花一个星期的时间写的API接口测试汇总,包含测试计划,;测试流程。测试用例设计,执行接口测试,api测试报告,cookie,session,token鉴权原理以及实战,深入理解http,

目录

1.HTTP请求流程

1.1Request请求

1.1.1请求方法Request Method

1.1.2请求地址Request URL

1.1.3请求参数

1.1.4请求头Request Headers

1.2Response响应

1.2.1协议状态码

1.2.2响应数据

1.2.3响应头

2.接口测试工具Postman的使用

一.接口文档

1.怎么做接口测试

2.查看接口文档

二.Postman

1.使用Postman与接口文档做接口测试

1.1GET请求

请求参数:

 1.2Post请求

 2.Postman的使用

2.1Postman添加合集

2.2运行集合

2.3Postman字体大小设置

3.Postman断言

4.Postman中关于token请求的测试

4.1token请求流程:

4.2动态参数(关联)的解决思路

 5.Postman数据驱动

概念

解决的问题

实战

6.Postman测试报告的生成

下载安装node

安装newman

生成测试报告

2.1请求方法

 2.2请求地址

2.3请求头

2.4请求参数

2.4.1GET请求的请求参数(GET请求的请求参数与请求头中的请求参数格式没有任何关系)

2.4.2POST请求的请求参数 

 2.5Postman测试断言

3.接口测试工具JMeter的使用

3.1项目的创建

3.1.1线程组

3.1.2简单控制器:实现分组

3.1.3测试用例的添加

3.2请求方法

3.2 请求地址

3.3请求头

3.4请求数据

3.4.1 GET请求的请求数据

3.4.2 POST请求的请求数据

4.Postman与JMeter处理动态参数的步骤

4.1Postman处理动态参数

定义动态参数

调用动态参数使用{{}}

4.2JMeter处理动态参数

定义动态参数

调用动态参数

5.API测试报告的生成

5.1Postman测试报告

1.安装node.js

 2.安装newman

3.控制台生成测试报告

首先在postman中导出测试数据

控制台进入导出文件所在本地磁盘路径

生成测试报告

4.生成HTML的测试报告    

 安装HTML套件

生成html测试报告

5.输出htmlextra报告

安装htmlextra套件

运行htmlextra套件 

结果展示

配置好node.js与newman

导出测试用例文件

安装HTML套件

控制台进入文件所在路径

运行命令:newman run +文件名  -r  html

生成的测试报告在导出JSON文件的文件夹下,使用浏览器打开即可

5.2JMeter测试报告的生成

.配置ant

1.1认识ant

1.2ant的环境搭建

1.2.1验证环境变量

2.配置buid.xml文件

3.目录结构

3.1首先在JMeter的目录下创建一个tests的文件夹

3.2在tests下创建report和script文件夹

4.测试报告自动发送邮件

4.1下载自动发送邮件的jar包

4.2将下载的jar包放到ant目录下的lib目录下

5.修改JMeter的配置文件

6.移动ant-jmeter-1.1.1.jar文件

7.生成HTML的测试报告

7.1控制台进到我们之前创建的tests目录下

7.2输入ant即可生成测试报告

7.3生成的测试报告在我们之前创建的report下的HTML目录下

搭建好ant的环境

配置build.xml文件

配置好目录结构

控制台进入build.xml文件所在本地路径

控制台输入ant即可生成测试报告


很多刚开始学习接口自动化测试的小伙伴对凡书抱怨说:别的博主写的api接口测试太杂了,凡书能不能出一篇完整的api接口测试啊,呐,凡叔花了一个星期的时间给你们写出来了,喜欢的小伙伴记得三连哦。

1.HTTP请求流程

1.1Request请求

1.1.1请求方法Request Method

    • GET:客户端从服务端获取资源

    • POST:客户端往服务端发送请求添加新的资源

    • PUT:客户端针对服务端已有的数据进行更新

    • DELETE:客户端删除服务端已有的数据

    • CONNEC:HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器

    • OPTIONS:允许客户端查看服务器的特性

    • TRACE:回显服务器收到的请求,主要用于测试或诊断

    • HEAD:类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头

1.1.2请求地址Request URL

1.1.3请求参数

    • GET请求的请求参数

    • POST请求的请求参数

1.1.4请求头Request Headers

    • Content-Type:指的是请求参数的数据格式

    • Cookie:反爬虫,身份凭证

    • Referer:发送请求的地址是从哪里来的

    • User-Agent:发送网络请求的时候向服务端标注请求是通过什么浏览器或者什么软件(PostMan,JMeter)发送的

1.2Response响应

1.2.1协议状态码

    • 200 #请求成功

    • 201 #修改成功

    • 204  #删除成功

    • 301 #永久重定向
     

    • 302 #临时重定项

    • 400 Bad Request #客户端请求错误,请求头或请求参数不对

    • 401 Unauthorized #无权限访问该系统   

    • 403 Forbidden #有权限但是禁止访问   

    • 404 #请求的资源不存在
 ,请求的地址不存在,所以导致请求的资源也是不存在  

    • 405 #不被允许的请求方法
 ,比如这个接口的请求方法是GET请求,你使用的是POST请求  

    • 500 #服务器内部错误
     

    • 504 #GateWay Timeout 网关超时  

1.2.2响应数据

    • 响应数据的格式是由响应头中的Content-type决定的

1.2.3响应头

    • content-type:指明返回的响应数据的数据格式是什么

    • set-cookie:服务端返回给客户端的登录凭证  

2.接口测试工具Postman的使用

一.接口文档

1.怎么做接口测试

发送Request的请求信息以及Response的响应信息:
1、使用浏览器的network
2、使用charles的工具
3、查看开发的接口文档

2.查看接口文档

YAPI平台:http://yapi.smart-xwork.cn/

二.Postman

1.1GET请求

接口文档地址:

http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
接口文档:
GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string 
Host: ws.webxml.com.cn
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>

请求参数:

输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 
免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。

 1.2Post请求

1.2.1请求数据为xml格式

请求文档地址
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
请求文档
请求方法&请求头&请求地址
POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"
请求参数
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

  响应参数

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap:Body>
</soap:Envelope>

1.2.2请求数据为表单格式

接口文档地址
接口文档
请求方法&请求地址&请求头

POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length
请求参数

mobileCode=string&userID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
响应参数:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>

 2.Postman的使用

2.1Postman添加合集

2.2运行集合

2.3Postman字体大小设置

3.Postman断言

测试用例都需要加断言来判断测试的结果
在postman里面,编写断言使用的语言是:javascript,简称JS,编写的地方是在测试用例的tests区域

验证api测试用例,必须三个维度来验证,三个关系是并且的关系
    1、协议状态码
    2、业务状态码
    3、响应数据

4.Postman中关于token请求的测试

4.1token请求流程:

1.客户端使用账号密码登录到服务端
2.服务端将账号密码储存到服务端并返回一个令牌给客户端
3.客服端再有其他请求需要发送令牌给服务端

4.2动态参数(关联)的解决思路

1.首先通过登录的接口登录成功
2.通过响应数据拿到返回的授权
3.在需要授权登录的接口中定义变量(返回的授权)
4.在登录的接口中添加请求头调用变量,获取到登录接口拿到的授权
postman中调用变量名称,使用的是{{}}
5.先执行需要授权登录的接口,在执行登录接口
动态参数(关联)/上个接口的输出是下个输出的输入
1、授权登录接口输出了响应数据“授权登录令牌”
2、该“授权登录令牌”是登录接口的输入
6.必须要在集合中来执行接口,不能单独执行需要授权的接口
1、通过接口http://101.43.158.84:5000/auth,登录成功
2、登录成功后,在响应数据中返回认证授权

3、在该http://101.43.158.84:5000/auth的接口的tests中,定义变量获取access_token(授权的令牌)

4、下来在接口http://101.43.158.84:5000/index的请求中添加请求头,key为Authorization value为:jwt 获取到的授权的令牌,如Authorization:jwt {{token}}

5、下来执行的顺序必须是:
       先执行登录授权的接口http://101.43.158.84:5000/auth
       再执行http://101.43.158.84:5000/index的接口,这样就能够获取到调用变量的值

6、必须是在collection中执行,不能单独的执行http://101.43.158.84:5000/index接口,如果单独执行,依然是401,没授权

 5.Postman数据驱动

  • 概念

    那么在自动化测试中(工具&代码),把共有的数据分离出来,这个思想就是数据驱动的思想,如请求地址,那么我们可以把请求地址分离出来,
  • 解决的问题

    不管你的请求地址怎么变化,我只需要在一个地方进行维护
  • 实战

    有一个书籍管理的业务
    查看所有书籍:http://101.43.158.84:5000/v1/api/books
    添加书籍:http://101.43.158.84:5000/v1/api/books
      {
                "author": "wuya",
                "done": true,
                "name": "Python接口自动化测试实战"
        }
    查看具体的书:http://101.43.158.84:5000/v1/api/book/{{bookID}}
    删除书籍信息:http://101.43.158.84:5000/v1/api/book/{{bookID}}

    我们可以发现他们的请求地址是相同的,那么我们就可以把他们的请求地址分离出来,这样不管后期请求地址怎么变换,我们只需要再一个地方管理就可以

     断言的数据分离

6.Postman测试报告的生成

我们使用工具newman就可以生成测试报告(自动化测试的结果)
使用newman的前提是需要安装node.js,通过node.js来安装newman的工具
安装newman的命令:
npm install -g newman --registry=https://registry.npm.taobao.org
  • 下载安装node

    检验是否安装成功
    C:\Users\000>npm
    npm <command>
    
    Usage:
    
    npm install        install all the dependencies in your project
    npm install <foo>  add the <foo> dependency to your project
    npm test           run this project's tests
    npm run <foo>      run the script named <foo>
    npm <command> -h   quick help on <command>
    npm -l             display usage info for all commands
    npm help <term>    search for help on <term> (in a browser)
    npm help npm       more involved overview (in a browser)
    
    All commands:
    
        access, adduser, audit, bin, bugs, cache, ci, completion,
        config, dedupe, deprecate, diff, dist-tag, docs, doctor,
        edit, exec, explain, explore, find-dupes, fund, get, help,
        hook, init, install, install-ci-test, install-test, link,
        ll, login, logout, ls, org, outdated, owner, pack, ping,
        pkg, prefix, profile, prune, publish, rebuild, repo,
        restart, root, run-script, search, set, set-script,
        shrinkwrap, star, stars, start, stop, team, test, token,
        uninstall, unpublish, unstar, update, version, view, whoami
    
    Specify configs in the ini-formatted file:
        C:\Users\000\.npmrc
    or on the command line via: npm <command> --key=value
    
    More configuration info: npm help config
    Configuration fields: npm help 7 config
    
    npm@8.1.2 D:\Ruanjian\node_modules\npm
  • 安装newman

    C:\Users\特昂糖>npm install -g newman --registry=https://registry.npm.taobao.org
    npm WARN deprecated har-validator@5.1.5: this library is no longer supported
    npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
    npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
    npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
    
    added 128 packages in 13s
    npm notice
    npm notice New minor version of npm available! 8.1.2 -> 8.3.0
    npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0
    npm notice Run npm install -g npm@8.3.0 to update!
    npm notice

    验证是否安装成功

    C:\特昂糖>newman
    
    Usage: newman [options] [command]
    
    Options:
      -v, --version               output the version number
      -h, --help                  display help for command
    
    Commands:
      run [options] <collection>  Initiate a Postman Collection run from a given URL or path
    
    To get available options for a command:
      newman <command> -h
  • 生成测试报告

    • 导出测试数据

    • 进入测试数据所在本地磁盘

      C:\Users\000>cd D:\Zhuomian
      
      C:\Users\000>d:
      
      D:\Zhuomian>dir
       驱动器 D 中的卷是 本地磁盘
       卷的序列号是 60B8-2ACA
      
       D:\Zhuomian 的目录
      
      2022/01/08  23:45    <DIR>          .
      2022/01/08  23:45    <DIR>          ..
      2022/01/05  11:50    <DIR>          a
      2022/01/05  09:39       143,171,304 charlesproxy.rar
      2022/01/07  16:35        28,020,736 node-v16.13.1-x64.msi
      2022/01/08  23:45        26,442,630 node-v16.13.1-x64.rar
      2022/01/05  14:56       116,785,528 Postman-win64-8.7.0-Setup.exe
      2021/12/21  09:15    <DIR>          python
      2022/01/07  17:06             5,443 书籍管理.postman_collection.json
      2021/10/13  22:35    <DIR>          头像
      2022/01/04  17:37    <DIR>          红包
                     5 个文件    314,425,641 字节
                     6 个目录 95,791,566,848 可用字节
    • 生成测试报告

       View Code

2.1请求方法

 2.2请求地址

2.3请求头

2.4请求参数

2.4.1GET请求的请求参数(GET请求的请求参数与请求头中的请求参数格式没有任何关系)

2.4.2POST请求的请求参数 

  • JSON格式请求参数

  • 表单格式请求参数

 2.5Postman测试断言

3.接口测试工具JMeter的使用

3.1项目的创建

3.1.1线程组

相当于文件夹,在这个文件夹下可以创建新的测试用例或者新的文件夹

3.1.2简单控制器:实现分组

3.1.3测试用例的添加

3.2请求方法

3.2 请求地址

不需要写http,可以全部写到路径那里

3.3请求头

控制元件---HTTP信息头管理器

3.4请求数据

3.4.1 GET请求的请求数据

3.4.2 POST请求的请求数据

3.4.2.1JSON格式请求数据

3.4.2.2表单格式请求数据

3.4.2.3xml格式请求数据

4.Postman与JMeter处理动态参数的步骤

4.1Postman处理动态参数

    • 定义动态参数

    • 调用动态参数使用{{}}

4.2JMeter处理动态参数

    • 定义动态参数

      • JSON提取器

      • 正则表达式提取器:

    • 调用动态参数

5.API测试报告的生成

5.1Postman测试报告

我们使用工具newman就可以生成测试报告(自动化测试的结果)
使用newman的前提是需要安装node.js,通过node.js来安装newman的工具

安装newman的命令:
npm install -g newman --registry=https://registry.npm.taobao.org

1.安装node.js

  下载地址:Download | Node.js  下载适合自己电脑的版本,这里我们使用Windows平台

  安装:只需双击即可完成安装,在这里建议不要将路径放到C盘,这是一种安装软件的共识。并且在安装的过程中,安装向导已经帮我们完成了环境变量的注册,我们可以通过环境变量来查看

  验证是否配置成功:控制台输入npm,如果显示以下数据则表示安装并且配置成功

C:\Users\000>npm
npm <command>

Usage:

npm install        install all the dependencies in your project
npm install <foo>  add the <foo> dependency to your project
npm test           run this project's tests
npm run <foo>      run the script named <foo>
npm <command> -h   quick help on <command>
npm -l             display usage info for all commands
npm help <term>    search for help on <term> (in a browser)
npm help npm       more involved overview (in a browser)

All commands:

    access, adduser, audit, bin, bugs, cache, ci, completion,
    config, dedupe, deprecate, diff, dist-tag, docs, doctor,
    edit, exec, explain, explore, find-dupes, fund, get, help,
    hook, init, install, install-ci-test, install-test, link,
    ll, login, logout, ls, org, outdated, owner, pack, ping,
    pkg, prefix, profile, prune, publish, rebuild, repo,
    restart, root, run-script, search, set, set-script,
    shrinkwrap, star, stars, start, stop, team, test, token,
    uninstall, unpublish, unstar, update, version, view, whoami

Specify configs in the ini-formatted file:
    C:\Users\000\.npmrc
or on the command line via: npm <command> --key=value

More configuration info: npm help config
Configuration fields: npm help 7 config

npm@8.1.2 D:\Ruanjian\node_modules\npm   #这里显示的是软件所在本地磁盘路径

 2.安装newman

  node.js配置成功后我们就可以安装newman了,安装newman的命令为:npm install -g newman --registry=https://registry.npm.taobao.org

C:\Users\000>npm install -g newman --registry=https://registry.npm.taobao.org
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.

added 128 packages in 13s
npm notice
npm notice New minor version of npm available! 8.1.2 -> 8.3.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0
npm notice Run npm install -g npm@8.3.0 to update!
npm notice

   验证是否安装成功:

C:\000>newman

Usage: newman [options] [command]

Options:
  -v, --version               output the version number
  -h, --help                  display help for command

Commands:
  run [options] <collection>  Initiate a Postman Collection run from a given URL or path

To get available options for a command:
  newman <command> -h

3.控制台生成测试报告

4.生成HTML的测试报告    

  •  安装HTML套件

    newman 支持四种格式报告文件:cli,json,html,junit
    
    生成 html 报告时需要安装html套件,命令行中执行:
      npm install -g newman-reporter-html
         输出报告时使用的命令:
        -r html,json,junit         指定生成html,json,xml形式的测试报告
        --reporter-json-export jsonReport.json          生成json格式的测试报告
        --reporter-junit-export xmlReport.xml            生成xml格式的测试报告
        --reporter-html-export htmlReport.html          生成html格式的测试报告
        默认生成的测试报告保存在当前目录下,如果文件名前加上路径,则保存在指定的目录下
    
      例:输出json和html文件报告
  • 生成html测试报告

    输出命令:
           newman run 风暴平台.postman_collection.json -r html
    输出结果展示

5.输出htmlextra报告

  • 安装htmlextra套件

    npm install -g newman-reporter-htmlextra
  • 运行htmlextra套件 

    newman run 风暴平台.postman_collection.json -r htmlextra
  • 结果展示

  • 配置好node.js与newman

  • 导出测试用例文件

  • 安装HTML套件

    npm install -g newman-reporter-html
  • 控制台进入文件所在路径

  • 运行命令:newman run +文件名  -r  html

  • 生成的测试报告在导出JSON文件的文件夹下,使用浏览器打开即可

5.2JMeter测试报告的生成

.配置ant

1.1认识ant

ant下载地址:https://ant.apache.org/bindownload.cgi

   ant翻译过来是蚂蚁的意思,是优秀的Java构建工具。Apache Ant是一个Java库和命令行工具,其任务是驱动构建文件中描述为
相互依赖的目标和扩展点的进程。Ant 的主要已知用途是构建 Java 应用程序。Ant 提供了许多内置任务,允许编译、组装、测试和
运行 Java 应用程序。Ant 还可以有效地用于构建非 Java 应用程序,例如 C 或 C++ 应用程序。更一般地说,Ant可用于试验任
何类型的过程,这些过程可以用目标和任务来描述。再简单点,就是jmeter整合ant可以生成HTML的测试报告

1.2ant的环境搭建

解压zip压缩包,将压缩包放置你要放置的目录。我这里放置在D:\Ruanjian\apache-ant-1.10.1,目录结构如下:

 然后将bin目录配置到Path环境变量中:

1.2.1验证环境变量

C:>ant

Buildfile: build.xml does not exist!
Build failed

C:>ant -version

Apache Ant(TM) version 1.10.1 compiled on February 2 2017

2.配置buid.xml文件

这里演示的是使用sina邮箱发送,QQ邮箱接收
可以将账号这些换成自己的
<?xml version="1.0" encoding="UTF8"?>

<project name="ant-jmeter-test" default="run" basedir=".">
<!--jmeter所在本地磁盘路径-->
<property name="jmeterPath" value="D:\Ruanjian\apache-jmeter-5.4.3"/>
<!--邮箱的配置-->
<property name="mailhost" value="smtp.sina.cn"/>
<!--发送邮件的邮箱账号-->
<property name="username" value="******@sina.com"/>
<!--发送邮件的邮箱密码-->
<property name="password" value="*****"/>
<!--发送邮件的账号-->
<property name="mailfrom" value="******@sina.com"/>
<!--接收邮件的邮箱账号-->
<property name="mail_to" value="******41@qq.com"/>
<!--发送邮件的主题-->
<property name="mailsubject" value="API测试报告"/>
<!--sina邮箱的端口-->
<property name="mail_port" value="25"/>
<!--发送邮件的正文-->
<property name="message" value="Hi!请查收API测试报告,如有任何疑问,请联系我!"/>


    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
     </tstamp>
     <property name="jmeter.home" value="${jmeterPath}" />
     <property name="jmeter.result.jtl.dir" value="${jmeterPath}\tests\report\jtl" />
     <property name="jmeter.result.html.dir" value="${jmeterPath}\tests\report\html" />
     <property name="htmlReportNameSummary" value="testReport" />
     <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${htmlReportNameSummary}${time}.jtl" />
     <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${htmlReportNameSummary}${time}.html" />

     
    <target name="run">
         <antcall target="test" />
         <antcall target="report" />
         <antcall target="sendEmail" />
     </target>
     
     <!--执行接口测试-->
     <target name="test">
          <echo>执行接口自动化测试</echo>
         <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
         <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
            <!--要执行的测脚本的路径与文件名-->
            <testplans dir="${jmeterPath}\tests\script" includes="TestDev.jmx" />
             <property name="jmeter.save.saveservice.output_format" value="xml"/>
         </jmeter>
     </target>
     
     <!--解决报告中NAN字段显示问题-->
     <path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan-2.7.2.jar"/>
        <fileset dir="${jmeter.home}/lib" includes="serializer-2.7.2.jar"/>
    </path>

     <!--生成HTML测试报告-->
     <target name="report">
     <echo>生成接口自动测试报告</echo>
         <xslt classpathref="xslt.classpath"
              force="true"
              in="${jmeter.result.jtlName}" out="${jmeter.result.htmlName}"
              style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"  />
        
        <!--复制图片-->
        <copy todir="${jmeter.result.html.dir}">
             <fileset dir="${jmeter.home}/extras">
                 <include name="collapse.png" />
                 <include name="expand.png" />
             </fileset>
        </copy>
     </target>
      <!--自动发送邮件-->
    <target name="sendEmail">
    <echo>发送自动化测试报告</echo>
         <mail mailhost="${mailhost}"
            ssl="ture"
            user="${username}" 
            password="${password}" 
            mailport="${mail_port}"
            subject="${mailsubject}" 
            messagemimetype="text/html" 
            tolist="${mail_to}">
         <from address="${mailfrom}" />
               <attachments>
                     <fileset dir="${jmeter.result.html.dir}">
                             <include name="${htmlReportNameSummary}${time}.html"/>
                             <include name="collapse.png" />
                             <include name="expand.png" />
                     </fileset>
               </attachments>
               <message>
               ${message}
               </message>
         </mail>
     </target>
 </project>

3.目录结构

3.1首先在JMeter的目录下创建一个tests的文件夹

tests中存放build.xml文件,测试脚本与生成的测试报告等

3.2在tests下创建report和script文件夹

report中存放测试报告,里面创建HTML与JTL存放不同格式的测试报告
script中存放测试的脚本

4.测试报告自动发送邮件

4.1下载自动发送邮件的jar包

https://files.cnblogs.com/files/blogs/713119/JMeter%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A%E8%87%AA%E5%8A%A8%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E6%8F%92%E4%BB%B6.rar

4.2将下载的jar包放到ant目录下的lib目录下

5.修改JMeter的配置文件

打开jmeter的bin目录下的jmeter.properties文件
做如下修改: 
把jmeter.save.saveservice.output_format=csv修改为jmeter.save.saveservice.output_format=xml

需要删除前面的#号

6.移动ant-jmeter-1.1.1.jar文件

在apache-jmeter的extras下找到ant-jmeter-1.1.1.jar的文件移动到apache-ant-1.10.0下的lib目录下面

7.生成HTML的测试报告

7.1控制台进到我们之前创建的tests目录下

C:\Users\特昂糖>cd D:\Ruanjian\apache-jmeter-5.4.3\tests

C:\Users\特昂糖>d:

D:\Ruanjian\apache-jmeter-5.4.3\tests>

7.2输入ant即可生成测试报告

如果出现以下构建失败的错误表示没有权限,使用Windows系统以管理员身份运行控制台即可解决
BUILD FAILED
D:\Ruanjian\apache-jmeter-5.4.3\tests\build.xml:29: The following error occurred while executing this line:
D:\Ruanjian\apache-jmeter-5.4.3\tests\build.xml:56: input file D:\Ruanjian\apache-jmeter-5.4.3\tests\report\jtl\testReport202201171026.jtl does not exist
Buildfile: D:\Ruanjian\apache-jmeter-5.4.3\tests\build.xml

run:

test:
     [echo] 执行接口自动化测试
   [jmeter] Executing test plan: D:\Ruanjian\apache-jmeter-5.4.3\tests\script\TestDev.jmx ==> D:\Ruanjian\apache-jmeter-5.4.3\tests\report\jtl\testReport202201171030.jtl
   [jmeter] 2022-01-17 22:30:01,908 main ERROR FileManager (jmeter.log) java.io.FileNotFoundException: jmeter.log (拒绝访问。) java.io.FileNotFoundException: jmeter.log (拒绝访问。)
   [jmeter]     at java.io.FileOutputStream.open0(Native Method)
   [jmeter]     at java.io.FileOutputStream.open(FileOutputStream.java:270)
   [jmeter]     at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory.createManager(FileManager.java:438)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory.createManager(FileManager.java:422)
   [jmeter]     at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:114)
   [jmeter]     at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileManager.getFileManager(FileManager.java:182)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:96)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:52)
   [jmeter]     at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1120)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1045)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1037)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:651)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:247)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:293)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:626)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:699)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:716)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:270)
   [jmeter]     at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
   [jmeter]     at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
   [jmeter]     at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
   [jmeter]     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:137)
   [jmeter]     at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:55)
   [jmeter]     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
   [jmeter]     at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:33)
   [jmeter]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363)
   [jmeter]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388)
   [jmeter]     at org.apache.jmeter.JMeter.<clinit>(JMeter.java:126)
   [jmeter]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   [jmeter]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
   [jmeter]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   [jmeter]     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   [jmeter]     at org.apache.jmeter.NewDriver.main(NewDriver.java:252)
   [jmeter]
   [jmeter] 2022-01-17 22:30:01,915 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.FileAppender for element File: java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@49139829] unable to create manager for [jmeter.log] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@9597028] java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@49139829] unable to create manager for [jmeter.log] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@9597028]
   [jmeter]     at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:116)
   [jmeter]     at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileManager.getFileManager(FileManager.java:182)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:96)
   [jmeter]     at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:52)
   [jmeter]     at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1120)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1045)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1037)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:651)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:247)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:293)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:626)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:699)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:716)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:270)
   [jmeter]     at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
   [jmeter]     at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
   [jmeter]     at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
   [jmeter]     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:137)
   [jmeter]     at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:55)
   [jmeter]     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
   [jmeter]     at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:33)
   [jmeter]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363)
   [jmeter]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388)
   [jmeter]     at org.apache.jmeter.JMeter.<clinit>(JMeter.java:126)
   [jmeter]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   [jmeter]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
   [jmeter]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   [jmeter]     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   [jmeter]     at org.apache.jmeter.NewDriver.main(NewDriver.java:252)
   [jmeter]
   [jmeter] 2022-01-17 22:30:01,949 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.FileAppender for element File: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.FileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.FileAppender
   [jmeter]     at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:236)
   [jmeter]     at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:134)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1120)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1045)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1037)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:651)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:247)
   [jmeter]     at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:293)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:626)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:699)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:716)
   [jmeter]     at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:270)
   [jmeter]     at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
   [jmeter]     at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
   [jmeter]     at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
   [jmeter]     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:137)
   [jmeter]     at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:55)
   [jmeter]     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
   [jmeter]     at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:33)
   [jmeter]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363)
   [jmeter]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388)
   [jmeter]     at org.apache.jmeter.JMeter.<clinit>(JMeter.java:126)
   [jmeter]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   [jmeter]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
   [jmeter]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   [jmeter]     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   [jmeter]     at org.apache.jmeter.NewDriver.main(NewDriver.java:252)
   [jmeter]
   [jmeter] 2022-01-17 22:30:01,957 main ERROR Null object returned for File in Appenders.
   [jmeter] 2022-01-17 22:30:01,965 main ERROR Unable to locate appender "jmeter-log" for logger config "root"
   [jmeter] Creating summariser <summary>
   [jmeter] Created the tree successfully using D:\Ruanjian\apache-jmeter-5.4.3\tests\script\TestDev.jmx
   [jmeter] Starting standalone test @ Mon Jan 17 22:30:02 CST 2022 (1642429802382)
   [jmeter] Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
   [jmeter] summary +      1 in 00:00:01 =    1.6/s Avg:   420 Min:   420 Max:   420 Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0
   [jmeter] summary +     23 in 00:00:05 =    4.2/s Avg:   236 Min:   105 Max:  1631 Err:     1 (4.35%) Active: 0 Started: 1 Finished: 1
   [jmeter] summary =     24 in 00:00:06 =    3.9/s Avg:   243 Min:   105 Max:  1631 Err:     1 (4.17%)
   [jmeter] Tidying up ...    @ Mon Jan 17 22:30:08 CST 2022 (1642429808769)
   [jmeter] ... end of run

report:
     [echo] 生成接口自动测试报告

BUILD FAILED
D:\Ruanjian\apache-jmeter-5.4.3\tests\build.xml:29: The following error occurred while executing this line:
D:\Ruanjian\apache-jmeter-5.4.3\tests\build.xml:56: input file D:\Ruanjian\apache-jmeter-5.4.3\tests\report\jtl\testReport202201171030.jtl does not exist

Total time: 8 seconds

7.3生成的测试报告在我们之前创建的report下的HTML目录下

总的测试用例个数:16,成功率:93.75%,失败率:6%,
失败原因:自己代码写的不对,不是程序的问题覆盖场景:登录,产品管理,地址管理
结论:本次自动化测试完整的覆盖了登录业务,产品管理业务,地址管理业务,所有的测试场景测试通过
  • 搭建好ant的环境

  • 配置build.xml文件

  • 配置好目录结构

  • 控制台进入build.xml文件所在本地路径

  • 控制台输入ant即可生成测试报告

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值