前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用teprunner测试平台编写从登录到下单的大流程接口自动化用例

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

作者头像
dongfanger
发布2022-01-05 13:52:25
5940
发布2022-01-05 13:52:25
举报
文章被收录于专栏:dongfanger

大家好,我是刚哥。

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

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

代码语言:javascript
复制
# 登录
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以上版本:

代码语言:javascript
复制
$ tep -V
0.9.1

然后初始化项目:

代码语言:javascript
复制
$ 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环境变量里面的域名:

代码语言:javascript
复制
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
}
  1. 可以只修改domain,数据库可以先不管。

编辑fixtures/fixture_login.py:

代码语言:javascript
复制
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
  1. 修改url。
  2. 修改body。

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

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

代码语言:javascript
复制
from tep.client import request


def test(env_vars, login):
    # 搜索商品

    # 添加购物车

    # 下单
    
    # 支付

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

搜索商品:

代码语言:javascript
复制
# 搜索商品
    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
  1. token直接从login fixture中取值。
  2. 从响应中提取sku_id 和sku_price。

添加购物车:

代码语言:javascript
复制
# 添加购物车
    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
  1. token直接从login fixture中取值。
  2. 入参修改为搜索商品提取的变量和自定义的sku_num变量。
  3. 提取商品总价total_price。

下单:

代码语言:javascript
复制
# 下单
    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
  1. token直接从login fixture中取值。
  2. 入参全部使用变量替换。
  3. 提取订单id。

支付:

代码语言:javascript
复制
 # 支付
    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"
  1. token直接从login fixture中取值。
  2. 入参使用下单提取的order_id变量。
  3. 添加一条支付成功的断言。

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

代码语言:javascript
复制
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"
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档