# -*- coding:utf-8 -*- """ JoyHub C端二维码访问统计接口测试用例 """ import os import json import allure import logging import pytest from dulizhan.library.BusinessKw.JoyHubC.DownloadQrcodeManage import JoyHubCDownloadQrcodeManage logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @pytest.mark.skip(reason="当前环境未配置可用二维码测试数据,暂时跳过") @allure.feature("C端 - 二维码访问统计模块") class TestJoyHubCDownloadQrcode: qrcode_code = os.environ.get("JOYHUB_C_QRCODE_CODE", "ABC123") qrcode_exists = False @classmethod def setup_class(cls): """在整个测试类开始时初始化C端二维码访问统计业务关键字""" logging.info("=============================================") logging.info("=========== 开始JoyHub C端二维码访问统计接口测试 =========") logging.info("=============================================") cls.test_case = JoyHubCDownloadQrcodeManage() @allure.story("验证C端获取二维码信息") @allure.title("测试C端获取二维码信息接口") def test_joyhub_c_download_qrcode_get_get(self): """测试C端获取二维码信息接口""" with allure.step("1. 准备请求参数"): params = { "code": TestJoyHubCDownloadQrcode.qrcode_code } 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_download_qrcode_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字段" if resp["code"] != 0: pytest.skip(f"当前环境未配置可用二维码code,code={resp.get('code')}, msg={resp.get('msg')}") assert "data" in resp, "响应中缺少data字段" if resp["data"] is None: pytest.skip("当前环境未配置可用二维码code,获取二维码信息返回data为空") TestJoyHubCDownloadQrcode.qrcode_exists = True logging.info("C端获取二维码信息接口验证通过") @allure.story("验证C端增加二维码访问/点击次数") @allure.title("测试C端增加二维码访问/点击次数接口") def test_joyhub_c_download_qrcode_increment_post(self): """测试C端增加二维码访问/点击次数接口""" if not TestJoyHubCDownloadQrcode.qrcode_exists: pytest.skip("没有可用于增加访问/点击次数的二维码数据") with allure.step("1. 准备请求参数"): params = { "code": TestJoyHubCDownloadQrcode.qrcode_code, "visitCount": True, "clickCount": True } 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_download_qrcode_increment_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端增加二维码访问/点击次数接口验证通过")