zoukankan      html  css  js  c++  java
  • pytest

    @fixture("scope"=" ",autouse=True)

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

    二. Fixture基础实例入门

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

    复制代码
    import pytest
    
    @pytest.fixture()
    def before():
        print '
    before each test'
    
    def test_1(before):
        print 'test_1()'
    
    def test_2(before):
        print 'test_2()'
        assert 0
    复制代码

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

    fixture的scope参数

    scope参数有四种,分别是'function','module','class','session',默认为function。

    • function:每个test都运行,默认是function的scope
    • class:每个class的所有test只运行一次
    • module:每个module的所有test只运行一次
    • session:每个session只运行一次
    # -*- coding:utf-8 -*-
    import pytest
    
    @pytest.fixture(scope='function')
    def setup_function(request):
        def teardown_function():
            print("teardown_function called.")
        request.addfinalizer(teardown_function)  # 此内嵌函数做teardown工作
        print('setup_function called.')
    
    @pytest.fixture(scope='module')
    def setup_module(request):
        def teardown_module():
            print("teardown_module called.")
        request.addfinalizer(teardown_module)
        print('setup_module called.')
    
    @pytest.mark.website
    def test_1(setup_function):
        print('Test_1 called.')
    
    def test_2(setup_module):
        print('Test_2 called.')
    
    def test_3(setup_module):
        print('Test_3 called.')
        assert 2==1+1              # 通过assert断言确认测试结果是否符合预期

    fixture的autouse参数

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

    复制代码
    import time
    import pytest
    
    @pytest.fixture(scope="module", autouse=True)
    def mod_header(request):
        print('
    -----------------')
        print('module      : %s' % request.module.__name__)
        print('-----------------')
    
    @pytest.fixture(scope="function", autouse=True)
    def func_header(request):
        print('
    -----------------')
        print('function    : %s' % request.function.__name__)
        print('time        : %s' % time.asctime())
        print('-----------------')
    
    def test_one():
        print('in test_one()')
    
    def test_two():
        print('in test_two()')

    @pytest.mark.parametrize("a", [])

    用来传入多个参数

  • 相关阅读:
    chrome、firefox、IE中input输入光标位置错位解决方案
    IE兼容opacity
    Linux下redis5的安装及伪分布式集群搭建
    Linux文件编辑命令详细整理
    saltstack
    redis集群
    系统调优
    centos7 telnet 登录升级ssh
    keepalive简单高可用 ha
    docker
  • 原文地址:https://www.cnblogs.com/liusouthern/p/10937243.html
Copyright © 2011-2022 走看看