大家好,我是刚哥。
对于登录→搜索商品→添加购物车→下单→支付这样的大流程,使用teprunner测试平台该如何编写接口自动化用例呢?
假设这几个节点的接口和参数如下:
- # 登录
- url:/login
- method: post
- body: {"username": "dongfanger", "password": "123456"}
- response: {"token": "de2e3ffu29"}
- # 搜索商品
- url:/searchSku
- method: get
- headers: {"token": "de2e3ffu29"}
- body: {"skuName": "电子书"}
- response: {"skuId": "222", "price": "2.3"}
- # 添加购物车
- url:/addCart
- method: post
- headers: {"token": "de2e3ffu29"}
- body: {"skuId": "222", "skuNum": "3"}
- response: {"skuId": "222", "price": "2.3", "skuNum": "3", "totalPrice": "6.9"}
- # 下单
- url:/order
- method: post
- headers: {"token": "de2e3ffu29"}
- body: {"skuId": "222", "price": "2.3", "skuNum": "3", "totalPrice": "6.9"}
- response: {"orderId": "333"}
- # 支付
- url:/pay
- method: post
- headers: {"token": "de2e3ffu29"}
- body: {"orderId": "333", "payAmount": "6.9"}
- response: {"success": "true"}
我不会直接在teprunner测试平台的界面上直接写新用例的代码,而是先在PyCharm中把代码写好调通,再手动复制粘贴或者经过Git同步到平台上。本文就来介绍下怎么在PyCharm中借助tep编写这个大流程的接口自动化用例。
先使用命令检查tep已经是0.9.1以上版本:
- $ tep -V
- 0.9.1
然后初始化项目:
- $ tep startproject login-pay-big-process
- 2021-12-27 12:41:26.721 | INFO | tep.scaffold:create_scaffold:53 - Create new project: login-pay-big-process
- Project root dir: /Users/dongfanger/PycharmProjects/login-pay-big-process
- Created folder: login-pay-big-process
- Created folder: login-pay-big-process/fixtures
- Created folder: login-pay-big-process/tests
- Created folder: login-pay-big-process/files
- Created folder: login-pay-big-process/reports
- Created file: login-pay-big-process/.gitignore
- Created file: login-pay-big-process/conf.yaml
- Created file: login-pay-big-process/conftest.py
- Created file: login-pay-big-process/pytest.ini
- Created file: login-pay-big-process/requirements.txt
- Created file: login-pay-big-process/fixtures/__init__.py
- Created file: login-pay-big-process/fixtures/fixture_admin.py
- Created file: login-pay-big-process/fixtures/fixture_env_vars.py
- Created file: login-pay-big-process/fixtures/fixture_login.py
- Created file: login-pay-big-process/fixtures/fixture_your_name.py
- Created file: login-pay-big-process/tests/__init__.py
- Created file: login-pay-big-process/tests/test_login.py
- Created file: login-pay-big-process/tests/test_post.py
- Created file: login-pay-big-process/tests/test_mysql.py
- Created file: login-pay-big-process/tests/test_request.py
修改fixtures/fixture_env_vars.py环境变量里面的域名:
- mapping = {
- "qa": {
- "domain": "https://qa.com", # 1
- "mysql_engine": mysql_engine("127.0.0.1", # host
- "2306", # port
- "root", # username
- "123456", # password
- "qa"), # db_name
- },
- "release": {
- "domain": "https://release.com",
- "mysql_engine": mysql_engine("127.0.0.1",
- "2306",
- "root",
- "123456",
- "release"),
- }
- # Add your environment and variables
- }
可以只修改domain,数据库可以先不管。
编辑fixtures/fixture_login.py:
- from tep.client import request
- from tep.fixture import *
- def _jwt_headers(token):
- return {"Content-Type": "application/json", "authorization": f"Bearer {token}"}
- @pytest.fixture(scope="session")
- def login(env_vars):
- # Code your login
- logger.info("Administrator login")
- response = request(
- "post",
- url=env_vars.domain + "/login", # 1
- headers={"Content-Type": "application/json"},
- json={"username": "dongfanger", "password": "123456"} # 2
- )
- assert response.status_code < 400
- response_token = jmespath.search("token", response.json())
- class Clazz:
- token = response_token
- jwt_headers = _jwt_headers(response_token)
- return Clazz
修改url。
修改body。
然后可以跑一下tests/test_login.py看能否登录成功。
在tests目录下新建test_login_pay.py文件:
- from tep.client import request
- def test(env_vars, login):
- # 搜索商品
- # 添加购物车
- # 下单
- # 支付
先把流程架子备注清楚。再一个接口一个接口添加。
搜索商品:
- # 搜索商品
- response = request(
- "get",
- url=env_vars.domain + "/searchSku",
- headers={"token": login.token},
- params={"skuName": "电子书"}
- )
- sku_id = jmespath.search("skuId", response.json())
- sku_price = jmespath.search("price", response.json())
- assert response.status_code < 400
- token直接从login fixture中取值。
- 从响应中提取sku_id 和sku_price。
添加购物车:
- # 添加购物车
- sku_num = 3
- response = request(
- "post",
- url=env_vars.domain + "/addCart",
- headers={"token": login.token},
- json={"skuId": sku_id, "skuNum": sku_num}
- )
- total_price = jmespath.search("totalPrice", response.json())
- assert response.status_code < 400
- token直接从login fixture中取值。
- 入参修改为搜索商品提取的变量和自定义的sku_num变量。
- 提取商品总价total_price。
下单:
- # 下单
- response = request(
- "post",
- url=env_vars.domain + "/order",
- headers={"token": login.token},
- json={"skuId": sku_id, "price": sku_price, "skuNum": sku_num, "totalPrice": total_price}
- )
- order_id = jmespath.search("orderId", response.json())
- assert response.status_code < 400
- token直接从login fixture中取值。
- 入参全部使用变量替换。
- 提取订单id。
支付:
- # 支付
- response = request(
- "post",
- url=env_vars.domain + "/pay",
- headers={"token": login.token},
- json={"orderId": order_id, "payAmount": "6.9"}
- )
- assert response.status_code < 400
- assert response.json()["success"] == "true"
token直接从login fixture中取值。
入参使用下单提取的order_id变量。
添加一条支付成功的断言。
这样一个大流程用例就写好了。完整代码如下:
- import jmespath
- from tep.client import request
- def test(env_vars, login):
- # 搜索商品
- response = request(
- "get",
- url=env_vars.domain + "/searchSku",
- headers={"token": login.token},
- params={"skuName": "电子书"}
- )
- sku_id = jmespath.search("skuId", response.json())
- sku_price = jmespath.search("price", response.json())
- assert response.status_code < 400
- # 添加购物车
- sku_num = 3
- response = request(
- "post",
- url=env_vars.domain + "/addCart",
- headers={"token": login.token},
- json={"skuId": sku_id, "skuNum": sku_num}
- )
- total_price = jmespath.search("totalPrice", response.json())
- assert response.status_code < 400
- # 下单
- response = request(
- "post",
- url=env_vars.domain + "/order",
- headers={"token": login.token},
- json={"skuId": sku_id, "price": sku_price, "skuNum": sku_num, "totalPrice": total_price}
- )
- order_id = jmespath.search("orderId", response.json())
- assert response.status_code < 400
- # 支付
- response = request(
- "post",
- url=env_vars.domain + "/pay",
- headers={"token": login.token},
- json={"orderId": order_id, "payAmount": "6.9"}
- )
- assert response.status_code < 400
- assert response.json()["success"] == "true"