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

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

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

附上汇总贴:pytest接口自动化测试框架 | 汇总_COCOgsta的博客-CSDN博客


完整方法如下:

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

参数说明:

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

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

import pytest

# fixture前置条件 function对应 setup 每条用例都会执行一下scope='function'
# 后置条件 autouse 默认是False 手动调用 不要每次都手动调用 自动调用

# fixture前置条件 后置条件 关闭系统
@pytest.fixture(scope='function', autouse=True)
def login():
    print('登录系统')
    yield
    print('退出系统')

class TestCase1:
    def test_03(self):
        print('测试用例三')

    def test_04(self):
        print('测试用例四')


if __name__ == '__main__':
    pytest.main(['-sv', 'py_test1.py'])

运行结果:

/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
Testing started at 上午9:37 ...
Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test1.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3

============================= test session starts ==============================
collecting ... collected 2 items

py_test1.py::TestCase1::test_03 登录系统
PASSED                                   [ 50%]测试用例三
退出系统

py_test1.py::TestCase1::test_04 登录系统
PASSED                                   [100%]测试用例四
退出系统


============================== 2 passed in 0.04s ===============================

Process finished with exit code 0

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

import pytest

# class 一个类中有多个函数 所有函数执行之前要做的前置条件 后置条件 setupclass
@pytest.fixture(scope='class', autouse=True)
def login():
    print('登录系统')
    yield
    print('退出系统')

class TestCase1:
    def test_03(self):
        print('测试用例三')

    def test_04(self):
        print('测试用例四')


if __name__ == '__main__':
    pytest.main(['-sv', 'py_test2.py'])

运行结果

/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
Testing started at 上午9:38 ...
Launching pytest with arguments py_test2.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3

============================= test session starts ==============================
collecting ... collected 2 items

py_test2.py::TestCase1::test_03 登录系统
PASSED                                   [ 50%]测试用例三

py_test2.py::TestCase1::test_04 PASSED                                   [100%]测试用例四
退出系统


============================== 2 passed in 0.03s ===============================

Process finished with exit code 0

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

import pytest

# module 一个py文件中有多个类 所有的类用例执行之前要做的事情 之后要做的事情
@pytest.fixture(scope='module', autouse=True)
def login():
    print('登录系统')
    yield
    print('退出系统')

class TestCase1:
    def test_03(self):
        print('测试用例三')

    def test_04(self):
        print('测试用例四')

class TestCase2:
    def test_05(self):
        print('测试用例五')

    def test_06(self):
        print('测试用例六')


if __name__ == '__main__':
    pytest.main(['-sv', 'py_test3.py'])

运行结果

/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
Testing started at 上午9:42 ...
Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test3.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3

============================= test session starts ==============================
collecting ... collected 4 items

py_test3.py::TestCase1::test_03 登录系统
PASSED                                   [ 25%]测试用例三

py_test3.py::TestCase1::test_04 PASSED                                   [ 50%]测试用例四

py_test3.py::TestCase2::test_05 PASSED                                   [ 75%]测试用例五

py_test3.py::TestCase2::test_06 PASSED                                   [100%]测试用例六
退出系统


============================== 4 passed in 0.06s ===============================

Process finished with exit code 0

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

  1. params:一个可选的参数列表,实现参数化功能
import pytest

# params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来
# 数据从外部取
# params 元组 列表 元组加字典 列表加字典
# params .param固定的写法
@pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'])
def login(request):
    print('登录系统')
    yield request.param
    print('退出系统')

class TestCase1:
    def test_03(self, login):
        print('测试用例三', login)

    def test_04(self):
        print('测试用例四')



if __name__ == '__main__':
    pytest.main(['-sv', 'py_test4.py'])

运行结果:

/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
Testing started at 上午9:43 ...
Launching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3

============================= test session starts ==============================
collecting ... collected 6 items

py_test4.py::TestCase1::test_03[\u8863\u670d] 登录系统
PASSED                     [ 16%]测试用例三 衣服
退出系统

