接口自动化工具(测试,编写,生成)

[TOC]

coupler

gitos: https://gitee.com/mliang/coupler

graph TB

subgraph 服务中转
browser1[前端]--请求-->coupler1[coupler]
coupler1[coupler]--返回-->browser1[前端]
coupler1[coupler]--请求-->server1[后端]
server1[后端]--返回-->coupler1[coupler]
end

subgraph 服务模拟
coupler2[coupler]--返回-->browser2[前端]
browser2[前端]--请求-->coupler2[coupler]
end

subgraph 接口测试
coupler3[coupler]--请求-->server3[后端]
server3[后端]--返回-->coupler3[coupler]
end
graph LR

coupler((coupler))-->文档生成
coupler-->历史文档比较

开发记录

1.0 一阶段

  • 基础运行框架搭建
  • 基础测试框架搭建
  • 基础模块构建
  • 测试模块开发

1.0 架构

graph TB

subgraph core
config
document
interface
params
result
server
end
graph TB

subgraph extend
swagger
graphql
end
graph TB

subgraph tools
compare
end

核心 coupler.core

配置 core.config
  • 接口文档目录
  • 接口文档生成目录
  • 通过代理接口访问,生成接口文档
  • server服务,数据来源:远程接口数据,本地数据,mock产生
  • server服务,将远程数据或mock数据保存本地
文档 core.document
加载 load.base
加载 load.openapi
接口 core.interface
扫描 scanner
访问 access
信息 core.message
记录 record
输出 export
访问参数 core.params
比较 compare
返回结果 core.result
比较 compare

工具 coupler.tools

tools.document
转换 trans4openapi
转换 trans4base

扩展 coupler.extend

加载 load.swagger.yaml
加载 load.swagger.json

功能流程

接口测试
  • require
  • [x] 接口文档
  • [x] 远程接口服务器
  • action
  • [x] session的记录和共享
  • [x] 参数组织
  • [x] 返回数据校验
  • [x] 结果输出
  • 参数校验内容
  • [x] 长度 (defalut:100)
  • [x] 非空 (defalut:false)
  • [x] 类型 (defalut:null)
  • [x] 格式 (defalut:null)
  • 返回结果校验内容
  • [x] 非空 (defalut:true)
  • [x] 类型 (defalut:null)
  • [x] 格式 (defalut:null)
  • 数据类型
  • [x] string
  • [x] number
  • [x] boolean
  • [x] array
  • [x] object
  • [x] date(defalut:'yyyy-MM-dd HH:mm:ss')
  • [x] timestamp
  • 数据格式
  • [x] 手机
  • [x] 邮箱
graph TB

1[加载接口文档]
2[加载接口列表]
3{批量扫描接口}
4[记录dead接口]
5[访问alive接口]
6[准备参数]
7[访问接口]
8[比较返回数据]
9[记录比较结果]
10[整理记录并输出]

1-->2
2-->3
3--dead-->4
3--alive-->5
5-->6
6-->7
7-->8
8-->9
9-->10
4-->10
  1. 加载、解析接口文档
  2. 加载接口列表
  3. 批量扫描接口,返回接口状态(alive,dead)
  4. 记录dead接口
  5. 访问alive接口
    1. 准备参数
    2. 访问接口
    3. 比较返回数据
    4. 记录比较结果
  6. 整理记录并输出
服务
graph TB

1[加载接口文档]
2[加载接口列表]
3[启动服务器并监听]
4[接口访问]
5{接口是否存在}
6{参数是否正确}
7{是否远程接口访问}
8[扫描远程接口]
a{远程接口是否存在}
9{访问远程}
10[比较返回数据]
b{返回数据是否符合规范}
11[返回数据]
12{是否本地模拟接口}
13{是否存在本地数据}
14[组织mock数据]
15[加载本地数据]
16{是否将mock数据保存在本地}
17[将mock数据保存在本地]

20>记录错误信息]
21[输出错误信息]
1-->2
2-->3
3-->4
4-->5
5--否-->20
5--是-->6
6--否-->20
6--是-->7
7--是-->8
8-->a
a--否-->20
a--是-->9
9--访问失败-->20
9-->10
10-->b
b--否-->20
b--是-->11
7--否-->12
12--是-->13
13--是-->15
15-->11
13--否-->14
14-->11
14-->16
16--是-->17

11-->21
  1. 加载、解析接口文档
  2. 加载接口列表
  3. 启动服务器并监听
  4. 接口访问
    1. 判断接口是否存在,记录错误信息
    2. 判断参数是否正确,记录错误信息
    3. 是否远程接口访问
      1. 扫描远程接口,记录错误信息
      2. 访问远程接口,记录错误信息
      3. 比较返回数据,记录错误信息
      4. 是否需要将数据保存在本地
      5. 返回数据
    4. 是否本地模拟接口
      1. 判断是否存在本地数据
      2. 组织mock数据
      3. 是否需要将mock数据保存在本地
      4. 返回数据

