优化代码结构,去掉冗余代码

This commit is contained in:
wangjie 2025-03-18 16:35:21 +08:00
parent 686988b126
commit 9d36982eef
5 changed files with 78 additions and 32 deletions

View File

@ -8,7 +8,6 @@ import time
import pytest
from common.models import TestMetrics
from utils.report_data_handle import ReportDataHandle

View File

@ -3,10 +3,11 @@
from common.base_api import BaseApi
class Login(BaseApi):
class Login:
"""登录模块"""
def login(self, username: str, password: str):
@staticmethod
def login(username: str, password: str):
"""获取登录权限"""
address = '/user/login'
json = {
@ -14,8 +15,8 @@ class Login(BaseApi):
'password': password
}
return self.send_post_request(address, json_data=json)
return BaseApi.send_post_request(address, json_data=json)
if __name__ == '__main__':
r = Login().login('18800000001', '123456')
r = Login.login('18800000001', '123456')

View File

@ -1,16 +1,33 @@
[pytest]
;命令行参数(可添加多个,以空格分隔)
;addopts = -vs testCase/ --alluredir ./Temp --html=./outFiles/pytest_report/report.html --capture=sys --self-contained-html
# 命令行参数
;addopts = -vs
; --alluredir=./outFiles/Temp
; --html=./outFiles/pytest_report/pytest_report.html
; --self-contained-html
; --clean-alluredir
; --capture=sys
; --allure-no-captureprint()和日志的输出不会进入 Allure 报告Allure 只会收集手动添加的 allure.attach() 相关内容)
; --allure-link-pattern=issue:http://your-issue-tracker/{}
; --allure-link-pattern=test_case:http://your-test-case-tracker/{}
# 测试文件配置
;testpaths = testCase/
;python_files = test_*.py
;python_classes = Test*
;python_functions = test_*
# 严格模式(让 @pytest.mark.xfail 失败的测试用例在“意外通过”时,变为真正的失败)
;xfail_strict = true
;自定义mark
# 自定义标记
markers =
demo : marks tests as demo
smoke: marks tests as smoke
test : marks tests as test
smoke: 冒烟测试用例
demo: 演示用例
test: 测试用例
api: API测试用例
high: 高优先级
medium: 中优先级
low: 低优先级
;日志相关
;控制台输出日志内容默认False

View File

@ -17,7 +17,7 @@ def get_token():
"""获取登录V1的token"""
logger.info("开始用例前置操作")
# 调登录接口获取登录接口的token
login_response = Login().login('18800000001', '123456')
login_response = Login.login('18800000001', '123456')
token = BaseApi.get_json(login_response)['data']['token']
CacheHandler.set_cache('Authorization', f'Bearer {token}')
logger.info("结束用例前置操作")

View File

@ -5,38 +5,67 @@ import os
import allure
import pytest
from common.base_api import BaseApi
from configs.paths_config import DATAS_DIR
from pageApi.login import Login
from utils.allure_handle import allure_attach_json
from utils.yaml_handle import YamlHandle
@allure.feature("登录模块")
class TestLogin:
data_smsCode = YamlHandle(DATAS_DIR + os.sep + 'sms_code.yaml').read_yaml()
params = [(item['case_title'], item['expected']) for item in data_smsCode]
@allure.story("测试获取验证码")
@allure.title('{case_title}')
@allure.severity(allure.severity_level.BLOCKER)
@pytest.mark.run(order=1)
@pytest.mark.parametrize('case_title,message', params)
@pytest.mark.dependency(name='get_smsCode')
# @pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_smsCode(self, case_title, message):
"""获取验证码"""
r = "获取验证码成功"
assert r == message
"""测试登录相关功能"""
# 加载测试数据
data_sms_code = YamlHandle(DATAS_DIR + os.sep + 'sms_code.yaml').read_yaml()
params_sms = [(item['case_title'], item['expected']) for item in data_sms_code]
data_login = YamlHandle(DATAS_DIR + os.sep + 'login.yaml').read_yaml()
params = [(item['case_title'], item['username'], item['password'], item['expected']) for item in data_login]
params_login = [(item['case_title'], item['username'], item['password'], item['expected']) for item in data_login]
@allure.severity(allure.severity_level.BLOCKER)
@allure.story("测试获取验证码")
@allure.title('{case_title}')
@allure.description("""
测试获取短信验证码接口:
1. 验证接口是否正常返回
2. 验证错误提示信息
""")
@pytest.mark.run(order=1)
@pytest.mark.parametrize('case_title,message', params_sms)
@pytest.mark.dependency(name='get_sms_code')
# @pytest.mark.flaky(reruns=3, reruns_delay=2)
def test_sms_code(self, case_title, message):
"""获取验证码"""
with allure.step("准备请求数据"):
allure_attach_json("请求参数", {"message": message})
with allure.step("验证响应结果"):
response = "获取验证码成功"
assert response == message
@allure.severity(allure.severity_level.CRITICAL)
@allure.story("测试登录")
@allure.title('{case_title}')
@allure.severity(allure.severity_level.CRITICAL)
@allure.description("""
测试用户登录接口:
1. 验证接口是否正常返回
2. 验证错误场景处理
""")
@pytest.mark.run(order=2)
@pytest.mark.parametrize('case_title, username, password,message', params)
@pytest.mark.dependency(depends=["get_smsCode"], scope='class')
@pytest.mark.parametrize('case_title, username, password,message', params_login)
@pytest.mark.dependency(depends=["get_sms_code"], scope='class')
def test_login(self, case_title, username, password, message):
"""登录测试"""
r = Login().login(username, password)
assert r.json()['errorMsg'] == message
with allure.step("准备登录请求数据"):
login_data = {
"username": username,
"password": password
}
allure_attach_json("请求参数", login_data)
with allure.step("发送登录请求"):
response = Login.login(username, password)
with allure.step("验证响应结果"):
response_data = BaseApi.get_json(response)
assert response_data['errorMsg'] == message, f"接口返回错误:{response_data['message']}"