08 接口自动化-用例管理框架pytest之fixtrue,conftest.py,allure报告以及logo定制

文章目录

一、使用fixture实现部分前后置

语法:
@pytest.fixture(scope="作用域",params="数据驱动",autouser="自动执行",ids="自定义参数名称",name="别名")

  • scope="作用域"
    • functioin:在每个方法(测试用例)的前后执行一次。
    • class:在每个类的前后执行一次。
    • module:在每个py文件前后执行一次。
    • package/session:每个package前后执行一次。
1.function级别:在每个函数的前后执行
复制代码
import pytest
@pytest.fixture(scope="function")
def execute_sql():
    print("执行数据库的验证,查询数据库。")
    yield "execute_sql"
    print("关闭数据库的连接")

调用:

复制代码
class Test03:
    @pytest.mark.smoke
    def test_01(self, execute_sql):
        print("Test03--test01--" + execute_sql)

yieldreturn,都可以返回值,并且返回的值可以在测试用例中获取。

  • yield生成器,返回一个对象,对象中可以有多个值,yield后面可以接代码。
  • return 返回一个值,return后面不能接代码。

注意:如果加入autouse=True参数,那么表示不需要手动调用,会自动调用,那么和setup、teardown功能一致。

2.class级别:在每个类的前后执行一次
复制代码
@pytest.fixture(scope="class")
def execute_sql_class():
    print("execute_sql_class-执行数据库的验证,查询数据库。")
    yield
    print("execute_sql_class-关闭数据库的连接")

调用:

复制代码
@pytest.mark.usefixtures("execute_sql_class")
class Test03:
3.module级别:在每个模块的前后执行一次

和setup_module和teardown_module效果一样

复制代码
@pytest.fixture(scope="module",autouse=True)
def execute_sql_module():
    print("execute_sql_module-执行数据库的验证,查询数据库。")
    yield
    print("execute_sql_module-关闭数据库的连接")
4.package、session级别,一般是和connftest.py文件一起使用

autouse=True 自动调用
params参数: 用于传输数据(list,tuple,字典列表,字典元组):

  • 需要在夹具里面通过request(固定写法)接收,

  • 再通过request.param(固定写法)获取数据,

  • 最后通过yield把数据返回到测试用例中,然后使用

    import pytest
    def read_yaml():
    return["无忧渡","折腰","淮水竹亭"]
    @pytest.fixture(scope="function",params=read_yaml())
    def execute_sql_func(request):
    print("execute_sql_func-执行数据库的验证,查询数据库。")
    yield request.param
    print("execute_sql_func-关闭数据库的连接")

ids参数: 它要和 params 一起使用,自定义参数名称。意义不大。了解即可

复制代码
import pytest
def read_yaml():
    return["无忧渡","折腰","淮水竹亭"]
@pytest.fixture(scope="function",params=read_yaml(),ids=
['wyd','zy','hszt'])
def execute_sql_func(request):
    print("execute_sql_func-执行数据库的验证,查询数据库。")
    yield request.param
    print("execute_sql_func-关闭数据库的连接")

name参数:fixture固件取的别名。意义不大。了解即可,用了别名后,真名会失效,只能使用别名。

复制代码
@pytest.fixture(scope="function",params=read_yaml(),ids=
['wyd','zy','hszt'],name="func")
def execute_sql_func(request):
    print("execute_sql_func-执行数据库的验证,查询数据库。")
    yield request.param
    print("execute_sql_func-关闭数据库的连接")

@pytest.mark.usefixtures("func")
class Test03:
    @pytest.mark.smoke
    def test_01(self, func):
        print("Test03--test01--" + func)

二、当fixture的级别为package,session时,那么一般和conftest.py文件一起使用。

1.名称是固定的conftest.py,主要用于单独的存放fixture固件的。
2.级别为packagesession时,那么可以在多个包甚至多个py文件里面共享前后置。

举例:登录。

模块:模块的共性。
3.发现conftest.py文件里面的fixture不需要导包可以直接使用。
4.conftest.py文件,可以有多个。
作用: 出现重复日志,初始化一次日志对象。规避日志重复。连接数据库。关闭数据库。
注意: 多个前置同时存在的优先级。

  • conftest.py为function级别时优先级高于setup/teardown
  • conftest.py为class级别时优先级高于setup_class/teardown_class
  • conftest.py为session级别时优先级高于setup_module/teardown_module

/tesecases/conftest.py 夹具:

复制代码
@pytest.fixture(scope="function")
def execute_sql_func_2():
    print("execute_sql_func_2")

/tesecases/user/conftest.py 夹具:

复制代码
import pytest

@pytest.fixture(scope="function")
def user_func():
    print("user--共性")

调用多个conftest.py 中的夹具:

复制代码
    def test_01(self, execute_sql_func_2, user_func):
        print("Test03--test01--" + execute_sql_func_2)
        print(user_func)

三、pytest断言

使用的是python原生的assert

比如:assert 'abc' in 'abcd'

四、pytest结合allure-pytest实现生成allure报告

第一步:下载allure文件
  • 1.官网下载allure文件
  • 2.下载之后解压到非中文的目录
  • 3.把bin路径配置到系统变量path中:E:\allure-2.13.7\bin (注意分号不要是中文的)
第二步:安装allure
  • 安装allure报告:pip install allure-pytest
  • 验证:allure --version
  • 注意:可能需要重启 pycharm。
第三步:执行命令生成报告
  • 1.在reports目录下生成temps目录,并且在temps目录下生产临时的json格式的临时报告
    os.system("hrun testsuites/test_suites.yml ‐‐alluredir=reports/temps ‐‐clean‐alluredir")
    加上--clean-alluredir表示:每执行一次把原来的清除。

  • 2.根据临时json报告生成allure报告
    os.system("allure generate reports/temps ‐o reports/allures ‐‐clean")
    加上--clean表示:每执行一次把原来的清除。

  • pytest.ini 文件代码:

    [pytest]
    addopts = ‐vs -m smoke ‐‐alluredir=reports/temps ‐‐clean‐alluredir
    testpaths = testcases/
    python_files = test_.py
    python_classes = Test

    python_functions = test_*
    markers =
    smoke: smoke testcases
    product: product testcases

  • all.py 文件代码:

    import os
    import time
    import pytest
    if name == "main":
    pytest.main()
    time.sleep(3)
    os.system("allure generate reports/temps ‐o reports/allures ‐‐clean")
    # 指定字符串运行测试用例:
    # pytest.main(['-vs','--reruns=2','-k','01'])
    # 指定模块运行测试用例:
    # pytest.main(['-vs','testcases/test_01.py'])
    # 指定文件夹运行测试用例:
    # pytest.main(['-vs','testcases/'])
    # 通过node id的方式运行测试用例:
    # pytest.main(['-vs','testcases/test_02.py::Test02::test_02'])

第四步:实现logo定制
1.修改\allure-2.x.x\config下的allure.yml配置文件,加入:最后一

句加上自定义logo插件。

复制代码
plugins:
  - junit-xml-plugin
  - xunit-xml-plugin
  - trx-plugin
  - behaviors-plugin
  - packages-plugin
  - screen-diff-plugin
  - xctest-plugin
  - jira-plugin
  - xray-plugin
  - custom-logo-plugin
2.修改插件里面的图片和样式
复制代码
.side‐nav__brand{
	background: url('logo.png') no‐repeat left center !important;
	margin‐left: 20px;
	height: 90px;
	background‐size: contain !important;
	}
.side‐nav__brand‐text{
	display: none;
	}
相关推荐
Bug改不动了1 分钟前
LangGraph基础知识(MemorySaver/SqliteSaver )(三)
python·langgraph
这里有鱼汤1 小时前
Python初学者常犯的错误汇总,建议收藏
后端·python
Johny_Zhao18 小时前
CentOS Stream 8 高可用 Kuboard 部署方案
linux·网络·python·网络安全·docker·信息安全·kubernetes·云计算·shell·yum源·系统运维·kuboard
站大爷IP18 小时前
精通einsum():多维数组操作的瑞士军刀
python
站大爷IP19 小时前
Python与MongoDB的亲密接触:从入门到实战的代码指南
python
Roc-xb20 小时前
/etc/profile.d/conda.sh: No such file or directory : numeric argument required
python·ubuntu·conda
世由心生21 小时前
[从0到1]环境准备--anaconda与pycharm的安装
ide·python·pycharm
猛犸MAMMOTH21 小时前
Python打卡第54天
pytorch·python·深度学习
梓羽玩Python1 天前
12K+ Star的离线语音神器!50MB模型秒杀云端API,隐私零成本,20+语种支持!
人工智能·python·github
成都犀牛1 天前
LangGraph 深度学习笔记:构建真实世界的智能代理
人工智能·pytorch·笔记·python·深度学习