From 1adbd7336baf3007d73967eeca2f9d48d28ca39f Mon Sep 17 00:00:00 2001 From: Wu Clan Date: Thu, 29 Aug 2024 12:53:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=BB=86=E8=8A=82=E5=A4=84=E7=90=86=20(#201)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 优化部分文件细节处理 * 修复typing检测 * 细节抛光 * 修复格式化 --- httpfpt/conftest.py | 2 +- httpfpt/run.py | 9 +++++---- .../test_project/test_api_testcase_template.py | 12 ++++-------- httpfpt/testcases/test_project/test_only_skip.py | 12 ++++-------- httpfpt/testcases/test_project/test_upload_file.py | 12 ++++-------- httpfpt/utils/case_auto_generator.py | 4 ++-- httpfpt/utils/cli/version.py | 8 ++++++-- httpfpt/utils/request/request_data_parse.py | 6 ++---- 8 files changed, 28 insertions(+), 37 deletions(-) diff --git a/httpfpt/conftest.py b/httpfpt/conftest.py index 9ba658f..cb086be 100644 --- a/httpfpt/conftest.py +++ b/httpfpt/conftest.py @@ -137,7 +137,7 @@ def pytest_runtest_makereport(item, call): def pytest_collection_modifyitems(items): """ - 解决数据驱动ids参数为中文时,控制台输出乱码问题 + 更新收集的测试用例配置 :param items: :return: diff --git a/httpfpt/run.py b/httpfpt/run.py index 1837724..8bf9862 100644 --- a/httpfpt/run.py +++ b/httpfpt/run.py @@ -165,8 +165,8 @@ def startup( def run( *args, # auto testcases - testcase_generate: bool = False, - testcase_re_generation: bool = True, + testcase_generate: bool = True, + testcase_re_generation: bool = False, # init clean_cache: bool = False, pydantic_verify: bool = True, @@ -212,7 +212,7 @@ def run( :return: """ try: - logo = f"""\n + banner = f"""\n /$$ /$$ /$$$$$$$$ /$$$$$$$$ /$$$$$$$ /$$$$$$$$ /$$$$$$$ /$$$$$$$$ | $$ | $$|__ $$__/|__ $$__/| $$__ $$| $$_____/| $$__ $$|__ $$__/ | $$ | $$ | $$ | $$ | $$ | $$| $$ | $$ | $$ | $$ @@ -223,9 +223,10 @@ def run( |__/ |__/ |__/ |__/ |__/ |__/ |__/ |__/ Starting... + Version: {get_version(cli=False)} """ - log.info(logo) + log.info(banner) redis_client.init() case_data.clean_cache_data(clean_cache) case_data.case_data_init(pydantic_verify) diff --git a/httpfpt/testcases/test_project/test_api_testcase_template.py b/httpfpt/testcases/test_project/test_api_testcase_template.py index 7e98552..6c38b85 100644 --- a/httpfpt/testcases/test_project/test_api_testcase_template.py +++ b/httpfpt/testcases/test_project/test_api_testcase_template.py @@ -1,21 +1,17 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import allure import pytest from httpfpt.common.send_request import send_request from httpfpt.utils.request.case_data_parse import get_testcase_data -allure_data, ddt_data, ids = get_testcase_data(filename='api_testcase_template.yaml') +ddt_data, ids = get_testcase_data(filename='api_testcase_template.yaml') -@allure.epic(allure_data['epic']) -@allure.feature(allure_data['feature']) class TestApiTestcaseTemplate: """ApicaseTemplate""" - @allure.story(allure_data['story']) - @pytest.mark.parametrize('data', ddt_data, ids=ids) - def test_api_testcase_template(self, data): + @pytest.mark.parametrize('case_data', ddt_data, ids=ids) + def test_api_testcase_template(self, case_data): """api_testcase_template""" - send_request.send_request(data) + send_request.send_request(case_data) diff --git a/httpfpt/testcases/test_project/test_only_skip.py b/httpfpt/testcases/test_project/test_only_skip.py index 9152243..9ef37c0 100644 --- a/httpfpt/testcases/test_project/test_only_skip.py +++ b/httpfpt/testcases/test_project/test_only_skip.py @@ -1,21 +1,17 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import allure import pytest from httpfpt.common.send_request import send_request from httpfpt.utils.request.case_data_parse import get_testcase_data -allure_data, ddt_data, ids = get_testcase_data(filename='only_skip.yml') +ddt_data, ids = get_testcase_data(filename='only_skip.yml') -@allure.epic(allure_data['epic']) -@allure.feature(allure_data['feature']) class TestOnlySkip: """OnlySkip""" - @allure.story(allure_data['story']) - @pytest.mark.parametrize('data', ddt_data, ids=ids) - def test_only_skip(self, data): + @pytest.mark.parametrize('case_data', ddt_data, ids=ids) + def test_only_skip(self, case_data): """only_skip""" - send_request.send_request(data) + send_request.send_request(case_data) diff --git a/httpfpt/testcases/test_project/test_upload_file.py b/httpfpt/testcases/test_project/test_upload_file.py index 68c3860..6861f8a 100644 --- a/httpfpt/testcases/test_project/test_upload_file.py +++ b/httpfpt/testcases/test_project/test_upload_file.py @@ -1,21 +1,17 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import allure import pytest from httpfpt.common.send_request import send_request from httpfpt.utils.request.case_data_parse import get_testcase_data -allure_data, ddt_data, ids = get_testcase_data(filename='upload_file.json') +ddt_data, ids = get_testcase_data(filename='upload_file.json') -@allure.epic(allure_data['epic']) -@allure.feature(allure_data['feature']) class TestUploadFile: """UploadFile""" - @allure.story(allure_data['story']) - @pytest.mark.parametrize('data', ddt_data, ids=ids) - def test_upload_file(self, data): + @pytest.mark.parametrize('case_data', ddt_data, ids=ids) + def test_upload_file(self, case_data): """upload_file""" - send_request.send_request(data) + send_request.send_request(case_data) diff --git a/httpfpt/utils/case_auto_generator.py b/httpfpt/utils/case_auto_generator.py index 876e8a3..f490fd1 100644 --- a/httpfpt/utils/case_auto_generator.py +++ b/httpfpt/utils/case_auto_generator.py @@ -66,8 +66,8 @@ def auto_generate_testcases(rewrite: bool = False) -> None: # -*- coding: utf-8 -*- import pytest -from common.send_request import send_request -from utils.request.case_data_parse import get_testcase_data +from httpfpt.common.send_request import send_request +from httpfpt.utils.request.case_data_parse import get_testcase_data ddt_data, ids = get_testcase_data(filename='{file_property[0]}') diff --git a/httpfpt/utils/cli/version.py b/httpfpt/utils/cli/version.py index 4b79ea6..93ae12f 100644 --- a/httpfpt/utils/cli/version.py +++ b/httpfpt/utils/cli/version.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from __future__ import annotations + import re from importlib.resources import read_text @@ -9,13 +11,15 @@ from httpfpt.utils.rich_console import console -def get_version(cli: bool = True) -> None: +def get_version(cli: bool = True) -> str | None: """获取版本号""" ver = read_text('httpfpt', '__init__.py') mob = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", ver, re.MULTILINE) if mob: + version = mob.group(1) if cli: console.print('') - console.print(f'HTTPFPT: [cyan]{mob.group(1)}[/]') + console.print(f'HTTPFPT: [cyan]{version}[/]') + return version else: raise cappa.Exit('\n❌ 未查询到版本号', code=1) diff --git a/httpfpt/utils/request/request_data_parse.py b/httpfpt/utils/request/request_data_parse.py index d3aaf1b..4c03c39 100644 --- a/httpfpt/utils/request/request_data_parse.py +++ b/httpfpt/utils/request/request_data_parse.py @@ -175,14 +175,12 @@ def proxies(self) -> dict | None: raise RequestDataParseError(_error_msg('参数 config:request:proxies 不是有效的 dict 类型')) keys = list(proxies.keys()) if 'http' not in keys or 'https' not in keys: - raise RequestDataParseError( - _error_msg('测试用例数据解析失败,参数 config:request:proxies 不符合规范') - ) + raise RequestDataParseError(_error_msg('参数 config:request:proxies 不符合规范')) for v in proxies.values(): if v is not None: if not isinstance(v, str): raise RequestDataParseError( - _error_msg(f'测试用例数据解析失败,参数 config:request:proxies:{v} 不是有效的 str 类型') + _error_msg(f'参数 config:request:proxies:{v} 不是有效的 str 类型') ) if self.request_engin == EnginType.requests: proxies = proxies