0
点赞
收藏
分享

微信扫一扫

自研开源项目(4)基于pytest框架推荐的自动化测试架构及脚本模板caterpillar_pytest_templates


更多信息请关注 个人网站

​​自研开源项目(1)邮件收发解析处理高层封装应用库caterpillar_mail​​

​​自研开源项目(2)打印日志高层封装应用库caterpillar_log​​

​​自研开源项目(3)基于pytest和数据驱动的自定义接口自动化框架caterpillar_apitest​​

​​自研开源项目(4)基于pytest框架推荐的自动化测试架构及脚本模板caterpillar_pytest_templates​​

一、项目源码地址

​​caterpillar_pytest_templates源码地址​​

二、目录结构说明

|-------__init__.py   # python用来标识package的文件
|-------conftest.py # 用来定义pytest自动化脚本中的session级别的fixture
|-------pytest.ini # 用来重新定义pytest默认行为的文件
|-------test_requirements.txt # 用来指定当前自动化测试脚本需要依赖的第三方python包
|-------libs # 用来存放自己封装的一些公共的函数库
|--------__init__.py # python中用来标识package的文件
|--------common.py # 自己封装的公共方法库,这里为示例
|-------testcases # 用来存放Python自动化测试脚本的目录
|--------__init__.py # python中用来标识package的文件
|--------test_demo01.py # 自动化脚本示例
|--------test_demo02.py # 自动化脚本示例

三、pytest.ini文件的说明

当前内容如下:这里定义了一些默认行为
(1)使用当前pytest.ini文件识别自动化脚本文件的命名必须为 test_开头或者_test.py结尾的文件,文件中测试类的命名必须以Test 开头或者Test结尾的类,测试脚本的函数方法必须以test_开头或者_test结尾的函数
(2)当前pytest.ini规定了不会去libs文件夹寻找自动化脚本
(3)当前pytest.ini规定了只会去testcases文件夹下寻找脚本
(4)最下面的那些指定了打这些标签的时候,pytest不会产生告警,如果打了其他的标签是会有告警产生的,如果忽略告警,需要到这里增加配置

[pytest]
markers=
# 自定义自动化脚本文件、类、函数名的命名规则
python_files = test_* *_test.py
python_classes = Test* *Test
python_functions = test_* *_test
# 指定脚本不搜索目录
norecursedirs=libs
# 指定脚本搜索目录
testpaths=testcases
# 消除mark标签产生的告警
smoke: smoke mark
function: function

四、conftest.py文件说明

当前conftest.py定义了在所有脚本执行之前会执行的函数,如果有一些公共操作,可以直接在conftest.py中prepare_before_all_case方法中直接增加即可

import pytest


@pytest.fixture(scope="session",autouse=True)
def prepare_before_all_case():
print("初始化步骤:在所有的自动化脚本之前自动执行一次")

五、自动化测试脚本文件的说明

(1)test_demo01.py文件
此实例脚本为测试步骤在当前脚本中定义,一个文件即为一个脚本,allure标注的对脚本功能本身没有实际意义,只是为了生成漂亮美观的日志

# 引入的第三方库需要在 tests/test_requirements.txt中注明
import pytest
import allure


@allure.step("测试步骤一:根据实际描述填写")
def step_01(): # 步骤函数命名不能以test_开头,否则将被识别为自动化用例
print("测试步骤一:根据实际描述填写")

@allure.step("测试步骤二:根据实际描述填写")
def step_02(): # 步骤函数命名不能以test_开头,否则将被识别为自动化用例
print("测试步骤二:根据实际描述填写")

@allure.step("测试步骤一:根据实际描述填写")
def step_03(): # 步骤函数命名不能以test_开头,否则将被识别为自动化用例
print("测试步骤二:根据实际描述填写")


@allure.feature("特性(对应敏捷开发中的feature)")
@allure.issue(url="",name="用例对应issuer的链接,若没有可删除此行")
@allure.link(url="",name="用例对应需求的链接,若没有,可删除此行")
@allure.story("故事(对应敏捷开发中的story)")
@allure.severity('用例的级别,一般常用的级别为:blocker(阻塞缺陷),critical(严重缺陷),normal(一般缺陷),minor次要缺陷,trivial(轻微缺陷)')
@allure.title("测试用例标题")
@allure.description("测试用例简要描述")
def test_demo01():
step_01()
step_02()
step_03()


if __name__=="__main__":
pytest.main(["-s","test_demo01"])

(2)test_demo02.py文件
此实例脚本演示的是测试步骤为一些公共的步骤,封装在自定义的库里,allure标注的同样也都是为了漂亮美观的日志,对脚本功能本身没有实际意义

# 引入的第三方库需要在 tests/test_requirements.txt中注明
import pytest
import allure
from ..libs import common


@allure.feature("特性(对应敏捷开发中的feature)")
@allure.issue(url="",name="用例对应issuer的链接,若没有可删除此行")
@allure.link(url="",name="用例对应需求的链接,若没有,可删除此行")
@allure.story("故事(对应敏捷开发中的story)")
@allure.severity('用例的级别,一般常用的级别为:blocker(阻塞缺陷),critical(严重缺陷),normal(一般缺陷),minor次要缺陷,trivial(轻微缺陷)')
@allure.title("测试用例标题")
@allure.description("测试用例简要描述")
def test_demo02():
common.common_step_01()
common.common_step_02()
common.common_step_03()

if __name__=="__main__":
pytest.main(["-s","test_demo02"])

其中comm.py文件的代码为:

import allure


@allure.step("封装的公共步骤一:根据实际应用描述及命名函数")
def common_step_01(): # 函数名根据实际含义自行命名,这里为示例
print("封装的公共步骤一:根据实际应用描述及命名函数")

@allure.step("封装的公共步骤二:根据实际应用描述及命名函数")
def common_step_02(): # 函数名根据实际含义自行命名,这里为示例
print("封装的公共步骤二:根据实际应用描述及命名函数")

@allure.step("封装的公共步骤三:根据实际应用描述及命名函数")
def common_step_03(): # 函数名根据实际含义自行命名,这里为示例
print("封装的公共步骤三:根据实际应用描述及命名函数")

六、与jenkins结合配置流水线

jenkins中流水线配置如下
其中shell执行命令如下:

pip3 install

path配置路径为:

tests/reports/allure_report

详细见下图所示

自研开源项目(4)基于pytest框架推荐的自动化测试架构及脚本模板caterpillar_pytest_templates_python

七、jenkins展示的allure报告如下

自研开源项目(4)基于pytest框架推荐的自动化测试架构及脚本模板caterpillar_pytest_templates_jenkins_02


举报

相关推荐

0 条评论