# -*- coding:utf-8 -*- """ news分类管理接口测试用例 """ import json import pytest import allure import logging import time import requests from dulizhan.library.BusinessKw.JoyHub.NewsCateManage import NewsCateManage logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @allure.feature("管理后台 - news分类管理模块") class TestNewsCateManage: news_cate_id = None created_names = [] token_set = False @classmethod def setup_class(cls): """在整个测试类开始时登录一次,所有测试用例共享token""" logging.info("=============================================") logging.info("=========== 开始登录,获取Token ============") logging.info("=============================================") cls.test_case = NewsCateManage() 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已设置") else: logging.error(f"登录失败: {login_response}") @allure.story("验证登录接口") @allure.title("测试登录接口") def test_joyhub_login_post(self): """测试登录接口""" assert TestNewsCateManage.token_set is True, "登录失败,未获取到token" logging.info("登录验证通过") @allure.story("验证获得news分类分页") @allure.title("测试获得news分类分页接口") def test_joyhub_news_cate_page_get(self): """测试获得news分类分页接口""" with allure.step("1. 调用分页接口"): resp = self.test_case.kw_joyhub_news_cate_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_cate_get_get(self): """测试获得news分类详情接口""" with allure.step("1. 从分页获取现有news分类ID"): page_resp = self.test_case.kw_joyhub_news_cate_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_cate_id = page_resp["data"]["list"][0].get("id") assert news_cate_id, "news分类ID为空" with allure.step("2. 调用获取详情接口"): resp = self.test_case.kw_joyhub_news_cate_get_get(news_cate_id=news_cate_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_cate_id, "返回的ID与请求的ID不一致" assert "name" in resp["data"], "响应中缺少name字段" assert isinstance(resp["data"]["name"], str), "name字段不是字符串类型" logging.info("获得news分类详情验证通过") @allure.story("验证创建和更新news分类") @allure.title("测试创建和更新news分类接口") def test_joyhub_news_cate_create_and_update(self): """测试创建和更新news分类接口""" with allure.step("1. 创建news分类"): timestamp = int(time.time()) created_name = f"测试news分类_{timestamp}" TestNewsCateManage.created_names.append(created_name) create_params = { "name": created_name, "status": 1, "rank_num": 1, "cover_image": {"url": "https://example.com/test.jpg", "name": "test.jpg"} } allure.attach(json.dumps(create_params, ensure_ascii=False), name="创建请求参数", attachment_type=allure.attachment_type.TEXT) create_resp = self.test_case.kw_joyhub_news_cate_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_cate_id = create_resp["data"] TestNewsCateManage.news_cate_id = news_cate_id logging.info(f"创建news分类成功,ID: {news_cate_id}") with allure.step("2. 调用更新接口"): timestamp = int(time.time()) updated_name = f"测试news分类_{timestamp}_updated" TestNewsCateManage.created_names.append(updated_name) update_params = { "news_cate_id": news_cate_id, "name": updated_name, "status": 2, "rank_num": 2, "cover_image": {"url": "https://example.com/test_updated.jpg", "name": "test_updated.jpg"} } allure.attach(json.dumps(update_params, ensure_ascii=False), name="更新请求参数", attachment_type=allure.attachment_type.TEXT) update_resp = self.test_case.kw_joyhub_news_cate_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_cate_delete_delete(self): """测试删除news分类接口""" if not TestNewsCateManage.news_cate_id: pytest.skip("没有可删除的news分类数据") with allure.step("1. 调用删除接口"): resp = self.test_case.kw_joyhub_news_cate_delete_delete(news_cate_id=TestNewsCateManage.news_cate_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分类验证通过") @classmethod def teardown_class(cls): """在整个测试类结束时清理测试数据""" logging.info("=============================================") logging.info("=========== 清理测试数据 ============") logging.info("=============================================") # 从数据库表jh_news_cate中删除测试数据 for name in cls.created_names: cls.test_case.clean_test_data_from_db(name) # 额外清理所有包含"测试news分类"的记录 cls.test_case.clean_test_data_from_db("测试news分类")