conftest特点

1、conftest.py 文件名称固定,不能修改
2、conftest.py 文件放在工程根目录则是全局目录调用,放在某个package下,则只在当前package下有效。1个工程可有多个conftest.py;package中必须要有__init__.py文件
3、conftest.py 的调用不需要import,pytest用例会自动识别该文件
4、conftest.py 不可被其他模块import
5、conftest.py 需结合fixture使用

conftest应用场景

1、共用的配置信息
2、共用的token
3、共用的测试场景

conftest.py 中fixture作用域

在这里插入图片描述
conftest.py

import pytest

@pytest.fixture(scope='session')
def login():
    token = 'dwyqdgkasd'
    print('session --> 登陆')
    return token

@pytest.fixture(scope='function')
def fdata():
    name = 'function'
    print('function --> fdata')
    return name

@pytest.fixture(scope='class')
def cdata():
    name = 'class'
    print('class --> cdata')
    return name

fixture(scope=‘session’) --> fixture只执行1次

test_01.py

def test01(login):
    token = login
    print(f'test_01 -->01 --> {token}')

def test02(login):
    token = login
    print(f'test_01 -->02 --> {token}')

test_02.py

class Test1:
    def test01(self,login):
        print(f'test_02 --> 01 --> {login}')

运行test_01.py 和 test_02.py,运行结果:

E:\study\python\study\BasicKnowledgePoints\s5_frame\f005_conftest>pytest -s test_01.py test_02.py
================================================= test session starts ================================================= platform win32 -- Python 3.6.6rc1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\study\python\study\BasicKnowledgePoints\s5_frame\f005_conftest
plugins: forked-1.3.0, html-2.1.1, metadata-1.9.0, repeat-0.8.0, xdist-1.34.0
collected 3 items

test_01.py session --> 登陆
test_01 -->01 --> dwyqdgkasd
.test_01 -->02 --> dwyqdgkasd
.
test_02.py test_02 --> 01 --> dwyqdgkasd
.

================================================== 3 passed in 0.14s ==================================================

1、conftest.py 中的login函数在当前package下全局可调用
2、login作用域是session,login虽被多个函数调用,但login函数只会执行1次,但他的返回值token可被调用的函数使用

fixture(scope=‘function’) --> fixture只执行1次

test_03.py

def test_01(fdata):
    print(f'test_03 --> 01 --> {fdata}')

class Test03:
    def test_03(self,fdata):
        print(f'test_03 --> Test03 -->03 --> {fdata}')

test_04.py

def test01(fdata):
    print(f'test_04 --> 01 --> {fdata}')

运行test_03.py 和 test_04.py,运行结果:

E:\study\python\study\BasicKnowledgePoints\s5_frame\f005_conftest>pytest -s test_03.py test_04.py
================================================= test session starts ================================================= platform win32 -- Python 3.6.6rc1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\study\python\study\BasicKnowledgePoints\s5_frame\f005_conftest
plugins: forked-1.3.0, html-2.1.1, metadata-1.9.0, repeat-0.8.0, xdist-1.34.0
collected 3 items

test_03.py function --> fdata
test_03 --> 01 --> function
.function --> fdata
test_03 --> Test03 -->03 --> function
.
test_04.py function --> fdata
test_04 --> 01 --> function
.

================================================== 3 passed in 0.21s ==================================================

1、conftest.py 中的fdata函数在当前package下全局可调用
2、fdata作用域是function,fdata被函数调用几次,则执行几次

fixture(scope=‘class’) --> fixture在每个调用它的class中只执行1次

test_05.py

class Test05:
    def test01(self,cdata):
        print(f'test_05 --> Test05 -->01 --> {cdata}')

    def test02(self,cdata):
        print(f'test_05 --> Test05 -->02 --> {cdata}')

test_06.py

class Test06:
    def test01(self, cdata):
        print(f'test_06 --> Test06 -->01 --> {cdata}')

    def test02(self, cdata):
        print(f'test_06 --> Test06 -->02 --> {cdata}')

运行test_05.py 和 test_06.py,运行结果:

E:\study\python\study\BasicKnowledgePoints\s5_frame\f005_conftest>pytest -s test_05.py test_06.py
================================================= test session starts ================================================= platform win32 -- Python 3.6.6rc1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\study\python\study\BasicKnowledgePoints\s5_frame\f005_conftest
plugins: forked-1.3.0, html-2.1.1, metadata-1.9.0, repeat-0.8.0, xdist-1.34.0
collected 4 items

test_05.py class --> cdata
test_05 --> Test05 -->01 --> class
.test_05 --> Test05 -->02 --> class
.
test_06.py class --> cdata
test_06 --> Test06 -->01 --> class
.test_06 --> Test06 -->02 --> class
.

================================================== 4 passed in 0.28s ==================================================

1、conftest.py 中的cdata函数在当前package下全局可调用
2、cdata作用域是class,cdata只在每个类调用它的时候运行1次

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