2.0 架构

扩展 extend

extend.graphql
加载 load.graphql
转换 trans4graphql

文档结构

数据结构

coupler 数据结构
  • 接口文档目录结构
- apis
    - [{version}]
        - api.json
        - [{interface}.json]
  • api.json 结构
- servers
    - url (string)
    - description (string)
- info 
    - description (string)
    - version (string)
- paths
    - /{path} 
- Components
    - schemas 
    - parameters
    - responses
  • {interface}.json 结构
- /{path} 
    - get|post|put|delete|options|head|patch|trace
        - description (string)    
        - parameters
            - name (string)
            - in (string:"query","header","path","cookie",default:get|) 
            - required (boolean,default:false)
            - description (string)
        - responses
            - default|HTTP Status Code
                - description
                - content
                    - application/json | text/plain
                        - schema 
                            - type
                            - properties
                                - key
                                    - type
                                    - format
基本数据结构
- servers
    - url (string)
    - description (string)
- info 
    - description (string)
    - version (string)
- paths
    - /{path} 
        - get|post|put|delete|options|head|patch|trace
            - description (string)    
            - parameters
                - name (string)
                - in (string:"query","header","path","cookie") 
                - required (boolean)
                - description (string)
            - responses
                - default|HTTP Status Code
                    - description
                    - content
                        - application/json | text/plain
                            - schema 
                                - type
                                - properties
                                    - key
                                        - type
                                        - format
- Components
    - schemas 
    - parameters
    - responses

需求

1.0 无界面版

  • 接口文档利于编写
  • 接口文档利于更新
  • 接口文档格式规范化
  • 接口文档适用于前后端
  • 记录文档改动
  • 遵循openapi3.0接口规范
后端接口测试
  • 接口可测试
  • 测试自动化
  • 测试结果可输出
前端访问接口
  • 工具提供接口模拟访问(无后端)
  • 工具提供后端接口转发
  • 前端接口访问结果输出

1.1 功能扩展

  • 测试可编排
  • 定时任务测试
  • 通过代理远程接口访问,生成接口文档

2.0 界面版

  • 结果可展示
  • 接口可展示
  • 可在线测试
  • graphql规范接入

3.0 代码生成版

  • 生成接口mock数据代码
  • 生成前后端接口代码

其他

文档版本差异比较
  • 生成统一文档并保存,以时间区分
  • 提供文档版本追加并记录
  • 提供文档版本差异比较内容
测试内容
  • 后端接口是否可访问
  • 后端接口返回是否正确
  • 后端接口是否对入参校验
  • 前端访问是否正确
结果内容
  • 接口访问是否正常
  • 接口响应耗时
  • 访问不正常前端原因
  • 响应不正常后端原因

资料

http状态码

1 消息

▪ 100 Continue
▪ 101 Switching Protocols
▪ 102 Processing

2 成功

▪ 200 OK
▪ 201 Created
▪ 202 Accepted
▪ 203 Non-Authoritative Information
▪ 204 No Content
▪ 205 Reset Content
▪ 206 Partial Content
▪ 207 Multi-Status

3 重定向

▪ 300 Multiple Choices
▪ 301 Moved Permanently
▪ 302 Move temporarily
▪ 303 See Other
▪ 304 Not Modified
▪ 305 Use Proxy
▪ 306 Switch Proxy
▪ 307 Temporary Redirect

4 请求错误

▪ 400 Bad Request
▪ 401 Unauthorized
▪ 402 Payment Required
▪ 403 Forbidden
▪ 404 Not Found
▪ 405 Method Not Allowed
▪ 406 Not Acceptable
▪ 407 Proxy Authentication Required
▪ 408 Request Timeout
▪ 409 Conflict
▪ 410 Gone
▪ 411 Length Required
▪ 412 Precondition Failed
▪ 413 Request Entity Too Large
▪ 414 Request-URI Too Long
▪ 415 Unsupported Media Type
▪ 416 Requested Range Not Satisfiable
▪ 417 Expectation Failed
▪ 421 too many connections
▪ 422 Unprocessable Entity
▪ 423 Locked
▪ 424 Failed Dependency
▪ 425 Unordered Collection
▪ 426 Upgrade Required
▪ 449 Retry With
▪ 451 Unavailable For Legal Reasons

5 服务器错误

▪ 500 Internal Server Error
▪ 501 Not Implemented
▪ 502 Bad Gateway
▪ 503 Service Unavailable
▪ 504 Gateway Timeout
▪ 505 HTTP Version Not Supported
▪ 506 Variant Also Negotiates
▪ 507 Insufficient Storage
▪ 509 Bandwidth Limit Exceeded
▪ 510 Not Extended
▪ 600 Unparseable Response Headers

