# -*- coding:utf-8 -*- """ JoyHub C端news管理接口测试用例 """ import json import allure import logging import pytest from dulizhan.library.BusinessKw.JoyHubC.NewsManage import JoyHubCNewsManage logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @allure.feature("C端 - news管理模块") class TestJoyHubCNews: news_id = None news_cate_id = None @classmethod def setup_class(cls): """在整个测试类开始时初始化C端news管理业务关键字""" logging.info("=============================================") logging.info("=========== 开始JoyHub C端news管理接口测试 =========") logging.info("=============================================") cls.test_case = JoyHubCNewsManage() @allure.story("验证C端获得news管理分页") @allure.title("测试C端获得news管理分页接口") def test_joyhub_c_news_page_get(self): """测试C端获得news管理分页接口""" with allure.step("1. 准备请求参数"): params = { "pageNo": 1, "pageSize": 10 } allure.attach(json.dumps(params, ensure_ascii=False), name="请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用获得news管理分页接口"): resp = self.test_case.kw_joyhub_c_news_page_get(**params) 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 "list" in resp["data"], "响应中缺少list字段" assert isinstance(resp["data"]["list"], list), "list字段不是列表类型" if "total" in resp["data"]: assert isinstance(resp["data"]["total"], int), "total字段不是整数类型" if resp["data"]["list"]: first_news = resp["data"]["list"][0] TestJoyHubCNews.news_id = first_news.get("id") TestJoyHubCNews.news_cate_id = first_news.get("cateId") logging.info("C端获得news管理分页接口验证通过") @allure.story("验证C端获得news详情") @allure.title("测试C端获得news详情接口") def test_joyhub_c_news_get_detail_get(self): """测试C端获得news详情接口""" if not TestJoyHubCNews.news_id: pytest.skip("没有可用于查询详情的news数据") with allure.step("1. 准备请求参数"): params = { "id": TestJoyHubCNews.news_id } allure.attach(json.dumps(params, ensure_ascii=False), name="请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用获得news详情接口"): resp = self.test_case.kw_joyhub_c_news_get_detail_get(**params) 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 resp["data"] is not None, "data字段为空" assert resp["data"].get("id") == TestJoyHubCNews.news_id, "返回的news ID与请求ID不一致" logging.info("C端获得news详情接口验证通过") @allure.story("验证C端获得news下一条") @allure.title("测试C端获得news下一条接口") def test_joyhub_c_news_get_next_get(self): """测试C端获得news下一条接口""" if not TestJoyHubCNews.news_id: pytest.skip("没有可用于查询下一条的news数据") with allure.step("1. 准备请求参数"): params = { "id": TestJoyHubCNews.news_id } if TestJoyHubCNews.news_cate_id: params["cateId"] = TestJoyHubCNews.news_cate_id allure.attach(json.dumps(params, ensure_ascii=False), name="请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用获得news下一条接口"): resp = self.test_case.kw_joyhub_c_news_get_next_get(**params) 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字段" if resp["data"] is not None: assert isinstance(resp["data"], dict), "data字段不是字典类型" logging.info("C端获得news下一条接口验证通过")