pytest的fixture装载方式
上一篇 /
下一篇 2017-07-06 17:29:16
/ 个人分类:Pytest
两种方法:
1.函数式装载
2.用装饰器
3.设置autos为True
方法1实例:
import pytest
@pytest.fixture()
def before():
num = 10
print ('\nbefore each test')
return num
def test_1(before):
assert before == 10
print ('test_1()')
def test_2(before):
assert before == 1
print ('test_2()')
方法2实例:
conftest.py
import os,pytest,tempfile
@pytest.fixture(scope='class')
def cleandir():
newpath = tempfile.mkdtemp()
os.chdir(newpath)
test_examples.py
import os
import pytest
@pytest.mark.usefixtures("cleandir")
class TestDirectoryInit(object):
def test_cwd_starts_empty(self):
assert os.listdir(os.getcwd()) == []
with open("myfile", "w") as f:
f.write("hello")
def test_cwd_again_starts_empty(self):
assert os.listdir(os.getcwd()) == []
方法3,
fixture decorator一个optional的参数是autouse, 默认设置为False。 当默认为False,就可以选择用上面两种方式来试用fixture
收藏
举报
TAG: