# -*- coding:utf-8 -*- """ JoyHub C端点赞记录接口测试用例 """ import json import time import allure import logging import pytest from dulizhan.library.BusinessKw.JoyHubC.LikeInfoManage import JoyHubCLikeInfoManage from dulizhan.library.BusinessKw.JoyHubC.NewsManage import JoyHubCNewsManage logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @pytest.mark.skip(reason="点赞记录接口依赖C端登录态,当前鉴权方式未打通,暂时跳过") @allure.feature("C端 - 点赞记录模块") class TestJoyHubCLikeInfo: like_id = None data_id = None device_id = None like_type = 1 @classmethod def setup_class(cls): """在整个测试类开始时初始化C端点赞记录业务关键字""" logging.info("=============================================") logging.info("=========== 开始JoyHub C端点赞记录接口测试 =========") logging.info("=============================================") cls.test_case = JoyHubCLikeInfoManage() cls.news_case = JoyHubCNewsManage() cls.device_id = f"auto_device_{int(time.time())}" news_resp = cls.news_case.kw_joyhub_c_news_page_get(pageNo=1, pageSize=10) if news_resp and news_resp.get("code") == 0 and news_resp.get("data", {}).get("list"): cls.data_id = news_resp["data"]["list"][0].get("id") @allure.story("验证C端创建点赞记录") @allure.title("测试C端创建点赞记录接口") def test_joyhub_c_like_info_create_post(self): """测试C端创建点赞记录接口""" if not TestJoyHubCLikeInfo.data_id: pytest.skip("没有可用于点赞的news数据") with allure.step("1. 准备请求参数"): params = { "id": int(time.time()), "type": TestJoyHubCLikeInfo.like_type, "dataId": TestJoyHubCLikeInfo.data_id, "deviceId": TestJoyHubCLikeInfo.device_id } allure.attach(json.dumps(params, ensure_ascii=False), name="请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用创建点赞记录接口"): resp = self.test_case.kw_joyhub_c_like_info_create_post(**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字段" if resp["code"] == 300034: pytest.skip("创建点赞记录接口依赖C端登录态,当前登录鉴权方式未打通") 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字段为空" TestJoyHubCLikeInfo.like_id = resp["data"] logging.info("C端创建点赞记录接口验证通过") @allure.story("验证C端获得点赞记录分页") @allure.title("测试C端获得点赞记录分页接口") def test_joyhub_c_like_info_page_get(self): """测试C端获得点赞记录分页接口""" with allure.step("1. 准备请求参数"): params = { "pageNo": 1, "pageSize": 10 } if TestJoyHubCLikeInfo.data_id: params["type"] = TestJoyHubCLikeInfo.like_type params["dataId"] = TestJoyHubCLikeInfo.data_id params["deviceId"] = TestJoyHubCLikeInfo.device_id allure.attach(json.dumps(params, ensure_ascii=False), name="请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用获得点赞记录分页接口"): resp = self.test_case.kw_joyhub_c_like_info_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 not TestJoyHubCLikeInfo.like_id and resp["data"]["list"]: TestJoyHubCLikeInfo.like_id = resp["data"]["list"][0].get("id") logging.info("C端获得点赞记录分页接口验证通过") @allure.story("验证C端获得点赞记录") @allure.title("测试C端获得点赞记录接口") def test_joyhub_c_like_info_get_get(self): """测试C端获得点赞记录接口""" if not TestJoyHubCLikeInfo.like_id: pytest.skip("没有可用于查询详情的点赞记录") with allure.step("1. 准备请求参数"): params = { "id": TestJoyHubCLikeInfo.like_id } allure.attach(json.dumps(params, ensure_ascii=False), name="请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用获得点赞记录接口"): resp = self.test_case.kw_joyhub_c_like_info_get_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") == TestJoyHubCLikeInfo.like_id, "返回的点赞记录ID与请求ID不一致" logging.info("C端获得点赞记录接口验证通过") @allure.story("验证C端取消点赞") @allure.title("测试C端取消点赞接口") def test_joyhub_c_like_info_delete_post(self): """测试C端取消点赞接口""" if not TestJoyHubCLikeInfo.like_id: pytest.skip("没有可用于取消点赞的点赞记录") with allure.step("1. 准备请求参数"): params = { "id": TestJoyHubCLikeInfo.like_id, "type": TestJoyHubCLikeInfo.like_type, "dataId": TestJoyHubCLikeInfo.data_id, "deviceId": TestJoyHubCLikeInfo.device_id } allure.attach(json.dumps(params, ensure_ascii=False), name="请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用取消点赞接口"): resp = self.test_case.kw_joyhub_c_like_info_delete_post(**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 isinstance(resp["data"], bool), "data字段不是布尔类型" logging.info("C端取消点赞接口验证通过")