• pytest fixture 中的使用包信scope skip conftest.py 和参数化等功能使用


    pytest中所谓的夹具:

    fixture scope参数详解:
    scope: 表示fixture共享夹具的范围,有以下五个范围

    function 功能: 在函数或方法运行时执行fixture函数的功能 默认作用域
    class 功能:在类运行时调用一次。
    module 功能:在一个模块(一个py文件为一个模块)被运行时,执行一次fixture
    package 功能:当前被使用fixture的文件所在的包,执行一次fixture
    session 功能:当前被使用fixture的文件所在的整个项目目录,执行一次fixture
     

    函数作为参数或使用@pytest.mark.usefixtures("login") 引用函数来操作:

    1. import pytest
    2. @pytest.fixture(scope="class")
    3. def login():
    4. print("登录成功")
    5. return "token"
    6. # 第一种引用方式:直接将函数名当成参数传递
    7. def test_a(login):
    8. print("test_a execute! ", login)
    9. # 第二种引用方式:使用pytest.mark.usefixtures
    10. @pytest.mark.usefixtures("login")
    11. def test_b():
    12. print(login)

    conftest.py实现数据共享
    场景: 你与其他测试⼯程师合作⼀起开发时,公共的模块要在不同⽂件中,要在⼤家都访问到的地⽅。
    解决: 使⽤ conftest.py 这个⽂件进⾏数据共享,并且他可以放在不同位置起着不同的范围共享作⽤。
    前提:
    conftest ⽂件名是不能换的
    放在项⽬下是全局的数据共享的地⽅
    执⾏:
    系统执⾏到参数 login 时先从本模块中查找是否有这个名字的变量什么的,
    之后在 conftest.py 中找是否有。
    步骤: 将登陆模块带@pytest.fixture 写在 conftest.py

    conftest.py 文件

    1. import pytest
    2. '''
    3. scope : function /class / module /package /session
    4. '''
    5. @pytest.fixture(scope="class")
    6. def login():
    7. print("登录")
    8. yield "token : kjl;kj;jk;1232131"
    9. print("登出")
    10. @pytest.fixture(scope="class")
    11. def db_connect():
    12. print("连接数据库")
    13. yield
    14. print("断开数据库")

    使用夹具,作为参数传入函数:

    1. class TestClass():
    2. def test_01(self,login,db_connect):
    3. print("test -001")
    4. def test_02(self,login,db_connect):
    5. print("test -002")

     

    跳过的测试方法

    gogoabc.py

    1. class gloa(object):
    2. def max(values):
    3. _max = values[0]
    4. for val in values:
    5. if val > _max:
    6. _max = val
    7. return _max
    8. def min(values):
    9. _min = values[0]
    10. for val in values:
    11. if val < _min:
    12. _min = val
    13. return _min

    跳过使用  @pytest.mark.skip 使用测试方法跳过

    1. import pytest
    2. from two.gogoabc import gloa
    3. #跳过这个方法不执行,装饰器,在不改变原有方法的情况下,给予方法额外功能
    4. @pytest.mark.skip
    5. def test_min():
    6. values = (2, 3, 1, 4, 6)
    7. val = gloa.min(values)
    8. assert val == 1
    9. def test_max():
    10. values = (2, 3, 1, 4, 6)
    11. val = gloa.max(values)
    12. assert val == 6
    13. if __name__ == '__main__':
    14. pytest.main()

    分模块进行执行可以使用  @pytest.mark.a进行标记

    marking.py

    1. import pytest
    2. @pytest.mark.a
    3. def test_a1():
    4. assert (1) == (1)
    5. @pytest.mark.a
    6. def test_a2():
    7. assert (1, 2) == (1, 2)
    8. @pytest.mark.a
    9. def test_a3():
    10. assert (1, 2, 3) == (1, 2, 3)
    11. @pytest.mark.b
    12. def test_b1():
    13. assert "falcon" == "fal" + "con"
    14. @pytest.mark.b
    15. def test_b2():
    16. assert "falcon" == f"fal{'con'}"

    执行方式如下   pytest -m a  marking.py    指定执行 marking中 a 的模块。

    参数化的方法使用 @pytest.mark.parametrize()进行操作

    parametrized.py

    1. import pytest
    2. from two.gogoabc import gloa
    3. @pytest.mark.parametrize("data, expected", [((2, 3, 1, 4, 6), 1),
    4. ((5, -2, 0, 9, 12), -2), ((200, 100, 0, 300, 400), 0)])
    5. def test_min(data, expected):
    6. val = gloa.min(data)
    7. assert val == expected
    8. @pytest.mark.parametrize("data, expected", [((2, 3, 1, 4, 6), 6),
    9. ((5, -2, 0, 9, 12), 12), ((200, 100, 0, 300, 400), 400),((200, 100, 0, 300, 400,650), 650)])
    10. def test_max(data, expected):
    11. val = gloa.max(data)
    12. assert val == expected

    命令行执行命令: pytest -v -s  .\test_params.py 

    1. rootdir: C:\pyworks\interfaceTest\one
    2. plugins: html-3.2.0, metadata-1.11.0
    3. collected 3 items
    4. test_params.py::test_demo[name1] 登录
    5. 用户名 tom
    6. PASSED退出
    7. test_params.py::test_demo[name2] 登录
    8. 用户名 jack
    9. PASSED退出
    10. test_params.py::test_demo[name3] 登录
    11. 用户名 tony
    12. PASSED退出

  • 相关阅读:
    研究生复试核心竞争力:编程(加分项)
    v-decorator和v-model的使用对比
    CSS总结---持续更新中 2022.8.4
    机器人精确移动包
    java-php-python-ssm在线课堂辅助系统计算机毕业设计
    redo日志、undo日志与事务隔离性
    RC4Drop加密技术:原理、实践与安全性探究
    C++:map和set
    基于若依(SpringBoot前后分离版-vue)的WebSocket消息推送实现
    来入门一下C语言打印Hello World
  • 原文地址:https://blog.csdn.net/davice_li/article/details/127873696