fixture 是pytest 一个很重要的功能, 下面介绍如何调用fixture
目录
利用fixture autouse 功能,自动调用fixture
-
fixture 作为函数参数(最简单的一种方式)
import pytest
@pytest.fixture()
def before():
print '\nbefore each test'
def test_1(before):
print 'test_1()'
class Test1:
def test_3(self, before):
print('test_1()')
def test_4(self, before):
print('test_2()')
上列中, 所有使用参数before 的test function, 都会调用fixture "before"
-
利用fixture autouse 功能,自动调用fixture
import pytest
@pytest.fixture(scope="module", autouse=True)
def before():
print('\nonly once in each test')
def test_1():
print('test_1()')
在scope 定义的范围内,pytest会自动调用fixture "before"
.
-
利用pytest.mark.usefixture()
import pytest
@pytest.fixture()
def before():
print('\nbefore each test')
@pytest.mark.usefixtures("before")
def test_1():
print('test_1()')