swagger

image

openapi

image

- openapi (string)
- info (Info Object)
    - title (string)
    - description (string)
    - termsOfService (string)
    - contact (Contact Object)
        - name (string)
        - url (string)
        - email (string)
    - license (License Object)
        - name (string)
        - url (string)
    - version (string)
- servers ([Server Object])
    - url (string)
    - description (string)
    - variables (Map[string, Server Variable Object])
        - enum ([string])
        - default (string)
        - description (string)
- paths ([Paths Object])
    - /{path} (Path Item Object)
        - $ref (string)
        - summary (string)
        - description (string)
        - get (Operation Object)
            - tags ([string])
            - summary (string)
            - description (string)
            - externalDocs (External Documentation Object)
                - description (string)
                - url (string)
            - operationId (string)
            - parameters ([Parameter Object|Reference Object])
                - name (string)
                - in (string:"query","header","path","cookie") 
                - description (string)
                - required (boolean)
                - deprecated (boolean)
                - allowEmptyValue (boolean)
                - style (string:more)
                - explode (boolean)
                - allowReserved (boolean)
                - schema (Schema Object | Reference Object)
                - example (Any)
                - examples (Map[ string, Example Object | Reference Object])
                    - summary (string)
                    - description (string)
                    - value (Any)
                    - externalValue (string)
                - content (Map[string, Media Type Object])
            - requestBody (Request Body Object|Reference Object)
                - description (string)
                - content (Map[string, Media Type Object])
                    - schema (Schema Object | Reference Object)
                    - example (Any)
                    - examples (Map[ string, Example Object | Reference Object])
                    - encoding (Map[string, Encoding Object])
                        - contentType (string)
                        - headers (Map[string, Header Object | Reference Object])
                        - style (string)
                        - explode (boolean)
                        - allowReserved (boolean)
                - required (boolean)
            - responses (Responses Object)
                - default|HTTP Status Code (Response Object | Reference Object)
                    - description (string)
                    - headers (Map[string, Header Object | Reference Object])
                    - content (Map[string, Media Type Object])
                    - links (Map[string, Link Object | Reference Object])
                        - operationRef (string)
                        - operationId (string)
                        - parameters (Map[string, Any | {expression}])
                        - requestBody (Any | {expression})
                        - description (string)
                        - server (Server Object)
            - callbacks (Map[string, Callback Object|Reference Object])
                - {expression} (Path Item Object)
            - deprecated (boolean)
            - security ([Security Requirement Object])
                - type (string)
                - description (string)
                - name (string)
                - in (string)
                - scheme  (string)
                - bearerFormat  (string)
                - flows (OAuth Flows Object)
                    - implicit (OAuth Flow Object)
                        - authorizationUrl (string)
                        - tokenUrl (string)
                        - refreshUrl (string)
                        - scopes (Map[string, string])
                    - password (OAuth Flow Object)
                    - clientCredentials (OAuth Flow Object)
                    - authorizationCode (OAuth Flow Object)
                - openIdConnectUrl  (string)
            - servers ([Server Object])
        - put (Operation Object)
        - post (Operation Object)
        - delete (Operation Object)
        - options (Operation Object)
        - head (Operation Object)
        - patch (Operation Object)
        - trace (Operation Object)
        - servers [Server Object]
        - parameters [Parameter Object | Reference Object]
- components (Components Object)
    - schemas (Map[string,Schema Object|Reference Object])
        - nullable (boolean)
        - discriminator (Discriminator Object)
            - propertyName (string)
            -  mapping (Map[string, string])
        - readOnly (boolean)
        - writeOnly (boolean)
        - xml (XML Object)
            - name (string)
            - namespace (string)
            - prefix (string)
            - attribute (boolean)
            - wrapped (boolean)
        - externalDocs (External Documentation Object)
        - example (Any)
        - deprecated (boolean)
    - responses (Map[string,Response Object|Reference Object])
    - parameters (Map[string,Parameter Object|Reference Object])
    - examples (Map[string,Example Object|Reference Object])
    - requestBodies (Map[string,Request Body Object|Reference Object])
    - headers (Map[string,Header Object|Reference Object])
    - securitySchemes (Map[string,Security Scheme Object|Reference Object])
    - links (Map[string,Link Object|Reference Object])
    - callbacks (Map[string,Callback Object|Reference Object])
- security ([Security Requirement Object])
- tags ([Tag Object])
    - name
    - description
    - externalDocs (External Documentation Object)
- externalDocs (External Documentation Object)

Header Object follows the structure of the Parameter Object

转载于:https://my.oschina.net/u/924762/blog/2996928

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值