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

pytest接口自动化测试框架 | @pytest.fixture()装饰器

武飞扬头像
热爱编程的通信人
帮助1

视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》

一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!


完整方法如下:

fixture(scope='function', params=None, autouse=False, ids=None, name=None)

参数说明:

  1. scope参数:标记方法的作用域。有4个可选值:function(默认,函数)、class(类)、module(模块)、package/session(包)

-function:每一个函数或方法都会调用

  1.  
    import pytest
  2.  
     
  3.  
    # fixture前置条件 function对应 setup 每条用例都会执行一下scope='function'
  4.  
    # 后置条件 autouse 默认是False 手动调用 不要每次都手动调用 自动调用
  5.  
     
  6.  
    # fixture前置条件 后置条件 关闭系统
  7.  
    @pytest.fixture(scope='function', autouse=True)
  8.  
    def login():
  9.  
    print('登录系统')
  10.  
    yield
  11.  
    print('退出系统')
  12.  
     
  13.  
    class TestCase1:
  14.  
    def test_03(self):
  15.  
    print('测试用例三')
  16.  
     
  17.  
    def test_04(self):
  18.  
    print('测试用例四')
  19.  
     
  20.  
     
  21.  
    if __name__ == '__main__':
  22.  
    pytest.main(['-sv', 'py_test1.py'])
学新通

运行结果:

  1.  
    /Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test1.py
  2.  
    Testing started at 上午9:37 ...
  3.  
    Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test1.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
  4.  
     
  5.  
    ============================= test session starts ==============================
  6.  
    collecting ... collected 2 items
  7.  
     
  8.  
    py_test1.py::TestCase1::test_03 登录系统
  9.  
    PASSED [ 50%]测试用例三
  10.  
    退出系统
  11.  
     
  12.  
    py_test1.py::TestCase1::test_04 登录系统
  13.  
    PASSED [100%]测试用例四
  14.  
    退出系统
  15.  
     
  16.  
     
  17.  
    ============================== 2 passed in 0.04s ===============================
  18.  
     
  19.  
    Process finished with exit code 0
学新通

-class:每一个类调用一次,一个类中可以有多个方法

  1.  
    import pytest
  2.  
     
  3.  
    # class 一个类中有多个函数 所有函数执行之前要做的前置条件 后置条件 setupclass
  4.  
    @pytest.fixture(scope='class', autouse=True)
  5.  
    def login():
  6.  
    print('登录系统')
  7.  
    yield
  8.  
    print('退出系统')
  9.  
     
  10.  
    class TestCase1:
  11.  
    def test_03(self):
  12.  
    print('测试用例三')
  13.  
     
  14.  
    def test_04(self):
  15.  
    print('测试用例四')
  16.  
     
  17.  
     
  18.  
    if __name__ == '__main__':
  19.  
    pytest.main(['-sv', 'py_test2.py'])
学新通

运行结果

  1.  
    /Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target py_test2.py::TestCase1
  2.  
    Testing started at 上午9:38 ...
  3.  
    Launching pytest with arguments py_test2.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
  4.  
     
  5.  
    ============================= test session starts ==============================
  6.  
    collecting ... collected 2 items
  7.  
     
  8.  
    py_test2.py::TestCase1::test_03 登录系统
  9.  
    PASSED [ 50%]测试用例三
  10.  
     
  11.  
    py_test2.py::TestCase1::test_04 PASSED [100%]测试用例四
  12.  
    退出系统
  13.  
     
  14.  
     
  15.  
    ============================== 2 passed in 0.03s ===============================
  16.  
     
  17.  
    Process finished with exit code 0
学新通

-module:每一个.py文件调用一次,该文件内又有多个function和class

  1.  
    import pytest
  2.  
     
  3.  
    # module 一个py文件中有多个类 所有的类用例执行之前要做的事情 之后要做的事情
  4.  
    @pytest.fixture(scope='module', autouse=True)
  5.  
    def login():
  6.  
    print('登录系统')
  7.  
    yield
  8.  
    print('退出系统')
  9.  
     
  10.  
    class TestCase1:
  11.  
    def test_03(self):
  12.  
    print('测试用例三')
  13.  
     
  14.  
    def test_04(self):
  15.  
    print('测试用例四')
  16.  
     
  17.  
    class TestCase2:
  18.  
    def test_05(self):
  19.  
    print('测试用例五')
  20.  
     
  21.  
    def test_06(self):
  22.  
    print('测试用例六')
  23.  
     
  24.  
     
  25.  
    if __name__ == '__main__':
  26.  
    pytest.main(['-sv', 'py_test3.py'])
