1. 增加测试数据分析预警机制 2. 优化自动生成测试用例方法 3. 将用例跳过方法 提取成公共的fixture,自动运行 4. 修改pytet-html测试报告列’用例描述’值获取 5. 根 据最新修改,更新readme文件~
This commit is contained in:
@@ -13,6 +13,18 @@ from loguru import logger
|
||||
from common_utils.base_request import BaseRequest
|
||||
|
||||
|
||||
@pytest.fixture(scope="function", autouse=True)
|
||||
def case_skip(request):
|
||||
"""处理跳过用例"""
|
||||
# 使用 request.getfixturevalue() 方法来获取测试用例函数的参数值
|
||||
# 注意这里的"case"需要与@pytest.mark.parametrize("case", cases)中传递的保持一致
|
||||
case = request.getfixturevalue("case")
|
||||
if case.get("run") is None or case.get("run") is False:
|
||||
reason = f"{case.get('title')}: 标记了该用例为false,不执行\\n"
|
||||
logger.warning(f"{reason}")
|
||||
pytest.skip(reason)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def login_init():
|
||||
"""
|
||||
@@ -28,7 +40,7 @@ def login_init():
|
||||
req_data = {
|
||||
"url": host + "/api/accounts/login.json",
|
||||
"method": "POST",
|
||||
"pk": "json",
|
||||
"request_type": "json",
|
||||
"headers": {"Content-Type": "application/json; charset=utf-8;"},
|
||||
"payload": {"login": login, "password": password, "autologin": 1}
|
||||
}
|
||||
|
||||
@@ -10,33 +10,25 @@
|
||||
import pytest
|
||||
from loguru import logger
|
||||
from pytest_html import extras # 往pytest-html报告中填写额外的内容
|
||||
from common_utils.func_handle import add_docstring
|
||||
import allure
|
||||
from case_utils.allure_handle import allure_title, allure_step
|
||||
|
||||
# 读取用例数据
|
||||
cases = [{"title": "demo case 01", "user": "flora1", "age": 17, "run": False},
|
||||
{"title": "demo case 02", "user": "lucy", "age": 17, "run": False}]
|
||||
cases = [{"title": "demo用例01", "user": "flora", "age": 17, "run": True},
|
||||
{"title": "demo用例02", "user": "lucy", "age": 17, "run": False}]
|
||||
|
||||
|
||||
@allure.story("demo模块")
|
||||
@pytest.mark.test_login_demo
|
||||
@pytest.mark.parametrize("case", cases)
|
||||
@allure.story("demo模块(手动用例)")
|
||||
@pytest.mark.test_demo
|
||||
@pytest.mark.parametrize("case", cases, ids=["{}".format(case["title"]) for case in cases])
|
||||
def test_demo(case, extra):
|
||||
logger.info("-----------------------------START-开始执行用例-----------------------------")
|
||||
logger.debug(f"当前执行的用例数据:{case}")
|
||||
# 给当前测试方法添加文档注释
|
||||
add_docstring(case.get("title", ""))(test_demo)
|
||||
# 添加用例标题作为allure中显示的用例标题
|
||||
allure_title(case.get("title", ""))
|
||||
if case.get("run", None):
|
||||
# 将用例数据显示在pytest-html报告中
|
||||
extra.append(extras.json(case, name="用例数据"))
|
||||
# 在allure报告中显示请求的用例数据
|
||||
allure_step(step_title="用例数据", content=f"{case}")
|
||||
assert case["user"] == "flora"
|
||||
else:
|
||||
reason = f"标记了该用例为false,不执行\\n"
|
||||
logger.warning(f"{reason}")
|
||||
pytest.skip(reason)
|
||||
# 将用例数据显示在pytest-html报告中
|
||||
extra.append(extras.json(case, name="用例数据"))
|
||||
# 在allure报告中显示请求的用例数据
|
||||
allure_step(step_title="用例数据", content=f"{case}")
|
||||
assert case["user"] == "flora"
|
||||
logger.info("-----------------------------END-用例执行结束-----------------------------")
|
||||
|
||||
@@ -15,27 +15,33 @@ from case_utils.assert_handle import assert_response, assert_sql
|
||||
from loguru import logger
|
||||
from case_utils.request_data_handle import RequestPreDataHandle, RequestHandle, after_request_extract
|
||||
from pytest_html import extras # 往pytest-html报告中填写额外的内容
|
||||
from common_utils.func_handle import add_docstring
|
||||
from case_utils.allure_handle import allure_title
|
||||
import allure
|
||||
from config.settings import db_info
|
||||
from config.global_vars import GLOBAL_VARS
|
||||
|
||||
# 读取用例数据
|
||||
cases = YamlHandle(filename=os.path.join(DATA_DIR, "test_login_demo.yaml")).read_yaml
|
||||
yaml_data = YamlHandle(filename=os.path.join(DATA_DIR, "login_demo.yaml")).read_yaml
|
||||
case_common = yaml_data["case_common"]
|
||||
cases = []
|
||||
for k, v in yaml_data.items():
|
||||
if k != "case_common":
|
||||
cases.append(v)
|
||||
|
||||
|
||||
@allure.story(f'{cases["case_common"]["allure_story"]}')
|
||||
@pytest.mark.test_login_demo
|
||||
@pytest.mark.parametrize("case", cases.get("case_info"))
|
||||
def test_login_demo(case, extra):
|
||||
logger.info("-----------------------------START-开始执行用例-----------------------------")
|
||||
logger.debug(f"当前执行的用例数据:{case}")
|
||||
# 给当前测试方法添加文档注释
|
||||
add_docstring(case.get("title", ""))(test_login_demo)
|
||||
# 添加用例标题作为allure中显示的用例标题
|
||||
allure_title(case.get("title", ""))
|
||||
if case.get("run", None):
|
||||
@allure.epic(case_common["allure_epic"])
|
||||
@allure.feature(case_common["allure_feature"])
|
||||
class TestLoginDemo:
|
||||
|
||||
@allure.story(case_common["allure_story"])
|
||||
@pytest.mark.test_login_demo
|
||||
@pytest.mark.auto
|
||||
@pytest.mark.parametrize("case", cases, ids=["{}".format(case["title"]) for case in cases])
|
||||
def test_login_demo_auto(self, case, extra):
|
||||
logger.info("-----------------------------START-开始执行用例-----------------------------")
|
||||
logger.debug(f"当前执行的用例数据:{case}")
|
||||
# 添加用例标题作为allure中显示的用例标题
|
||||
allure_title(case.get("title", ""))
|
||||
# 处理请求前的用例数据
|
||||
case_data = RequestPreDataHandle(case).request_data_handle()
|
||||
# 将用例数据显示在pytest-html报告中
|
||||
@@ -50,8 +56,4 @@ def test_login_demo(case, extra):
|
||||
assert_sql(db_info[GLOBAL_VARS["env_key"]], case_data["assert_sql"])
|
||||
# 断言成功后进行参数提取
|
||||
after_request_extract(response, case_data.get("extract", None))
|
||||
else:
|
||||
reason = f"标记了该用例为false,不执行\\n"
|
||||
logger.warning(f"{reason}")
|
||||
pytest.skip(reason)
|
||||
logger.info("-----------------------------END-用例执行结束-----------------------------")
|
||||
logger.info("-----------------------------END-用例执行结束-----------------------------")
|
||||
|
||||
Reference in New Issue
Block a user