优化方法模块

This commit is contained in:
谈林海 2023-04-25 17:31:13 +08:00
parent 9aaf7eb04b
commit 1c1c808b8f
22 changed files with 122 additions and 64 deletions

View File

@ -79,7 +79,7 @@ def collection():
def __init__(self):
self.requests = RestClient()
self.headers = RestClient.headers()
self.cache = HandleYaml(root / 'test_data/cache.yml')
self.cache = HandleYaml(root / 'test_data/cache.yaml')
self.mysql = MysqlDB() if config.mysql_db.switch else logger.warning('当前数据库配置: 关闭')
yield Core()

View File

@ -9,3 +9,95 @@
响应耗时(ms): 1240.0
接口响应码: 400
2023-04-25 17:15:42.582 | WARNING | utils.requests_control:cookie_token:33 - 未获取到登录认证信息,请检查: <urllib3.connection.HTTPSConnection object at 0x104059dc0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
2023-04-25 17:15:42.621 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:15:42.769 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/phegda/advrest
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'terminal': 'PC', 'apiVersion': '1.0.1', 'site': {'Siteid': 1015, 'siteName': 'waibu', 'qid': '04478', 'pageType': 'null', 'pageUrl': 'https://www.tianqi.com/shanghai/', 'newsType': 'null', 'keywords': '上海天气,上海天气预报,上海天气预报查询,上海今日天气,上海周末天气,上海一周天气预报,上海天气预报一周,天气预报查询一周,上海天气预报10天,上海天气预报查询15天,上海未来一周的天气预报,上海天气情况,上海40日天气预报,天气预报40天,上海30日天气预报,天气预报30天'}, 'device': {'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'geo': {'country': '', 'city': ''}, 'dnt': 0, 'deviceType': 'PC', 'os': 'MACOS', 'osVersion': 'X', 'height': 1080, 'width': 1920}, 'user': {'userId': '16805156868205498'}, 'imp': [{'slotId': 102298, 'pageNum': '5', 'styleId': [1004]}], 'requestId': '16805156922345810'}
响应数据: {'msg': 'bad request ...', 'success': False}
响应耗时(ms): 80.0
接口响应码: 400
2023-04-25 17:16:53.820 | WARNING | utils.requests_control:cookie_token:33 - 未获取到登录认证信息,请检查: <urllib3.connection.HTTPSConnection object at 0x106295cd0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
2023-04-25 17:16:53.836 | ERROR | utils.read_yaml_control:read_yaml:22 - 文件路径不存在,请检查
2023-04-25 17:17:43.671 | WARNING | utils.requests_control:cookie_token:33 - 未获取到登录认证信息,请检查: <urllib3.connection.HTTPSConnection object at 0x102f2ec70>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
2023-04-25 17:17:43.706 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:17:43.842 | INFO | utils.decorator_control:wrapper:24 - 请求返回结果为text类型
2023-04-25 17:17:43.842 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/login
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'account': '{{phone_number()}}', 'password': 'xxxxxxxxxx'}
响应数据: 404
响应耗时(ms): 84.0
接口响应码: 404
2023-04-25 17:17:43.890 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:17:43.970 | INFO | utils.decorator_control:wrapper:24 - 请求返回结果为text类型
2023-04-25 17:17:43.970 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/login
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'account': '18900001000', 'password': 'xxxxxx'}
响应数据: 404
响应耗时(ms): 78.0
接口响应码: 404
2023-04-25 17:28:23.447 | WARNING | utils.requests_control:cookie_token:33 - 未获取到登录认证信息,请检查: <urllib3.connection.HTTPSConnection object at 0x102d1ed00>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
2023-04-25 17:28:23.488 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:28:23.627 | INFO | utils.decorator_control:wrapper:24 - 请求返回结果为text类型
2023-04-25 17:28:23.627 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/imports
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'key': '14584996399', 'key2': None}
响应数据: 404
响应耗时(ms): 89.0
接口响应码: 404
2023-04-25 17:28:23.670 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:28:23.746 | INFO | utils.decorator_control:wrapper:24 - 请求返回结果为text类型
2023-04-25 17:28:23.746 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/imports
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'key': ['9819'], 'key2': [None]}
响应数据: 404
响应耗时(ms): 75.0
接口响应码: 404
2023-04-25 17:28:40.603 | WARNING | utils.requests_control:cookie_token:33 - 未获取到登录认证信息,请检查: <urllib3.connection.HTTPSConnection object at 0x10519ed30>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
2023-04-25 17:28:40.643 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:28:40.728 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/phegda/advrest
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'terminal': 'PC', 'apiVersion': '1.0.1', 'site': {'Siteid': 1015, 'siteName': 'waibu', 'qid': '04478', 'pageType': 'null', 'pageUrl': 'https://www.tianqi.com/shanghai/', 'newsType': 'null', 'keywords': '上海天气,上海天气预报,上海天气预报查询,上海今日天气,上海周末天气,上海一周天气预报,上海天气预报一周,天气预报查询一周,上海天气预报10天,上海天气预报查询15天,上海未来一周的天气预报,上海天气情况,上海40日天气预报,天气预报40天,上海30日天气预报,天气预报30天'}, 'device': {'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'geo': {'country': '', 'city': ''}, 'dnt': 0, 'deviceType': 'PC', 'os': 'MACOS', 'osVersion': 'X', 'height': 1080, 'width': 1920}, 'user': {'userId': '16805156868205498'}, 'imp': [{'slotId': 102298, 'pageNum': '4', 'styleId': [1004]}], 'requestId': '16805156922345810'}
响应数据: {'data': {'ad': [], 'bidId': '3af5848b85f7e4bb31e493d38f3061ca', 'requestId': '16805156922345810'}, 'success': True}
响应耗时(ms): 83.0
接口响应码: 200
2023-04-25 17:28:53.645 | WARNING | utils.requests_control:cookie_token:33 - 未获取到登录认证信息,请检查: <urllib3.connection.HTTPSConnection object at 0x105b56d00>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
2023-04-25 17:28:53.677 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:28:53.760 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/phegda/advrest
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'terminal': 'PC', 'apiVersion': '1.0.1', 'site': {'Siteid': 1015, 'siteName': 'waibu', 'qid': '04478', 'pageType': 'null', 'pageUrl': 'https://www.tianqi.com/shanghai/', 'newsType': 'null', 'keywords': '上海天气,上海天气预报,上海天气预报查询,上海今日天气,上海周末天气,上海一周天气预报,上海天气预报一周,天气预报查询一周,上海天气预报10天,上海天气预报查询15天,上海未来一周的天气预报,上海天气情况,上海40日天气预报,天气预报40天,上海30日天气预报,天气预报30天'}, 'device': {'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'geo': {'country': '', 'city': ''}, 'dnt': 0, 'deviceType': 'PC', 'os': 'MACOS', 'osVersion': 'X', 'height': 1080, 'width': 1920}, 'user': {'userId': '16805156868205498'}, 'imp': [{'slotId': 102298, 'pageNum': '1', 'styleId': [1004]}], 'requestId': '16805156922345810'}
响应数据: {'data': {'ad': [], 'bidId': '0253b4be85f76ad63563e788d83061ca', 'requestId': '16805156922345810'}, 'success': True}
响应耗时(ms): 81.0
接口响应码: 200
2023-04-25 17:29:20.790 | WARNING | utils.requests_control:cookie_token:33 - 未获取到登录认证信息,请检查: <urllib3.connection.HTTPSConnection object at 0x103b72d30>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
2023-04-25 17:29:20.830 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 17:29:21.012 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/phegda/advrest
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'terminal': 'PC', 'apiVersion': '1.0.1', 'site': {'Siteid': 1015, 'siteName': 'waibu', 'qid': '04478', 'pageType': 'null', 'pageUrl': 'https://www.tianqi.com/shanghai/', 'newsType': 'null', 'keywords': '上海天气,上海天气预报,上海天气预报查询,上海今日天气,上海周末天气,上海一周天气预报,上海天气预报一周,天气预报查询一周,上海天气预报10天,上海天气预报查询15天,上海未来一周的天气预报,上海天气情况,上海40日天气预报,天气预报40天,上海30日天气预报,天气预报30天'}, 'device': {'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'geo': {'country': '', 'city': ''}, 'dnt': 0, 'deviceType': 'PC', 'os': 'MACOS', 'osVersion': 'X', 'height': 1080, 'width': 1920}, 'user': {'userId': '16805156868205498'}, 'imp': [{'slotId': 102298, 'pageNum': '3', 'styleId': [1004]}], 'requestId': '16805156922345810'}
响应数据: {'msg': 'bad request ...', 'success': False}
响应耗时(ms): 109.0
接口响应码: 400

View File

@ -8,7 +8,7 @@ import pytest
@allure.feature('XXX模块')
@allure.title('XXX接口')
@pytest.mark.imports
@pytest.mark.datafile('test_data/imports/test_imports.yml')
@pytest.mark.datafile('test_data/imports/imports.yml')
def test_imports(core, env, case, inputs, expectation):
# core.requests: 返回请求方法对象
# core.headers: 返回全局请求头

View File

@ -7,7 +7,7 @@ import pytest
@allure.feature('天气模块')
@allure.title('天气查询接口')
@pytest.mark.datafile('test_data/tianqi/test_tianqi.yml')
@pytest.mark.datafile('test_data/tianqi/tianqi.yml')
def test_tianqi(core, env, case, inputs, expectation):
# core.requests: 返回请求方法对象
# core.headers: 返回全局请求头

View File

@ -9,7 +9,7 @@ tests:
json: {"key": "{{phone_number()}}", "key2": "$cache.test_login"}
file: "imports.xlsx"
sql:
assert_key: success
assert_key: $..success
assert_way: unequal
expectation:
response: true
@ -20,7 +20,7 @@ tests:
json: {"key": ["{{random_int()}}"], "key2": ["$cache.test_login"]}
file: "imports.xlsx"
sql:
assert_key: success
assert_key: $..success
assert_way: unequal
expectation:
response: true

View File

@ -9,7 +9,7 @@ tests:
json:
file:
sql:
assert_key: success
assert_key: $..success
assert_way: unequal
expectation:
response: true
@ -20,7 +20,7 @@ tests:
json:
file:
sql:
assert_key: success
assert_key: $..success
assert_way: unequal
expectation:
response: true

View File

@ -9,7 +9,7 @@ tests:
json: { "terminal": "PC","apiVersion": "1.0.1","site": { "Siteid": 1015,"siteName": "waibu","qid": "04478","pageType": "null","pageUrl": "https://www.tianqi.com/shanghai/","newsType": "null","keywords": "上海天气,上海天气预报,上海天气预报查询,上海今日天气,上海周末天气,上海一周天气预报,上海天气预报一周,天气预报查询一周,上海天气预报10天,上海天气预报查询15天,上海未来一周的天气预报,上海天气情况,上海40日天气预报,天气预报40天,上海30日天气预报,天气预报30天" },"device": { "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36","geo": { "country": "","city": "" },"dnt": 0,"deviceType": "PC","os": "MACOS","osVersion": "X","height": 1080,"width": 1920 },"user": { "userId": "16805156868205498" },"imp": [ { "slotId": 102298,"pageNum": "{{random_int(1, 5)}}" ,"styleId": [ 1004 ] } ],"requestId": "16805156922345810" }
file:
sql:
assert_key: success
assert_key: $..success
assert_way: unequal
expectation:
response: true
response: false

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,27 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/4/21 10:24
# @Author : 谈林海
import os
import shutil
class CleanFile:
"""清理文件夹"""
def __init__(self, path):
self._path = path
def __call__(self, *args, **kwargs):
"""
如果文件夹不存在就创建如果文件存在就清空
"""
if not os.path.exists(self._path):
os.mkdir(self._path)
else:
shutil.rmtree(self._path)
os.mkdir(self._path)
if __name__ == '__main__':
pass

View File

@ -5,7 +5,7 @@
from pathlib import Path
from utils.path import root
from utils.fake_data_control import Mock
from utils.clean_file_control import CleanFile
from utils.pathlib_control import FileUtils
from utils.read_yaml_control import HandleYaml
@ -16,17 +16,16 @@ class CaseHandler:
"""初始化目录路径"""
self._file_path = file_path
CleanFile(root / 'test_cases')()
FileUtils.remove(root / 'test_cases')
@property
def get_data_path(self):
"""获取测试数据路径及转换为测试用例路径"""
root_dir = Path(self._file_path)
data_paths = [_path for _path in root_dir.rglob('*.yml') if
_path.is_file() and _path.name != 'cache.yml']
data_paths = FileUtils.glob_files(root / 'test_data', '*.yml')
case_paths = [Path(str(_path).replace('test_data', 'test_cases')).parent for _path in root_dir.rglob('*.yml') if
_path.is_file() and _path.name != 'cache.yml']
_path.is_file() and _path.name != 'cache.yaml']
return data_paths, case_paths
@property
@ -35,7 +34,6 @@ class CaseHandler:
_data_paths = self.get_data_path[0]
data_list = [HandleYaml(_path).read_yaml() for _path in _data_paths]
# data_dict = {k: v for k, v in zip(_data_paths, data_list)}
return data_list
def get_case_model(self, case_path):
@ -86,7 +84,7 @@ class TestCaseAutoCreate(CaseHandler):
# @Time : {Mock().now_time()}
import allure
import pytest
from utils.json_control import get_json
from utils.json_control import JsonHandler
from utils.assert_control import Assert
@ -94,10 +92,11 @@ from utils.assert_control import Assert
@pytest.mark.datafile('test_data/{feature}/{datafile}.yml')
def test_tianqi(core, env, case, inputs, expectation):
res = core.requests.request(env, {'data' if params else 'json'}=inputs[{"'params'" if params else "'json'"}], headers=core.headers).json()
assert Assert(get_json(res, inputs['assert_key']), expectation['response']).ass(inputs['assert_way']) is True"""
assert Assert(JsonHandler(res).find_one(inputs['assert_key']), expectation['response']).ass(inputs['assert_way']) is True"""
return content
if __name__ == '__main__':
TestCaseAutoCreate().generate_test_case()

View File

@ -12,7 +12,7 @@ from utils.read_yaml_control import HandleYaml
class Config(BaseModel):
"""定义配置类"""
cache_path: Union[str, Path] = root / 'test_data/cache.yml'
cache_path: Union[str, Path] = root / 'test_data/cache.yaml'
class DataHandler:

View File

@ -77,22 +77,17 @@ class FileUtils:
Path(path).mkdir(parents=True, exist_ok=exist_ok)
@staticmethod
def remove(path: Union[str, Path], ignore_errors=True) -> None:
def remove(path: Union[str, Path]) -> None:
"""
删除一个文件或目录可以选择是否忽略不存在的文件
:param path: 文件/目录路径
:param ignore_errors: 是否忽略不存在的文件
:return: None
"""
try:
path = Path(path)
if FileUtils.is_file(path):
os.remove(str(path))
elif FileUtils.is_dir(path):
os.rmdir(str(path))
except OSError:
if not ignore_errors:
raise
if not os.path.exists(path):
os.mkdir(path)
else:
shutil.rmtree(path)
os.mkdir(path)
@staticmethod
def move(src: Union[str, Path], dst: Union[str, Path], overwrite=True) -> None:
@ -212,16 +207,14 @@ class FileUtils:
f.write("\n".join(lines))
@staticmethod
def glob_files(pattern: Union[str, Path]) -> List[Path]:
def glob_files(root_dir: Union[str, Path], pattern: str) -> List[Path]:
"""
获取符合条件的所有文件
:param root_dir: 目录路径
:param pattern: 匹配模式可以使用 * 通配符
:return: List[Path]
"""
path = Path(pattern)
if not path.is_absolute():
path = Path.cwd() / path
return list(Path.glob(path))
return [_path for _path in Path(root_dir).rglob(pattern) if _path.is_file()]
@staticmethod
def get_size(path: Union[str, Path], human_readable=True) -> Union[int, str]:
@ -247,8 +240,8 @@ class FileUtils:
suffixes = ["B", "KB", "MB", "GB", "TB"]
suffix_index = 0
while size > 1024 and suffix_index < 4:
suffix_index += 1 # increment the index of the suffix
size = size / 1024.0 # apply the division
suffix_index += 1
size = size / 1024.0
return "%.*f%s" % (precision, size, suffixes[suffix_index])
@staticmethod
@ -271,3 +264,4 @@ class FileUtils:
"""
path = Path(path)
return path.name, path.suffix, FileUtils.get_size(path)

View File

@ -36,4 +36,4 @@ class HandleYaml:
if __name__ == '__main__':
HandleYaml('/Users/tanlinhai/PythonProjects/t2-api-autotest/test_data/tianqi/test_tianqi.yml').read_yaml()
HandleYaml('/test_data/tianqi/tianqi.yml').read_yaml()