pytest固件功能測試

實例1 

conftest.py

@pytest.fixture()
def fun1():
    print ('run func1')
    yield
    print ('func1 done')

@pytest.fixture()
def fun2():
    print ('run func2')
    yield
    print ('func2 done')

test_db.py

def test01(fun1):
    print ('run test01')

def test02(fun1):
    print ('run test02')

執行:

 

實例2

conftest不變

test_db.py

def test01(fun1):
    print ('run test01')

def test02(fun1):
    print ('run test02')

實例3:

修改fun1的scope爲'module'

@pytest.fixture(scope="module")
def fun1():
    print ('run func1')
    yield
    print ('func1 done')

@pytest.fixture()
def fun2():
    print ('run func2')
    yield
    print ('func2 done')

def test01(fun1):
    print ('run test01')

def test02(fun1):
    print ('run test02')

以上可以看出,估計的scope默認爲function,即每個函數執行的時候,默認調用一次.當將固件的scope變爲module的時候,則只會在文件調用的時候作用一次.且conftest的配置信息,會自動加載引用,只需要在函數中直接引用即可.

發佈了106 篇原創文章 · 獲贊 22 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章