接口自动化之添加商品(pytest)

1、项目结构

2、拿取登录的鉴权码assess_token.py

python 复制代码
import requests

def login_token():
    BASE_url="http://192.168.10.39:6255/wx/auth/login"

    json={"username":"user123","password":"user123"}

    res=requests.post(url=BASE_url,json=json)
    #需要结合实际的json格式,获取的格式为{data:{token:""}}
    token=res.json()["data"]['token']
    headers={'x-litemall-token':token}
    return headers

3、读取文件根目录的config.py

python 复制代码
import os

BASE_DIR = os.path.dirname(__file__)

4、读取需要测试的json数据read_data.py

这是一个json文件(additems.json):

python 复制代码
[
  {
    "goodsId":1135002,
    "number":1,
    "productId":204,
    "expect" : "成功"
  },
  {
    "goodsId":" ",
    "number":1,
    "productId":204,
    "expect" : "参数不对"
  },
  {
    "goodsId":1135002,
    "number":" ",
    "productId":204,
    "expect" : "系统内部错误"
  },
  {
    "goodsId":1135002,
    "number":1,
    "productId":" ",
    "expect" : "参数不对"
  }
]

这是读取数据的read_data.py

python 复制代码
from config import BASE_DIR

import json

def goods_data():
    with open(BASE_DIR + '\\data\\additems.json',encoding='utf-8') as f:
        data1 = json.load(f)

    new_list = [] 

    for d in data1:
        new_list.append(tuple(d.values()))

    return new_list

5、发送添加商品的请求以及判定执行结果add_tools.py

python 复制代码
import requests

from assess_token import login_token

def addgds(goodsId,number,productId):

    BASE_URL = "http://192.168.10.39:6255"


    url1 = f"{BASE_URL}/wx/cart/add"
    payload = {
    "goodsId":goodsId,
    "number":number,
    "productId":productId}

    resp = requests.post(url=url1, json=payload, timeout=10,headers=login_token())

    res_json = resp.json()

    # litemall 规范:errno=0 代表成功
    if res_json.get("errno") == 0:
        return res_json.get("errmsg")   # 成功标记
    else:
        return res_json.get("errmsg")   # 失败标记

6、执行添加商品的接口测试additem.py

python 复制代码
import pytest

from reas_data import goods_data

from tools.add_tools import addgds


class TestAdd:
    # 参数化:批量读取json内多组账号密码+预期结果
    @pytest.mark.parametrize('goodsId,number,productId,expect', goods_data())
    def test_add_pass(self, goodsId,number,productId,expect):

        actual_result = addgds(goodsId,number,productId)

        print(f"预期结果:{expect},实际结果:{actual_result}")
        # 断言:实际值 = 预期值,不相等则用例失败py

        assert expect in actual_result

7、配置pytest.ini文件

python 复制代码
[pytest]
addopts = --capture=tee-sys --html=report/additem_report.html  --self-contained-html
testpaths = ./litemall
python_files = additem.py
python_classes = Test*
python_functions = test*

8、打开终端执行pytest生成测试报告

相关推荐
weixin_416667962 小时前
银河麒麟V10看门狗试验-1
网络·chrome·python
古城小栈2 小时前
Pydantic 从入门到实践全讲解
python
happylifetree2 小时前
Python18:核心语法-数据存储与运算-运算符-算术运算符
python
打工仔折腾 AI2 小时前
从Attention到BERT:双向预训练语言模型到底解决了什么问题
人工智能·后端·python·深度学习·语言模型·bert
迅猛龙办公室3 小时前
实现第一个python程序(HelloWorld)
开发语言·python
拉格朗日(Lagrange)3 小时前
【第2 章】WorkBuddy 从入门到高手
开发语言·python
wtblszn4 小时前
空压机在线监测物联网方案
大数据·运维·物联网·自动化·能源
老歌老听老掉牙4 小时前
斜抛运动问题分析:给定最大高度与墙面位置的轨迹与时间求解
python·斜抛运动
言乐64 小时前
Python自动去除水印
开发语言·python·django·virtualenv·pygame
czq_26867194874 小时前
Python打卡第31天
开发语言·python