pytest---fixture中autouse参数

前言

  在调用fixture的时候,需要传入fixture的名称,如果用例都需要这个fixture,每个用例都进行传入fixture名称,这就比较麻烦了,有什么好的方法?当然fixture中的autouse就是控制传参范围。

autouse

autouse属于fixture参数中的其中一个,默认是为False,不会使作用域的方法全部都进行使用,当设置为True时,作用域范围内的用例都会进行执行fixture内容。

import pytest

@pytest.fixture(autouse=True)
def login():
    print('完成登录')
    yield
    print('退出登录')


class Test_01:
    def test_01(self):
        print('---用例01---')

    def test_02(self):
        print('---用例02---')


if __name__ == '__main__':
    pytest.main(['-vs'])

 

通过执行结果表明:当我们设置autouse参数为True时,默认测试作用域fixture内的测试函数都会全部执行。

混合使用

其实autouse设置为True和其他的fixture进行使用过程中是互不影响的,可以一起使用。

import pytest


@pytest.fixture(autouse=True)
def ti():
    print('每个测试用例都会执行!')


@pytest.fixture()
def login():
    print('\n完成登录')
    yield
    print('\n退出登录')


class Test_01:
    def test_01(self,login):
        print('---用例01---')

    def test_02(self):
        print('---用例02---')


if __name__ == '__main__':
    pytest.main(['-vs'])

 

 

简答的介绍了autouse的参数内容,小伙伴们可以自己动手进行操作下。

 

posted @ 2021-02-01 20:19  测试-安静  阅读(858)  评论(0编辑  收藏  举报