gpt4 book ai didi

如何使用 Teprunner 测试平台编写从登录到下单的大流程接口自动化用例

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 33 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章如何使用 Teprunner 测试平台编写从登录到下单的大流程接口自动化用例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

如何使用 Teprunner 测试平台编写从登录到下单的大流程接口自动化用例

大家好,我是刚哥.

对于登录→搜索商品→添加购物车→下单→支付这样的大流程,使用teprunner测试平台该如何编写接口自动化用例呢?

假设这几个节点的接口和参数如下:

  1. # 登录
  2. url:/login
  3. method: post
  4. body: {"username": "dongfanger", "password": "123456"}
  5. response: {"token": "de2e3ffu29"}
  6. # 搜索商品
  7. url:/searchSku
  8. method: get
  9. headers: {"token": "de2e3ffu29"}
  10. body: {"skuName": "电子书"}
  11. response: {"skuId": "222", "price": "2.3"}
  12. # 添加购物车
  13. url:/addCart
  14. method: post
  15. headers: {"token": "de2e3ffu29"}
  16. body: {"skuId": "222", "skuNum": "3"}
  17. response: {"skuId": "222", "price": "2.3", "skuNum": "3", "totalPrice": "6.9"}
  18. # 下单
  19. url:/order
  20. method: post
  21. headers: {"token": "de2e3ffu29"}
  22. body: {"skuId": "222", "price": "2.3", "skuNum": "3", "totalPrice": "6.9"}
  23. response: {"orderId": "333"}
  24. # 支付
  25. url:/pay
  26. method: post
  27. headers: {"token": "de2e3ffu29"}
  28. body: {"orderId": "333", "payAmount": "6.9"}
  29. response: {"success": "true"}

我不会直接在teprunner测试平台的界面上直接写新用例的代码,而是先在PyCharm中把代码写好调通,再手动复制粘贴或者经过Git同步到平台上。本文就来介绍下怎么在PyCharm中借助tep编写这个大流程的接口自动化用例.

先使用命令检查tep已经是0.9.1以上版本:

  1. $ tep -V
  2. 0.9.1

然后初始化项目:

  1. $ tep startproject login-pay-big-process
  2. 2021-12-27 12:41:26.721 | INFO | tep.scaffold:create_scaffold:53 - Create new project: login-pay-big-process
  3. Project root dir: /Users/dongfanger/PycharmProjects/login-pay-big-process
  4. Created folder: login-pay-big-process
  5. Created folder: login-pay-big-process/fixtures
  6. Created folder: login-pay-big-process/tests
  7. Created folder: login-pay-big-process/files
  8. Created folder: login-pay-big-process/reports
  9. Created file: login-pay-big-process/.gitignore
  10. Created file: login-pay-big-process/conf.yaml
  11. Created file: login-pay-big-process/conftest.py
  12. Created file: login-pay-big-process/pytest.ini
  13. Created file: login-pay-big-process/requirements.txt
  14. Created file: login-pay-big-process/fixtures/__init__.py
  15. Created file: login-pay-big-process/fixtures/fixture_admin.py
  16. Created file: login-pay-big-process/fixtures/fixture_env_vars.py
  17. Created file: login-pay-big-process/fixtures/fixture_login.py
  18. Created file: login-pay-big-process/fixtures/fixture_your_name.py
  19. Created file: login-pay-big-process/tests/__init__.py
  20. Created file: login-pay-big-process/tests/test_login.py
  21. Created file: login-pay-big-process/tests/test_post.py
  22. Created file: login-pay-big-process/tests/test_mysql.py
  23. Created file: login-pay-big-process/tests/test_request.py

修改fixtures/fixture_env_vars.py环境变量里面的域名:

  1. mapping = {
  2. "qa": {
  3. "domain": "https://qa.com", # 1
  4. "mysql_engine": mysql_engine("127.0.0.1", # host
  5. "2306", # port
  6. "root", # username
  7. "123456", # password
  8. "qa"), # db_name
  9. },
  10. "release": {
  11. "domain": "https://release.com",
  12. "mysql_engine": mysql_engine("127.0.0.1",
  13. "2306",
  14. "root",
  15. "123456",
  16. "release"),
  17. }
  18. # Add your environment and variables
  19. }

可以只修改domain,数据库可以先不管.

