feat: 新增JoyHub模块测试用例和功能增强

1. 新增模块测试用例:
   - News分类接口测试 (Joyhub_NewsCate.py)
   - News内容接口测试 (Joyhub_News.py)
   - 产品分类接口测试 (Joyhub_ProductCate.py)
   - 产品属性接口测试 (Joyhub_ProductAttr.py)
   - 产品管理接口测试 (Joyhub_Product.py)
   - FAQ分类接口测试 (Joyhub_FaqCate.py)
   - FAQ内容接口测试 (Joyhub_Faq.py)
   - 博客分类接口测试 (Joyhub_BlogCate.py)
   - 地址国家接口测试 (Joyhub_AddressCountry.py)
   - 下载二维码接口测试 (Joyhub_DownloadQrcode.py)
   - 支付页产品推荐接口测试 (Joyhub_ProductPaymentRecommend.py)

2. 新增业务关键字层:
   - NewsCateManage.py
   - NewsManage.py
   - ProductCateManage.py
   - ProductAttrManage.py
   - ProductManage.py
   - FaqCateManage.py
   - FaqManage.py
   - BlogCateManage.py
   - AddressCountryManage.py
   - DownloadQrcodeManage.py
   - ProductPaymentRecommendManage.py

3. 接口层增强:
   - Dlizhan_interface.py 添加JoyHub相关接口封装

4. 功能增强:
   - run_tests.py 添加自动清除旧测试结果和报告功能
   - Joyhub_Product.py 添加数据库连接获取运费模板ID和品牌ID

5. 修复:
   - 修复产品创建测试用例缺少前置数据问题
This commit is contained in:
2026-05-08 18:09:48 +08:00
parent 32fd51380c
commit 3191ec4f3c
25 changed files with 4800 additions and 2 deletions

View File

@@ -0,0 +1,210 @@
# -*- coding:utf-8 -*-
"""
news管理接口测试用例
"""
import json
import pytest
import allure
import logging
import time
import requests
from dulizhan.library.BusinessKw.JoyHub.NewsManage import NewsManage
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@allure.feature("管理后台 - news管理模块")
class TestNewsManage:
news_id = None
news_cate_id = None
token_set = False
@classmethod
def setup_class(cls):
"""在整个测试类开始时登录一次所有测试用例共享token"""
logging.info("=============================================")
logging.info("=========== 开始登录获取Token ============")
logging.info("=============================================")
cls.test_case = NewsManage()
username = "joytest"
password = "Zhou1599"
cls.test_case._clear_user_fingerprint(username)
url = 'https://joyhub-website-manager-api-test.best-envision.com/admin-api/system/auth/login-dev'
payload = {'username': username, 'password': password}
headers = {'Content-Type': 'application/json', 'tenant-id': '126'}
response = requests.post(url, json=payload, headers=headers, verify=False, timeout=10)
login_response = response.json()
if login_response and login_response.get('code') == 0:
token = login_response.get('data', {}).get('accessToken', '')
cls.test_case.set_joyhub_token(token)
cls.token_set = True
logging.info("登录成功Token已设置")
# 获取news分类ID供后续使用
from dulizhan.library.BusinessKw.JoyHub.NewsCateManage import NewsCateManage
news_cate_manage = NewsCateManage()
news_cate_manage.set_joyhub_token(token)
cate_page_resp = news_cate_manage.kw_joyhub_news_cate_page_get(page_no=1, page_size=10)
if cate_page_resp.get('code') == 0 and cate_page_resp.get('data', {}).get('list'):
cls.news_cate_id = cate_page_resp['data']['list'][0].get('id')
logging.info(f"获取到news分类ID: {cls.news_cate_id}")
else:
logging.error(f"登录失败: {login_response}")
@allure.story("验证登录接口")
@allure.title("测试登录接口")
def test_joyhub_login_post(self):
"""测试登录接口"""
assert TestNewsManage.token_set is True, "登录失败未获取到token"
logging.info("登录验证通过")
@allure.story("验证获得news管理分页")
@allure.title("测试获得news管理分页接口")
def test_joyhub_news_page_get(self):
"""测试获得news管理分页接口"""
with allure.step("1. 调用分页接口"):
resp = self.test_case.kw_joyhub_news_page_get(page_no=1, page_size=10)
allure.attach(json.dumps(resp, ensure_ascii=False, indent=2), name="响应数据", attachment_type=allure.attachment_type.JSON)
with allure.step("2. 验证响应"):
assert resp is not None, "响应为空"
assert "code" in resp, "响应中缺少code字段"
assert resp["code"] == 0, f"请求失败code={resp.get('code')}, msg={resp.get('msg')}"
assert "data" in resp, "响应中缺少data字段"
assert "list" in resp["data"], "响应中缺少list字段"
assert isinstance(resp["data"]["list"], list), "list字段不是列表类型"
logging.info("获得news管理分页验证通过")
@allure.story("验证获得news管理详情")
@allure.title("测试获得news管理详情接口")
def test_joyhub_news_get_get(self):
"""测试获得news管理详情接口"""
with allure.step("1. 从分页获取现有news管理ID"):
page_resp = self.test_case.kw_joyhub_news_page_get(page_no=1, page_size=10)
assert page_resp.get("code") == 0, f"获取分页失败code={page_resp.get('code')}, msg={page_resp.get('msg')}"
assert page_resp.get("data", {}).get("list"), "没有找到可用的news管理数据"
news_id = page_resp["data"]["list"][0].get("id")
assert news_id, "news管理ID为空"
with allure.step("2. 调用获取详情接口"):
resp = self.test_case.kw_joyhub_news_get_get(news_id=news_id)
allure.attach(json.dumps(resp, ensure_ascii=False, indent=2), name="响应数据", attachment_type=allure.attachment_type.JSON)
with allure.step("3. 验证响应"):
assert resp is not None, "响应为空"
assert "code" in resp, "响应中缺少code字段"
assert resp["code"] == 0, f"请求失败code={resp.get('code')}, msg={resp.get('msg')}"
assert "data" in resp, "响应中缺少data字段"
assert "id" in resp["data"], "响应中缺少id字段"
assert resp["data"]["id"] == news_id, "返回的ID与请求的ID不一致"
assert "title" in resp["data"], "响应中缺少title字段"
assert isinstance(resp["data"]["title"], str), "title字段不是字符串类型"
logging.info("获得news管理详情验证通过")
@allure.story("验证创建和更新news管理")
@allure.title("测试创建和更新news管理接口")
def test_joyhub_news_create_and_update(self):
"""测试创建和更新news管理接口"""
with allure.step("1. 创建news管理"):
timestamp = int(time.time())
create_params = {
"title": f"测试news标题_{timestamp}",
"cover_image": {"url": "https://example.com/test.jpg", "name": "test.jpg"},
"content": f"<p>测试内容_{timestamp}</p>",
"status": 1,
"rank_num": 1
}
# 如果有可用的news分类ID添加关联
if TestNewsManage.news_cate_id:
create_params["cate_ids"] = [TestNewsManage.news_cate_id]
allure.attach(json.dumps(create_params, ensure_ascii=False), name="创建请求参数", attachment_type=allure.attachment_type.TEXT)
create_resp = self.test_case.kw_joyhub_news_create_post(**create_params)
allure.attach(json.dumps(create_resp, ensure_ascii=False, indent=2), name="创建响应数据", attachment_type=allure.attachment_type.JSON)
assert create_resp is not None, "响应为空"
assert "code" in create_resp, "响应中缺少code字段"
assert create_resp["code"] == 0, f"创建失败code={create_resp.get('code')}, msg={create_resp.get('msg')}"
assert "data" in create_resp, "响应中缺少data字段"
assert isinstance(create_resp["data"], int), "data字段不是整数类型"
news_id = create_resp["data"]
TestNewsManage.news_id = news_id
logging.info(f"创建news管理成功ID: {news_id}")
with allure.step("2. 调用更新接口"):
timestamp = int(time.time())
update_params = {
"news_id": news_id,
"title": f"测试news标题_{timestamp}_updated",
"cover_image": {"url": "https://example.com/test_updated.jpg", "name": "test_updated.jpg"},
"content": f"<p>测试内容_{timestamp}_updated</p>",
"status": 2,
"rank_num": 2
}
# 如果有可用的news分类ID添加关联
if TestNewsManage.news_cate_id:
update_params["cate_ids"] = [TestNewsManage.news_cate_id]
allure.attach(json.dumps(update_params, ensure_ascii=False), name="更新请求参数", attachment_type=allure.attachment_type.TEXT)
update_resp = self.test_case.kw_joyhub_news_update_put(**update_params)
allure.attach(json.dumps(update_resp, ensure_ascii=False, indent=2), name="更新响应数据", attachment_type=allure.attachment_type.JSON)
with allure.step("3. 验证更新响应"):
assert update_resp is not None, "响应为空"
assert "code" in update_resp, "响应中缺少code字段"
assert update_resp["code"] == 0, f"更新失败code={update_resp.get('code')}, msg={update_resp.get('msg')}"
assert "data" in update_resp, "响应中缺少data字段"
assert update_resp["data"] is True, "更新news管理失败"
logging.info("创建并更新news管理验证通过")
@allure.story("验证删除news管理")
@allure.title("测试删除news管理接口")
def test_joyhub_news_delete_delete(self):
"""测试删除news管理接口"""
if not TestNewsManage.news_id:
pytest.skip("没有可删除的news管理数据")
with allure.step("1. 调用删除接口"):
resp = self.test_case.kw_joyhub_news_delete_delete(news_id=TestNewsManage.news_id)
allure.attach(json.dumps(resp, ensure_ascii=False, indent=2), name="响应数据", attachment_type=allure.attachment_type.JSON)
with allure.step("2. 验证响应"):
assert resp is not None, "响应为空"
assert "code" in resp, "响应中缺少code字段"
assert resp["code"] == 0, f"删除失败code={resp.get('code')}, msg={resp.get('msg')}"
assert "data" in resp, "响应中缺少data字段"
assert resp["data"] is True, "删除news管理失败"
logging.info("删除news管理验证通过")
@allure.story("验证获得news分类关联列表")
@allure.title("测试获得news分类关联列表接口")
def test_joyhub_news_cate_relation_list_get(self):
"""测试获得news分类关联列表接口"""
with allure.step("1. 从分页获取现有news管理ID"):
page_resp = self.test_case.kw_joyhub_news_page_get(page_no=1, page_size=10)
assert page_resp.get("code") == 0, f"获取分页失败code={page_resp.get('code')}, msg={page_resp.get('msg')}"
assert page_resp.get("data", {}).get("list"), "没有找到可用的news管理数据"
news_id = page_resp["data"]["list"][0].get("id")
assert news_id, "news管理ID为空"
with allure.step("2. 调用获取分类关联列表接口"):
resp = self.test_case.kw_joyhub_news_cate_relation_list_get(news_id=news_id)
allure.attach(json.dumps(resp, ensure_ascii=False, indent=2), name="响应数据", attachment_type=allure.attachment_type.JSON)
with allure.step("3. 验证响应"):
assert resp is not None, "响应为空"
assert "code" in resp, "响应中缺少code字段"
assert resp["code"] == 0, f"请求失败code={resp.get('code')}, msg={resp.get('msg')}"
assert "data" in resp, "响应中缺少data字段"
assert isinstance(resp["data"], list), "data字段不是列表类型"
logging.info("获得news分类关联列表验证通过")