05-pytest-通过confest.py共享fixture

Posted 爱学习de测试小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了05-pytest-通过confest.py共享fixture相关的知识,希望对你有一定的参考价值。

目录

使用场景

代码示例


使用场景

  • 多个.py文件调用的公共功能可抽取出来,写成配置文件形式读取confest.py的配置

代码示例

  confest.py文件

# -*- coding: utf-8 -*-
# @Time    : 2021/10/9
# @Author  : 大海
import pytest

"""
注意以下3点:
   conftest.py配置脚本名称是固定的,不能改名称
   conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
   不需要import导入 conftest.py,pytest用例会自动查找
"""


@pytest.fixture()
def open_url():
    print("打开网址!")

  注意:

  • conftest.py配置脚本名称是固定的,不能改名称
  • conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
  • 不需要import导入 conftest.py,pytest用例会自动查找

  test_05.py

# -*- coding: utf-8 -*-
# @Time    : 2021/10/9
# @Author  : 大海
import pytest


def test_login(open_url):
    print('这是登录成功case')


def test_other():
    print('这是未登录状态case')


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

  test_06.py

# -*- coding: utf-8 -*-
# @Time    : 2021/10/9
# @Author  : 大海
import pytest


def test_login(open_url):
    print('这是登录成功test_06')


def test_other():
    print('这是未登录状态case')


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

  说明:运行上面两个文件,都可以调用到open_url方法,这样就实现抽取公共方法

以上是关于05-pytest-通过confest.py共享fixture的主要内容,如果未能解决你的问题,请参考以下文章

docker通过运行容器制作共享tar镜像包

锁对象只能通过继承在进程之间共享

Linux篇第十一篇——进程间通信(管道+system V共享内存)

Linux篇第十一篇——进程间通信(管道+system V共享内存)

出售周边+品牌合作!共享单车还能玩出什么新花样

在 Flink 中的算子之间共享状态