py_test4.py::TestCase1::test_03[\u5305\u5305] 登录系统
PASSED                     [ 33%]测试用例三 包包
退出系统

py_test4.py::TestCase1::test_03[\u978b\u5b50] 登录系统
PASSED                     [ 50%]测试用例三 鞋子
退出系统

py_test4.py::TestCase1::test_04[\u8863\u670d] 登录系统
PASSED                     [ 66%]测试用例四
退出系统

py_test4.py::TestCase1::test_04[\u5305\u5305] 登录系统
PASSED                     [ 83%]测试用例四
退出系统

py_test4.py::TestCase1::test_04[\u978b\u5b50] 登录系统
PASSED                     [100%]测试用例四
退出系统


============================== 6 passed in 0.08s ===============================

Process finished with exit code 0
  1. autouse:默认False,需要调用来激活fixture;如果为True,则所有用例自动调用fixture
  1. ids:用例标识ID,每个ids对应于params,如果没有ids,他们将从params自动生成
import pytest

# params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来
# 数据从外部取
# params 元组 列表 元组加字典 列表加字典
# params .param固定的写法
# ids用例去名字
@pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'], ids=['aa', 'bb', 'cc'])
def login(request):
    print('登录系统')
    yield request.param
    print('退出系统')

class TestCase1:
    def test_03(self, login):
        print('测试用例三', login)

    def test_04(self):
        print('测试用例四')



if __name__ == '__main__':
    pytest.main(['-sv', 'py_test4.py'])

运行结果:

/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
Testing started at 上午9:46 ...
Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test4.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3

============================= test session starts ==============================
collecting ... collected 6 items

py_test4.py::TestCase1::test_03[aa] 登录系统
PASSED                               [ 16%]测试用例三 衣服
退出系统

py_test4.py::TestCase1::test_03[bb] 登录系统
PASSED                               [ 33%]测试用例三 包包
退出系统

py_test4.py::TestCase1::test_03[cc] 登录系统
PASSED                               [ 50%]测试用例三 鞋子
退出系统

py_test4.py::TestCase1::test_04[aa] 登录系统
PASSED                               [ 66%]测试用例四
退出系统

py_test4.py::TestCase1::test_04[bb] 登录系统
PASSED                               [ 83%]测试用例四
退出系统

py_test4.py::TestCase1::test_04[cc] 登录系统
PASSED                               [100%]测试用例四
退出系统


============================== 6 passed in 0.10s ===============================

Process finished with exit code 0
  1. name:fixture的重命名,如果使用了name,那只能将name传入,函数名不再生效
import pytest

# params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来
# 数据从外部取
# params 元组 列表 元组加字典 列表加字典
# params .param固定的写法
# ids用例去名字
# name是给fixture 函数取小名
@pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'], ids=['aa', 'bb', 'cc'], name='l')
def login(request):
    print('登录系统')
    yield request.param
    print('退出系统')

class TestCase1:
    def test_03(self, l):
        print('测试用例三', l)

    def test_04(self):
        print('测试用例四')



if __name__ == '__main__':
    pytest.main(['-sv', 'py_test4.py'])

运行结果:

/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
Testing started at 上午9:47 ...
Launching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3

============================= test session starts ==============================
collecting ... collected 6 items

py_test4.py::TestCase1::test_03[aa] 登录系统
PASSED                               [ 16%]测试用例三 衣服
退出系统

py_test4.py::TestCase1::test_03[bb] 登录系统
PASSED                               [ 33%]测试用例三 包包
退出系统

py_test4.py::TestCase1::test_03[cc] 登录系统
PASSED                               [ 50%]测试用例三 鞋子
退出系统

py_test4.py::TestCase1::test_04[aa] 登录系统
PASSED                               [ 66%]测试用例四
退出系统

py_test4.py::TestCase1::test_04[bb] 登录系统
PASSED                               [ 83%]测试用例四
退出系统

py_test4.py::TestCase1::test_04[cc] 登录系统
PASSED                               [100%]测试用例四
退出系统


============================== 6 passed in 0.06s ===============================

Process finished with exit code 0
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值