学新通

运行结果

  1.  
    /Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test3.py
  2.  
    Testing started at 上午9:42 ...
  3.  
    Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test3.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
  4.  
     
  5.  
    ============================= test session starts ==============================
  6.  
    collecting ... collected 4 items
  7.  
     
  8.  
    py_test3.py::TestCase1::test_03 登录系统
  9.  
    PASSED [ 25%]测试用例三
  10.  
     
  11.  
    py_test3.py::TestCase1::test_04 PASSED [ 50%]测试用例四
  12.  
     
  13.  
    py_test3.py::TestCase2::test_05 PASSED [ 75%]测试用例五
  14.  
     
  15.  
    py_test3.py::TestCase2::test_06 PASSED [100%]测试用例六
  16.  
    退出系统
  17.  
     
  18.  
     
  19.  
    ============================== 4 passed in 0.06s ===============================
  20.  
     
  21.  
    Process finished with exit code 0
学新通

-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

  1. params:一个可选的参数列表,实现参数化功能
  1.  
    import pytest
  2.  
     
  3.  
    # params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来
  4.  
    # 数据从外部取
  5.  
    # params 元组 列表 元组加字典 列表加字典
  6.  
    # params .param固定的写法
  7.  
    @pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'])
  8.  
    def login(request):
  9.  
    print('登录系统')
  10.  
    yield request.param
  11.  
    print('退出系统')
  12.  
     
  13.  
    class TestCase1:
  14.  
    def test_03(self, login):
  15.  
    print('测试用例三', login)
  16.  
     
  17.  
    def test_04(self):
  18.  
    print('测试用例四')
  19.  
     
  20.  
     
  21.  
     
  22.  
    if __name__ == '__main__':
  23.  
    pytest.main(['-sv', 'py_test4.py'])
学新通

运行结果:

  1.  
    /Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target py_test4.py::TestCase1
  2.  
    Testing started at 上午9:43 ...
  3.  
    Launching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
  4.  
     
  5.  
    ============================= test session starts ==============================
  6.  
    collecting ... collected 6 items
  7.  
     
  8.  
    py_test4.py::TestCase1::test_03[\u8863\u670d] 登录系统
  9.  
    PASSED [ 16%]测试用例三 衣服
  10.  
    退出系统
  11.  
     
  12.  
    py_test4.py::TestCase1::test_03[\u5305\u5305] 登录系统
  13.  
    PASSED [ 33%]测试用例三 包包
  14.  
    退出系统
  15.  
     
  16.  
    py_test4.py::TestCase1::test_03[\u978b\u5b50] 登录系统
  17.  
    PASSED [ 50%]测试用例三 鞋子
  18.  
    退出系统
  19.  
     
  20.  
    py_test4.py::TestCase1::test_04[\u8863\u670d] 登录系统
  21.  
    PASSED [ 66%]测试用例四
  22.  
    退出系统
  23.  
     
  24.  
    py_test4.py::TestCase1::test_04[\u5305\u5305] 登录系统
  25.  
    PASSED [ 83%]测试用例四
  26.  
    退出系统
  27.  
     
  28.  
    py_test4.py::TestCase1::test_04[\u978b\u5b50] 登录系统
  29.  
    PASSED [100%]测试用例四
  30.  
    退出系统
  31.  
     
  32.  
     
  33.  
    ============================== 6 passed in 0.08s ===============================
  34.  
     
  35.  
    Process finished with exit code 0
学新通
  1. autouse:默认False,需要调用来激活fixture;如果为True,则所有用例自动调用fixture
  1. ids:用例标识ID,每个ids对应于params,如果没有ids,他们将从params自动生成
  1.  
    import pytest
  2.  
     
  3.  
    # params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来
  4.  
    # 数据从外部取
  5.  
    # params 元组 列表 元组加字典 列表加字典
  6.  
    # params .param固定的写法
  7.  
    # ids用例去名字
  8.  
    @pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'], ids=['aa', 'bb', 'cc'])
  9.  
    def login(request):
  10.  
    print('登录系统')
  11.  
    yield request.param
  12.  
    print('退出系统')
  13.  
     
  14.  
    class TestCase1:
  15.  
    def test_03(self, login):
  16.  
    print('测试用例三', login)
  17.  
     
  18.  
    def test_04(self):
  19.  
    print('测试用例四')
  20.  
     
  21.  
     
  22.  
     
  23.  
    if __name__ == '__main__':
  24.  
    pytest.main(['-sv', 'py_test4.py'])
