• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

自研开源项目4pytest框架推荐的自动化测试架构和脚本模板caterpillar_pytest_templates

武飞扬头像
redrose2100
帮助1

更多信息请关注 个人网站

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

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

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

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

一、项目源码地址

caterpillar_pytest_templates源码地址

二、目录结构说明


tests---------------------------------------------------------------------------------------
  |-------__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 mark
    usability: usability mark
    compatibility: compatibility mark
    reliability: reliability mark
    safety: safety mark
    performance: performance mark
学新通

四、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 -r tests/test_requirements.txt
python3 -m pytest -s tests --alluredir tests/reports/allure_report

path配置路径为:

tests/reports/allure_report

详细见下图所示
学新通

七、jenkins展示的allure报告如下

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjachj
系列文章
更多 icon
同类精品
更多 icon
继续加载