51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 4964|回复: 16
打印 上一主题 下一主题

[讨论] Pytest高级进阶之Fixture

[复制链接]
  • TA的每日心情
    郁闷
    2022-8-29 14:43
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2018-4-18 13:57:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    一. fixture介绍

    fixture是pytest的一个闪光点,pytest要精通怎么能不学习fixture呢?跟着我一起深入学习fixture吧。其实un
    ittest和nose都支持fixture,但是pytest做得更炫。 fixture是pytest特有的功能,它用pytest.fixture标识,定义
    在函数前面。在你编写测试函数的时候,你可以将此函数名称做为传入参数,pytest将会以依赖注入方式,
    将该函数的返回值作为测试函数的传入参数。 fixture有明确的名字,在其他函数,模块,类或整个工程调用
    它时会被激活。 fixture是基于模块来执行的,每个fixture的名字就可以触发一个fixture的函数,它自身也可
    以调用其他的fixture。 我们可以把fixture看做是资源,在你的测试用例执行之前需要去配置这些资源,执行
    完后需要去释放资源。比如module类型的fixture,适合于那些许多测试用例都只需要执行一次的操作。 fixt
    ure还提供了参数化功能,根据配置和不同组件来选择不同的参数。 fixture主要的目的是为了提供一种可靠
    和可重复性的手段去运行那些最基本的测试内容。比如在测试网站的功能时,每个测试用例都要登录和退出,
    利用fixture就可以只做一次,否则每个测试用例都要做这两步也是冗余。

    二. Fixture基础实例入门

    把一个函数定义为Fixture很简单,只能在函数声明之前加上“@pytest.fixture”。其他函数要来调用这个Fixt
    ure,只用把它当做一个输入的参数即可。 test_fixture_basic.py

    复制代码
    1. import pytest

    2. @pytest.fixture()
    3. def before():
    4.     print '\nbefore each test'

    5. def test_1(before):
    6.     print 'test_1()'

    7. def test_2(before):
    8.     print 'test_2()'
    9.     assert 0
    复制代码

    复制代码
    下面是运行结果,test_1和test_2运行之前都调用了before,也就是before执行了两次。默认情况下,fixt
    ure是每个测试用例如果调用了该fixture就会执行一次的。

    复制代码
    1. C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -s test_fixture_basic.py
    2. ============================= test session starts =============================
    3. platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe
    4. cachedir: .cache
    5. metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd
    6. k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}}
    7. rootdir: C:\Users\PycharmProjects\pytest_example, inifile:
    8. plugins: metadata-1.3.0, html-1.14.2
    9. collected 2 items

    10. test_fixture_basic.py::test_1
    11. before each test
    12. test_1()
    13. PASSED
    14. test_fixture_basic.py::test_2
    15. before each test
    16. test_2()
    17. FAILED
    复制代码


    ================================== FAILURES ===================================
    ___________________________________ test_2 ____________________________________

    before = None

        def test_2(before):
            print 'test_2()'
    >       assert 0
    E       assert 0

    test_fixture_basic.py:12: AssertionError
    ===================== 1 failed, 1 passed in 0.23 seconds ======================
    复制代码


    三. 调用fixture的三种方式

    1. 在测试用例中直接调用它,例如第二部分的基础实例。

    2. 用fixture decorator调用fixture

    可以用以下三种不同的方式来写,我只变化了函数名字和类名字,内容没有变。第一种是每个函数前声明
    ,第二种是封装在类里,类里的每个成员函数声明,第三种是封装在类里在前声明。在可以看到3中不同方
    式的运行结果都是一样。 test_fixture_decorator.py

    复制代码
    1. import pytest

    2. @pytest.fixture()
    3. def before():
    4.     print('\nbefore each test')

    5. @pytest.mark.usefixtures("before")
    6. def test_1():
    7.     print('test_1()')

    8. @pytest.mark.usefixtures("before")
    9. def test_2():
    10.     print('test_2()')

    11. class Test1:
    12.     @pytest.mark.usefixtures("before")
    13.     def test_3(self):
    14.         print('test_1()')

    15.     @pytest.mark.usefixtures("before")
    16.     def test_4(self):
    17.         print('test_2()')

    18. @pytest.mark.usefixtures("before")
    19. class Test2:
    20.     def test_5(self):
    21.         print('test_1()')

    22.     def test_6(self):
    23.         print('test_2()')
    复制代码

    复制代码


    3. 用autos调用fixture

    fixture decorator一个optional的参数是autouse, 默认设置为False。 当默认为False,就可以选择用上面两种
    方式来试用fixture。 当设置为True时,在一个session内的所有的test都会自动调用这个fixture。 权限大,
    责任也大,所以用该功能时也要谨慎小心。

    复制代码
    1. import time
    2. import pytest

    3. @pytest.fixture(scope="module", autouse=True)
    4. def mod_header(request):
    5.     print('\n-----------------')
    6.     print('module      : %s' % request.module.__name__)
    7.     print('-----------------')

    8. @pytest.fixture(scope="function", autouse=True)
    9. def func_header(request):
    10.     print('\n-----------------')
    11.     print('function    : %s' % request.function.__name__)
    12.     print('time        : %s' % time.asctime())
    13.     print('-----------------')

    14. def test_one():
    15.     print('in test_one()')

    16. def test_two():
    17.     print('in test_two()')
    复制代码

    复制代码


    四. fixture scope

    function:每个test都运行,默认是function的scope class:每个class的所有test只运行一次 module:每个m
    odule的所有test只运行一次 session:每个session只运行一次

    比如你的所有test都需要连接同一个数据库,那可以设置为module,只需要连接一次数据库,对于module
    内的所有test,这样可以极大的提高运行效率。

    五. fixture 返回值

    在上面的例子中,fixture返回值都是默认None,我们可以选择让fixture返回我们需要的东西。如果你的fixt
    ure需要配置一些数据,读个文件,或者连接一个数据库,那么你可以让fixture返回这些数据或资源。

    如何带参数 fixture还可以带参数,可以把参数赋值给params,默认是None。对于param里面的每个值,f
    ixture都会去调用执行一次,就像执行for循环一样把params里的值遍历一次。 test_fixture_param.py

    1. import pytest

    2. @pytest.fixture(params=[1, 2, 3])
    3. def test_data(request):
    4.     return request.param

    5. def test_not_2(test_data):
    6.     print('test_data: %s' % test_data)
    7.     assert test_data != 2
    复制代码



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情

    2020-2-2 12:43
  • 签到天数: 630 天

    连续签到: 1 天

    [LV.9]测试副司令

    2#
    发表于 2018-4-19 17:42:00 | 只看该作者
    腻害了,多谢分享
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2020-2-2 12:43
  • 签到天数: 630 天

    连续签到: 1 天

    [LV.9]测试副司令

    3#
    发表于 2018-4-19 17:42:06 | 只看该作者
    腻害了,多谢分享
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-20 07:48 , Processed in 0.098716 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表