Fork me on GitHub

版权声明 本站原创文章 由 萌叔 发表
转载请注明 萌叔 | https://vearne.cc

1. 引言

萌叔手上的API服务不少,核心的项目都有CI和单元测试。
但是毕竟在单元测试中,对外部数据库、微服务的调用都是mock的,缺乏对整个链路的自动化测试
这段时间开发了一个自动化测试框架 vearne/autotest
初步实现了对HTTP协议API服务的支持,后期应该还会支持gRPC协议的API服务

2. 框架的优势

  • 无需进行程序开发,只需要编写配置文件
  • 可以指定testcase之间的依赖关系
  • 无依赖关系的testcase可以并发执行,执行速度更快
  • 使用xpath提取变量,书写方便
  • 支持从文件中导入变量,支持从response中提取变量

3. 完整的示例

3.1 启动API服务

使用docker compose启动一个HTTP RESTful API服务

cd docker-compose
docker compose up -d

这个服务是book管理服务,它支持对book的增删改查

添加

curl -X POST 'http://localhost:8080/api/books' \
--header 'Content-Type: application/json' \
--data '{"title": "book3_title", "author": "book3_author"}'

接口返回

{
     "id": 3,
     "title": "book3_title",
     "author": "book3_author"
}

修改

curl -X PUT 'localhost:8080/api/books/3' \
--header 'Content-Type: application/json' \
--data '{"title": "book3_title", "author": "book3_author-2"}'

接口返回

{
     "id": 3,
     "title": "book3_title",
     "author": "book3_author-2"
}

3.2 自动化测试

autotest run -c=./config_files/autotest.yml -e=./config_files/.env.dev

自动化测试中的每一个测试用例都是

  • 1) 针对API服务的请求
  • 2) 针对响应的一组断言

my_http_api.yml

测试用例【2】

- id: 2
  request:
    # optional
    method: "post"
    url: "http://{{ HOST }}/api/books"
    headers:
      - "Content-Type: application/json"
    body: |
      {
          "title": "book3_title",
          "author": "book3_author"
      }
  rules:
    - name: "HttpStatusEqualRule"
      expectedStatus: 200
    - name: "HttpBodyEqualRule"
      xpath: "/title"
      Expected: "book3_title"
  export:
    xpath: "/id"
    # Extract the id value and export it to the variable MY_BOOK_ID
    exportTo: "MY_BOOK_ID"
    # default is string, optional value: integer | string | float
    type: integer

测试用例【2】对接口发出POST请求,添加了一本书,并且断言
1) HTTP的状态码是200
2) response的body中的title字段的值是”book3_title”
这里字段提取使用了XPath,XPath通常被用于对xml文件的解析,这里萌叔把它引申到对JSON字符串的处理
关于Xpath的语法详见参考资料1

对于测试用例【2】,萌叔还想使用接口返回中的某个值,所以后面萌叔export了其中的id值,
把它导出到变量MY_BOOK_ID,在后面的测试用例中,我们会用到它

测试用例【3】

- id: 3
  # Depends on TestCase2
  # TestCase2 must be executed first
  dependOnIDs: [2]
  request:
    # optional
    method: "put"
    # The user-defined variable HOST and the variable MY_BOOK_ID
    # obtained in TestCase2 are used here.
    url: "http://{{ HOST }}/api/books/{{ MY_BOOK_ID }}"
    headers:
      - "Content-Type: application/json"
    body: |
      {
          "title": "book3_title",
          "author": "book3_author-2"
      }
  rules:
    - name: "HttpStatusEqualRule"
      expectedStatus: 200
    - name: "HttpBodyEqualRule"
      xpath: "/author"
      Expected: "book3_author-2"

测试用例【3】依赖测试用例【2】,修改书本的作者,
这里断言http响应的状态码是200,author字段的值是”book3_author-2″

最佳实践

  • 建议对于有读有写的场景下,可以使用docker compose来构建一个干净的环境进行自动化测试
  • 要想查看自动化测试过程中,与API服务交互完整的输入输出,可以修改配置文件debug
global:
#  worker_num: 5
  worker_num: 1
  # default: true
  ignore_testcase_fail: true
  # 将debug改为true
  debug: false 
  • 对于测试用例较多的情况,请修改worker_num的值
    同一个文件内部的测试用例是并发的

注意: 同一个文件,测试用例的ID不允许重复

参考资料

1.XPath Syntax


微信公众号

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据