實例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的配置信息,會自動加載引用,只需要在函數中直接引用即可.