编辑fixtures/fixture_login.py:

  1. from tep.client import request
  2. from tep.fixture import *
  3. def _jwt_headers(token):
  4. return {"Content-Type": "application/json", "authorization": f"Bearer {token}"}
  5. @pytest.fixture(scope="session")
  6. def login(env_vars):
  7. # Code your login
  8. logger.info("Administrator login")
  9. response = request(
  10. "post",
  11. url=env_vars.domain + "/login", # 1
  12. headers={"Content-Type": "application/json"},
  13. json={"username": "dongfanger", "password": "123456"} # 2
  14. )
  15. assert response.status_code < 400
  16. response_token = jmespath.search("token", response.json())
  17. class Clazz:
  18. token = response_token
  19. jwt_headers = _jwt_headers(response_token)
  20. return Clazz

修改url.

修改body.

然后可以跑一下tests/test_login.py看能否登录成功.

在tests目录下新建test_login_pay.py文件:

  1. from tep.client import request
  2. def test(env_vars, login):
  3. # 搜索商品
  4. # 添加购物车
  5. # 下单
  6. # 支付

先把流程架子备注清楚。再一个接口一个接口添加.

搜索商品:

  1. # 搜索商品
  2. response = request(
  3. "get",
  4. url=env_vars.domain + "/searchSku",
  5. headers={"token": login.token},
  6. params={"skuName": "电子书"}
  7. )
  8. sku_id = jmespath.search("skuId", response.json())
  9. sku_price = jmespath.search("price", response.json())
  10. assert response.status_code < 400
  • token直接从login fixture中取值。
  • 从响应中提取sku_id 和sku_price。

添加购物车:

  1. # 添加购物车
  2. sku_num = 3
  3. response = request(
  4. "post",
  5. url=env_vars.domain + "/addCart",
  6. headers={"token": login.token},
  7. json={"skuId": sku_id, "skuNum": sku_num}
  8. )
  9. total_price = jmespath.search("totalPrice", response.json())
  10. assert response.status_code < 400
  • token直接从login fixture中取值。
  • 入参修改为搜索商品提取的变量和自定义的sku_num变量。
  • 提取商品总价total_price。

下单:

  1. # 下单
  2. response = request(
  3. "post",
  4. url=env_vars.domain + "/order",
  5. headers={"token": login.token},
  6. json={"skuId": sku_id, "price": sku_price, "skuNum": sku_num, "totalPrice": total_price}
  7. )
  8. order_id = jmespath.search("orderId", response.json())
  9. assert response.status_code < 400
  • token直接从login fixture中取值。
  • 入参全部使用变量替换。
  • 提取订单id。

支付:

  1. # 支付
  2. response = request(
  3. "post",
  4. url=env_vars.domain + "/pay",
  5. headers={"token": login.token},
  6. json={"orderId": order_id, "payAmount": "6.9"}
  7. )
  8. assert response.status_code < 400
  9. assert response.json()["success"] == "true"

token直接从login fixture中取值.

入参使用下单提取的order_id变量.

添加一条支付成功的断言.

这样一个大流程用例就写好了。完整代码如下:

  1. import jmespath
  2. from tep.client import request
  3. def test(env_vars, login):
  4. # 搜索商品
  5. response = request(
  6. "get",
  7. url=env_vars.domain + "/searchSku",
  8. headers={"token": login.token},
  9. params={"skuName": "电子书"}
  10. )
  11. sku_id = jmespath.search("skuId", response.json())
  12. sku_price = jmespath.search("price", response.json())
  13. assert response.status_code < 400
  14. # 添加购物车
  15. sku_num = 3
  16. response = request(
  17. "post",
  18. url=env_vars.domain + "/addCart",
  19. headers={"token": login.token},
  20. json={"skuId": sku_id, "skuNum": sku_num}
  21. )
  22. total_price = jmespath.search("totalPrice", response.json())
  23. assert response.status_code < 400
  24. # 下单
  25. response = request(
  26. "post",
  27. url=env_vars.domain + "/order",
  28. headers={"token": login.token},
  29. json={"skuId": sku_id, "price": sku_price, "skuNum": sku_num, "totalPrice": total_price}
  30. )
  31. order_id = jmespath.search("orderId", response.json())
  32. assert response.status_code < 400
  33. # 支付
  34. response = request(
  35. "post",
  36. url=env_vars.domain + "/pay",
  37. headers={"token": login.token},
  38. json={"orderId": order_id, "payAmount": "6.9"}
  39. )
  40. assert response.status_code < 400
  41. assert response.json()["success"] == "true"

原文链接:https://mp.weixin.qq.com/s/jwi18_gKHxkKmWgRfBnqUQ 。

最后此篇关于如何使用 Teprunner 测试平台编写从登录到下单的大流程接口自动化用例的文章就讲到这里了,如果你想了解更多关于如何使用 Teprunner 测试平台编写从登录到下单的大流程接口自动化用例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com