feat: add prompt based api testcase generator
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import allure
|
||||
import logging
|
||||
import requests
|
||||
import json
|
||||
import pytest
|
||||
|
||||
try:
|
||||
from joyhub_backend.library.joyhub_interface import JoyhubInterface
|
||||
except Exception:
|
||||
JoyhubInterface = None
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
BASE_URL = "http://test-manager-api.best-envision.com"
|
||||
API_PATH = "admin/video/getVideoLabelList"
|
||||
CASE_NAME = "获取视频标签列表"
|
||||
|
||||
|
||||
def _mask_sensitive(data):
|
||||
if data is None:
|
||||
return data
|
||||
|
||||
sensitive_keys = {"authorization", "token", "access_token", "accessToken", "cookie", "password", "passwd"}
|
||||
|
||||
if isinstance(data, dict):
|
||||
masked = {}
|
||||
for key, value in data.items():
|
||||
if str(key).lower() in sensitive_keys:
|
||||
masked[key] = "***MASKED***"
|
||||
else:
|
||||
masked[key] = _mask_sensitive(value)
|
||||
return masked
|
||||
|
||||
if isinstance(data, list):
|
||||
return [_mask_sensitive(item) for item in data]
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def _attach_json(name, data):
|
||||
allure.attach(
|
||||
json.dumps(_mask_sensitive(data), ensure_ascii=False, indent=2),
|
||||
name,
|
||||
allure.attachment_type.JSON
|
||||
)
|
||||
|
||||
|
||||
def _normalize_response(response):
|
||||
if isinstance(response, requests.Response):
|
||||
try:
|
||||
response_json = response.json()
|
||||
except ValueError:
|
||||
response_json = None
|
||||
return response.status_code, response_json, response.text
|
||||
|
||||
if isinstance(response, dict):
|
||||
return 200, response, json.dumps(response, ensure_ascii=False)
|
||||
|
||||
return None, None, str(response)
|
||||
|
||||
|
||||
@allure.feature("接口")
|
||||
class TestHubopsVideoLabelList(object):
|
||||
|
||||
def setup_method(self):
|
||||
logging.info("-----------------------------Test Start-------------------------------")
|
||||
|
||||
def teardown_method(self):
|
||||
logging.info("-----------------------------Test End-------------------------------")
|
||||
|
||||
@allure.story("获取视频标签列表")
|
||||
@allure.title("测试HubOps获取视频标签列表接口")
|
||||
def test_get_video_label_list(self):
|
||||
if JoyhubInterface is None:
|
||||
pytest.skip("joyhub_backend项目优先使用JoyhubInterface鉴权封装,请确认joyhub_backend.library.joyhub_interface.JoyhubInterface可导入")
|
||||
|
||||
with allure.step("1. 准备请求参数"):
|
||||
method = "POST"
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
body = {
|
||||
"page": 1,
|
||||
"limit": 10,
|
||||
"sort": "id",
|
||||
"label_name": "",
|
||||
"category_id": 0,
|
||||
"video_type": 0,
|
||||
"video_num": 0,
|
||||
"created_at": [],
|
||||
"order": "descending"
|
||||
}
|
||||
|
||||
logging.info("请求方法: %s", method)
|
||||
logging.info("请求路径: %s", API_PATH)
|
||||
_attach_json("请求头", headers)
|
||||
_attach_json("请求体", body)
|
||||
|
||||
with allure.step("2. 调用HubOps获取视频标签列表接口"):
|
||||
client = JoyhubInterface()
|
||||
response = client.request(
|
||||
CASE_NAME,
|
||||
method,
|
||||
API_PATH,
|
||||
body=body,
|
||||
headers=headers,
|
||||
expected_code=0
|
||||
)
|
||||
|
||||
status_code, response_json, response_text = _normalize_response(response)
|
||||
allure.attach(str(status_code), "HTTP状态码", allure.attachment_type.TEXT)
|
||||
|
||||
if response_json is not None:
|
||||
_attach_json("响应JSON", response_json)
|
||||
else:
|
||||
allure.attach(response_text, "响应内容", allure.attachment_type.TEXT)
|
||||
|
||||
with allure.step("3. 校验响应基础字段"):
|
||||
assert status_code == 200, "HTTP状态码应为200,实际为: {}".format(status_code)
|
||||
assert response_json is not None, "响应内容应为JSON且不能为空"
|
||||
assert isinstance(response_json, dict), "响应JSON应为对象类型"
|
||||
|
||||
assert "code" in response_json, "响应JSON应包含code字段"
|
||||
assert response_json.get("code") == 0, "响应code应为0,实际为: {}".format(response_json.get("code"))
|
||||
|
||||
assert "msg" in response_json, "响应JSON应包含msg字段"
|
||||
assert response_json.get("msg") is not None, "响应msg字段不应为None"
|
||||
|
||||
assert "data" in response_json, "响应JSON应包含data字段"
|
||||
assert response_json.get("data") is not None, "响应data字段不应为None"
|
||||
assert isinstance(response_json.get("data"), list), "响应data字段应为数组类型"
|
||||
|
||||
assert "count" in response_json, "响应JSON应包含count字段"
|
||||
assert response_json.get("count") is not None, "响应count字段不应为None"
|
||||
Reference in New Issue
Block a user