pytest:fixture之scope作用域

scope有4个作用范围:function、class、module、session

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

class:每个类只调用1次

module:每个模块只调用1次

session:多个模块调用1次,通常写在conftest中

scope=function

import pytest

"""
scope=function
执行顺序:setup_module ->fixture->test ->fixture->test ..
"""

@pytest.fixture() 
def login():
    print('登陆')

@pytest.fixture(scope='function')
def logout():
    print('登出')

def setup_module():
    print(f'setup')

def test_s1(login):
    print('s1')

def test_s2(logout):
    print('s2')

def test_s3():
    print('s3')

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

运行结果:

collecting ... collected 3 items

s001_function.py::test_s1 setup
登陆
PASSED                                         [ 33%]s1

s001_function.py::test_s2 登出
PASSED                                         [ 66%]s2

s001_function.py::test_s3 PASSED                                         [100%]s3

scope=function使用在类中

import pytest

"""
scope=fuction
运行顺序:setup_method -> fixture -> test -> setup_method -> fixture -> test
"""

@pytest.fixture()
def login():
    print('登陆')

@pytest.fixture(scope='function')
def logout():
    print('登出')

class Test1:
    def setup_method(self):
        print('setup_method')

    def test_s1(self,login):
        print('s1')

    def test_s2(self,logout):
        print('s2')

if __name__ == '__main__':
    pytest.main(['-q','s002_function_运用在class中.py'])

运行结果:

collecting ... collected 2 items

s002_function_运用在class中.py::Test1::test_s1 setup_method
登陆
PASSED                    [ 50%]s1

s002_function_运用在class中.py::Test1::test_s2 setup_method
登出
PASSED                    [100%]s2


============================== 2 passed in 0.20s ==============================

scope=class

import pytest

"""
scope=class
1、若class中的每个test用例都调用了login,只在class所有用例执行前运行一次
2、运行顺序
    setup_class -> fixture函数(整个类的运行过程中只运行一次,测试用例执行前)-> 测试用例
"""

@pytest.fixture(scope='class')
def login():
    print('登陆')

class Test2:
    def setup_class(self):
        print('setup')

    def test_s1(self,login):
        print('s1')

    def test_s2(self,login):
        print('s2')

    def test_s3(self):
        print('s3')

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

运行结果:

collecting ... collected 3 items

s003_class.py::Test2::test_s1 setup
登陆
PASSED                                     [ 33%]s1

s003_class.py::Test2::test_s2 PASSED                                     [ 66%]s2

s003_class.py::Test2::test_s3 PASSED                                     [100%]s3


============================== 3 passed in 0.22s ==============================

scope=module

import pytest
"""
scope=module:整个.py模块中login只执行一次
执行顺序:setup_module -> fixture -> tests
"""

@pytest.fixture(scope='module')
def login():
    print('登陆')

def setup_module():
    print('setup_module')

def test_s1(login):
    print('s1')

class Test1:
    def test_s2(self,login):
        print('s2')

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

运行结果:

collecting ... collected 2 items

s004_module.py::test_s1 setup_module
登陆
PASSED                                           [ 50%]s1

s004_module.py::Test1::test_s2 PASSED                                    [100%]s2


============================== 2 passed in 0.19s ==============================

scope=session

scope=session,可以跨多个.py文件,若多个py模块中的用例都调用了 fixture,只会运行一次(在调用fixture的用例开始时运行一次)

fixture(scope='session') 函数写在 conftest.py 文件中

conftest.py 文件文件名固定,放在工程根目录则是全局使用,放在某个package下,则只针对该package下的py文件有效

  conftest.py

import pytest

@pytest.fixture(scope='session')
def gblogin():
    print('gb登陆--session')

如上,test_001 和 test_005中分别调用了gblogin

test_001

import pytest


@pytest.fixture()
def login():
    print('登陆')

@pytest.fixture(scope='function')
def logout():
    print('登出')

def setup_module():
    print(f'setup')

def test_s1(login):
    print('s1')

def test_s2(logout):
    print('s2')

def test_s3(gblogin):
    print('s3')

test_005

import pytest

def test1(gblogin):
    print('s1')

执行所有用例:

E:\f004_fixtrue\f02_scope>pytest -s

运行结果:

collected 11 items

test_001_function.py setup
登陆
s1
.登出
s2
.gb登陆--session
s3
.
test_002_function_运用在class中.py setup_method
登陆
s1
.setup_method
登出
s2
.
test_003_class.py setup
登陆
s1
.s2
.s3
.
test_004_module.py setup_module
登陆
s1
.s2
.
test_005_session.py s1
.

================================================= 11 passed in 0.53s ==================================================

猜你喜欢

转载自blog.csdn.net/weixin_44006041/article/details/107748631
今日推荐