# -*- coding: utf-8 -*-
import json
import pytest
import requests
import jsonpath
from log_util import logger
class TestLitemall:
def setup_class(self):
# 管理端接口
url = "https://litemall.hogwarts.ceshiren.com/admin/auth/login"
user_data = {
"username": "hogwarts",
"password": "test12345",
"code": ""
}
r = requests.post(url, json=user_data, verify=False)
# 实例化token变量 注意:jsonpath.jsonpath返回的是列表
self.token = jsonpath.jsonpath(r.json(), "$.data.token")[0]
# 客户端接口
url_client = "https://litemall.hogwarts.ceshiren.com/wx/auth/login"
client_data = {
"username": "user123",
"password": "user123"
}
r = requests.post(url_client, json=client_data, verify=False)
self.token_client = jsonpath.jsonpath(r.json(), "$.data.token")[0]
def teardown_method(self):
# 数据清理
url = "https://litemall.hogwarts.ceshiren.com/admin/goods/delete"
r = requests.post(url, json={"id": self.goods_id}, verify=False, headers={"x-litemall-admin-token": self.token})
logger.info(f"删除商品接口响应信息为:{json.dumps(r.json(), indent=2, ensure_ascii=False)}")
# ============问题2:goods_name 不能重复,所以需要添加参数化
@pytest.mark.parametrize("goods_name", ["毛巾9020", "毛巾9021", "毛巾9022"])
def test_add_goods(self, goods_name):
# ======================上架商品接口==========================
url = "https://litemall.hogwarts.ceshiren.com/admin/goods/create"
goods_data = {
"goods": {"picUrl": "", "gallery": [], "isHot": False, "isNew": True, "isOnSale": True, "goodsSn": "9012",
"name": goods_name}, "specifications": [{"specification": "规格", "value": "标准", "picUrl": ""}],
"products": [{"id": 0, "specifications": ["标准"], "price": "20", "number": "20", "url": ""}],
"attributes": []}
headers = {"x-litemall-admin-token": self.token}
r = requests.post(url, json=goods_data, verify=False, headers=headers)
logger.info(f"上架商品接口响应信息为:{json.dumps(r.json(), indent=2, ensure_ascii=False)}")
# =====================商品列表接口(可以提取商品ID)======================
goods_list_url = "https://litemall.hogwarts.ceshiren.com/admin/goods/list"
goods_data = {
"name": goods_name,
"order": "desc",
"sort": "add_time"
}
r = requests.get(goods_list_url, params=goods_data, verify=False,
headers={"x-litemall-admin-token": self.token})
self.goods_id = r.json()["data"]["list"][0]["id"]
logger.info(f"商品列表接口响应信息为:{json.dumps(r.json(), indent=2, ensure_ascii=False)}")
# ================商品详情接口(可以提取商品库存ID)==================
goods_detail_url = "https://litemall.hogwarts.ceshiren.com/admin/goods/detail"
goods_detail_data = {
"id": self.goods_id
}
r = requests.get(goods_detail_url, params=goods_detail_data, verify=False,
headers={"x-litemall-admin-token": self.token})
products_id = r.json()["data"]["products"][0]["id"]
logger.info(f"商品详情接口口响应信息为:{json.dumps(r.json(), indent=2, ensure_ascii=False)}")
# =======================添加购物车接口======================================
url = "https://litemall.hogwarts.ceshiren.com/wx/cart/add"
# 问题:goodsId 和 productId 是写死的,变量的传递没有完成
# 解决方案:goodsId 和 productId 从其他的接口获取,并传递给添加购物车接口
cart_data = {
"goodsId": self.goods_id,
"number": 1,
"productId": products_id
}
r = requests.post(url, json=cart_data, verify=False, headers={"x-litemall-token": self.token_client})
logger.info(f"添加购物车接口响应信息为:{json.dumps(r.json(), indent=2, ensure_ascii=False)}")
# ======问题1:缺少断言
assert r.json()["errmsg"] == "成功"