学新通

运行结果:

  1.  
    /Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test4.py
  2.  
    Testing started at 上午9:46 ...
  3.  
    Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test4.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
  4.  
     
  5.  
    ============================= test session starts ==============================
  6.  
    collecting ... collected 6 items
  7.  
     
  8.  
    py_test4.py::TestCase1::test_03[aa] 登录系统
  9.  
    PASSED [ 16%]测试用例三 衣服
  10.  
    退出系统
  11.  
     
  12.  
    py_test4.py::TestCase1::test_03[bb] 登录系统
  13.  
    PASSED [ 33%]测试用例三 包包
  14.  
    退出系统
  15.  
     
  16.  
    py_test4.py::TestCase1::test_03[cc] 登录系统
  17.  
    PASSED [ 50%]测试用例三 鞋子
  18.  
    退出系统
  19.  
     
  20.  
    py_test4.py::TestCase1::test_04[aa] 登录系统
  21.  
    PASSED [ 66%]测试用例四
  22.  
    退出系统
  23.  
     
  24.  
    py_test4.py::TestCase1::test_04[bb] 登录系统
  25.  
    PASSED [ 83%]测试用例四
  26.  
    退出系统
  27.  
     
  28.  
    py_test4.py::TestCase1::test_04[cc] 登录系统
  29.  
    PASSED [100%]测试用例四
  30.  
    退出系统
  31.  
     
  32.  
     
  33.  
    ============================== 6 passed in 0.10s ===============================
  34.  
     
  35.  
    Process finished with exit code 0
学新通
  1. name:fixture的重命名,如果使用了name,那只能将name传入,函数名不再生效
  1.  
    import pytest
  2.  
     
  3.  
    # params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来
  4.  
    # 数据从外部取
  5.  
    # params 元组 列表 元组加字典 列表加字典
  6.  
    # params .param固定的写法
  7.  
    # ids用例去名字
  8.  
    # name是给fixture 函数取小名
  9.  
    @pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'], ids=['aa', 'bb', 'cc'], name='l')
  10.  
    def login(request):
  11.  
    print('登录系统')
  12.  
    yield request.param
  13.  
    print('退出系统')
  14.  
     
  15.  
    class TestCase1:
  16.  
    def test_03(self, l):
  17.  
    print('测试用例三', l)
  18.  
     
  19.  
    def test_04(self):
  20.  
    print('测试用例四')
  21.  
     
  22.  
     
  23.  
     
  24.  
    if __name__ == '__main__':
  25.  
    pytest.main(['-sv', 'py_test4.py'])
学新通

运行结果:

  1.  
    /Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target py_test4.py::TestCase1
  2.  
    Testing started at 上午9:47 ...
  3.  
    Launching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
  4.  
     
  5.  
    ============================= test session starts ==============================
  6.  
    collecting ... collected 6 items
  7.  
     
  8.  
    py_test4.py::TestCase1::test_03[aa] 登录系统
  9.  
    PASSED [ 16%]测试用例三 衣服
  10.  
    退出系统
  11.  
     
  12.  
    py_test4.py::TestCase1::test_03[bb] 登录系统
  13.  
    PASSED [ 33%]测试用例三 包包
  14.  
    退出系统
  15.  
     
  16.  
    py_test4.py::TestCase1::test_03[cc] 登录系统
  17.  
    PASSED [ 50%]测试用例三 鞋子
  18.  
    退出系统
  19.  
     
  20.  
    py_test4.py::TestCase1::test_04[aa] 登录系统
  21.  
    PASSED [ 66%]测试用例四
  22.  
    退出系统
  23.  
     
  24.  
    py_test4.py::TestCase1::test_04[bb] 登录系统
  25.  
    PASSED [ 83%]测试用例四
  26.  
    退出系统
  27.  
     
  28.  
    py_test4.py::TestCase1::test_04[cc] 登录系统
  29.  
    PASSED [100%]测试用例四
  30.  
    退出系统
  31.  
     
  32.  
     
  33.  
    ============================== 6 passed in 0.06s ===============================
  34.  
     
  35.  
    Process finished with exit code 0
学新通

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

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