2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.config import Conftest
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-05-19 01:06:16 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2012-10-05 20:24:44 +08:00
|
|
|
if "basedir" in metafunc.fixturenames:
|
2010-07-27 03:15:15 +08:00
|
|
|
metafunc.addcall(param="global")
|
2009-05-19 00:59:45 +08:00
|
|
|
metafunc.addcall(param="inpackage")
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def pytest_funcarg__basedir(request):
|
2009-05-19 00:59:45 +08:00
|
|
|
def basedirmaker(request):
|
2013-10-12 21:39:22 +08:00
|
|
|
d = request.getfuncargvalue("tmpdir")
|
2008-08-16 23:26:59 +08:00
|
|
|
d.ensure("adir/conftest.py").write("a=1 ; Directory = 3")
|
|
|
|
d.ensure("adir/b/conftest.py").write("b=2 ; a = 1.5")
|
2010-07-27 03:15:15 +08:00
|
|
|
if request.param == "inpackage":
|
2009-05-19 00:59:45 +08:00
|
|
|
d.ensure("adir/__init__.py")
|
|
|
|
d.ensure("adir/b/__init__.py")
|
2010-07-27 03:15:15 +08:00
|
|
|
return d
|
2012-06-27 03:56:03 +08:00
|
|
|
return request.cached_setup(
|
|
|
|
lambda: basedirmaker(request), extrakey=request.param)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-12-29 00:49:46 +08:00
|
|
|
def ConftestWithSetinitial(path):
|
|
|
|
conftest = Conftest()
|
|
|
|
conftest.setinitial([path])
|
|
|
|
return conftest
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
class TestConftestValueAccessGlobal:
|
|
|
|
def test_basic_init(self, basedir):
|
2008-08-16 23:26:59 +08:00
|
|
|
conftest = Conftest()
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.setinitial([basedir.join("adir")])
|
2008-08-16 23:26:59 +08:00
|
|
|
assert conftest.rget("a") == 1
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_onimport(self, basedir):
|
2009-02-27 18:18:27 +08:00
|
|
|
l = []
|
|
|
|
conftest = Conftest(onimport=l.append)
|
2010-07-27 03:15:15 +08:00
|
|
|
conftest.setinitial([basedir.join("adir"),
|
2010-06-08 03:02:26 +08:00
|
|
|
'--confcutdir=%s' % basedir])
|
2010-10-04 22:19:01 +08:00
|
|
|
assert len(l) == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
assert conftest.rget("a") == 1
|
2009-05-19 00:59:45 +08:00
|
|
|
assert conftest.rget("b", basedir.join("adir", "b")) == 2
|
2009-12-29 17:26:51 +08:00
|
|
|
assert len(l) == 2
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_immediate_initialiation_and_incremental_are_the_same(self, basedir):
|
2008-08-16 23:26:59 +08:00
|
|
|
conftest = Conftest()
|
2013-10-12 21:39:22 +08:00
|
|
|
len(conftest._path2confmods)
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.getconftestmodules(basedir)
|
2008-08-16 23:26:59 +08:00
|
|
|
snap1 = len(conftest._path2confmods)
|
|
|
|
#assert len(conftest._path2confmods) == snap1 + 1
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.getconftestmodules(basedir.join('adir'))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(conftest._path2confmods) == snap1 + 1
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.getconftestmodules(basedir.join('b'))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(conftest._path2confmods) == snap1 + 2
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_default_has_lower_prio(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir.join("adir"))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert conftest.rget('Directory') == 3
|
2010-11-13 16:05:11 +08:00
|
|
|
#assert conftest.lget('Directory') == pytest.Directory
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_not_existing(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir)
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(KeyError, lambda: conftest.rget('a'))
|
2010-11-18 05:12:16 +08:00
|
|
|
#pytest.raises(KeyError, "conftest.lget('a')")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_by_path(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir)
|
2009-05-19 00:59:45 +08:00
|
|
|
assert conftest.rget("a", basedir.join('adir')) == 1
|
|
|
|
#assert conftest.lget("a", basedir.join('adir')) == 1
|
2010-07-27 03:15:15 +08:00
|
|
|
assert conftest.rget("a", basedir.join('adir', 'b')) == 1.5
|
2009-05-19 00:59:45 +08:00
|
|
|
#assert conftest.lget("a", basedir.join('adir', 'b')) == 1
|
|
|
|
#assert conftest.lget("b", basedir.join('adir', 'b')) == 2
|
2010-11-18 05:12:16 +08:00
|
|
|
#assert pytest.raises(KeyError,
|
2009-05-19 00:59:45 +08:00
|
|
|
# 'conftest.lget("b", basedir.join("a"))'
|
2008-08-16 23:26:59 +08:00
|
|
|
#)
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_with_init_one_conftest(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir.join('adir'))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert conftest.rget("a") == 1
|
|
|
|
#assert conftest.lget("a") == 1
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_with_init_two_conftests(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir.join("adir", "b"))
|
2008-08-16 23:26:59 +08:00
|
|
|
conftest.rget("a") == 1.5
|
|
|
|
#conftest.lget("a") == 1
|
|
|
|
#conftest.lget("b") == 1
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_with_confmod(self, basedir):
|
2010-09-15 16:30:50 +08:00
|
|
|
startdir = basedir.join("adir", "b")
|
|
|
|
startdir.ensure("xx", dir=True)
|
|
|
|
conftest = ConftestWithSetinitial(startdir)
|
|
|
|
mod, value = conftest.rget_with_confmod("a", startdir)
|
2008-08-16 23:26:59 +08:00
|
|
|
assert value == 1.5
|
|
|
|
path = py.path.local(mod.__file__)
|
2009-05-19 00:59:45 +08:00
|
|
|
assert path.dirpath() == basedir.join("adir", "b")
|
2011-09-21 14:12:37 +08:00
|
|
|
assert path.purebasename.startswith("conftest")
|
2009-12-31 22:10:32 +08:00
|
|
|
|
2010-04-27 21:49:13 +08:00
|
|
|
def test_conftest_in_nonpkg_with_init(tmpdir):
|
|
|
|
tmpdir.ensure("adir-1.0/conftest.py").write("a=1 ; Directory = 3")
|
|
|
|
tmpdir.ensure("adir-1.0/b/conftest.py").write("b=2 ; a = 1.5")
|
|
|
|
tmpdir.ensure("adir-1.0/b/__init__.py")
|
|
|
|
tmpdir.ensure("adir-1.0/__init__.py")
|
2013-10-12 21:39:22 +08:00
|
|
|
ConftestWithSetinitial(tmpdir.join("adir-1.0", "b"))
|
2010-04-27 21:49:13 +08:00
|
|
|
|
2010-07-02 01:27:40 +08:00
|
|
|
def test_doubledash_not_considered(testdir):
|
|
|
|
conf = testdir.mkdir("--option")
|
|
|
|
conf.join("conftest.py").ensure()
|
|
|
|
conftest = Conftest()
|
|
|
|
conftest.setinitial([conf.basename, conf.basename])
|
|
|
|
l = conftest.getconftestmodules(None)
|
|
|
|
assert len(l) == 0
|
|
|
|
|
2012-06-27 03:56:03 +08:00
|
|
|
def test_issue151_load_all_conftests(testdir):
|
|
|
|
names = "code proj src".split()
|
|
|
|
for name in names:
|
|
|
|
p = testdir.mkdir(name)
|
|
|
|
p.ensure("conftest.py")
|
|
|
|
|
|
|
|
conftest = Conftest()
|
|
|
|
conftest.setinitial(names)
|
|
|
|
d = list(conftest._conftestpath2mod.values())
|
|
|
|
assert len(d) == len(names)
|
|
|
|
|
2010-10-14 00:45:07 +08:00
|
|
|
def test_conftest_global_import(testdir):
|
|
|
|
testdir.makeconftest("x=3")
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.config import Conftest
|
2010-10-14 00:45:07 +08:00
|
|
|
conf = Conftest()
|
|
|
|
mod = conf.importconftest(py.path.local("conftest.py"))
|
|
|
|
assert mod.x == 3
|
|
|
|
import conftest
|
|
|
|
assert conftest is mod, (conftest, mod)
|
|
|
|
subconf = py.path.local().ensure("sub", "conftest.py")
|
|
|
|
subconf.write("y=4")
|
|
|
|
mod2 = conf.importconftest(subconf)
|
|
|
|
assert mod != mod2
|
|
|
|
assert mod2.y == 4
|
|
|
|
import conftest
|
|
|
|
assert conftest is mod2, (conftest, mod)
|
|
|
|
""")
|
|
|
|
res = testdir.runpython(p)
|
|
|
|
assert res.ret == 0
|
|
|
|
|
2009-12-31 22:10:32 +08:00
|
|
|
def test_conftestcutdir(testdir):
|
|
|
|
conf = testdir.makeconftest("")
|
|
|
|
p = testdir.mkdir("x")
|
|
|
|
conftest = Conftest(confcutdir=p)
|
|
|
|
conftest.setinitial([testdir.tmpdir])
|
|
|
|
l = conftest.getconftestmodules(p)
|
|
|
|
assert len(l) == 0
|
|
|
|
l = conftest.getconftestmodules(conf.dirpath())
|
|
|
|
assert len(l) == 0
|
|
|
|
assert conf not in conftest._conftestpath2mod
|
2010-07-27 03:15:15 +08:00
|
|
|
# but we can still import a conftest directly
|
2009-12-31 22:10:32 +08:00
|
|
|
conftest.importconftest(conf)
|
|
|
|
l = conftest.getconftestmodules(conf.dirpath())
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
# and all sub paths get updated properly
|
|
|
|
l = conftest.getconftestmodules(p)
|
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
|
|
|
|
def test_conftestcutdir_inplace_considered(testdir):
|
|
|
|
conf = testdir.makeconftest("")
|
|
|
|
conftest = Conftest(confcutdir=conf.dirpath())
|
|
|
|
conftest.setinitial([conf.dirpath()])
|
|
|
|
l = conftest.getconftestmodules(conf.dirpath())
|
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
|
|
|
|
def test_setinitial_confcut(testdir):
|
|
|
|
conf = testdir.makeconftest("")
|
|
|
|
sub = testdir.mkdir("sub")
|
|
|
|
sub.chdir()
|
|
|
|
for opts in (["--confcutdir=%s" % sub, sub],
|
|
|
|
[sub, "--confcutdir=%s" % sub],
|
|
|
|
["--confcutdir=.", sub],
|
|
|
|
[sub, "--confcutdir", sub],
|
|
|
|
[str(sub), "--confcutdir", "."],
|
|
|
|
):
|
|
|
|
conftest = Conftest()
|
|
|
|
conftest.setinitial(opts)
|
|
|
|
assert conftest._confcutdir == sub
|
|
|
|
assert conftest.getconftestmodules(sub) == []
|
|
|
|
assert conftest.getconftestmodules(conf.dirpath()) == []
|
2010-02-04 19:26:53 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.multi(name='test tests whatever .dotdir'.split())
|
2010-02-07 05:37:04 +08:00
|
|
|
def test_setinitial_conftest_subdirs(testdir, name):
|
2010-02-04 19:26:53 +08:00
|
|
|
sub = testdir.mkdir(name)
|
|
|
|
subconftest = sub.ensure("conftest.py")
|
|
|
|
conftest = Conftest()
|
2010-06-08 03:02:26 +08:00
|
|
|
conftest.setinitial([sub.dirpath(), '--confcutdir=%s' % testdir.tmpdir])
|
2010-10-04 22:19:01 +08:00
|
|
|
if name not in ('whatever', '.dotdir'):
|
2010-02-07 05:37:04 +08:00
|
|
|
assert subconftest in conftest._conftestpath2mod
|
|
|
|
assert len(conftest._conftestpath2mod) == 1
|
|
|
|
else:
|
|
|
|
assert subconftest not in conftest._conftestpath2mod
|
|
|
|
assert len(conftest._conftestpath2mod) == 0
|
2010-10-10 19:48:48 +08:00
|
|
|
|
|
|
|
def test_conftest_confcutdir(testdir):
|
|
|
|
testdir.makeconftest("assert 0")
|
|
|
|
x = testdir.mkdir("x")
|
|
|
|
x.join("conftest.py").write(py.code.Source("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", action="store_true")
|
|
|
|
"""))
|
|
|
|
result = testdir.runpytest("-h", "--confcutdir=%s" % x, x)
|
|
|
|
result.stdout.fnmatch_lines(["*--xyz*"])
|
2013-04-18 19:24:53 +08:00
|
|
|
|
|
|
|
def test_conftest_import_order(testdir, monkeypatch):
|
|
|
|
ct1 = testdir.makeconftest("")
|
|
|
|
sub = testdir.mkdir("sub")
|
|
|
|
ct2 = sub.join("conftest.py")
|
|
|
|
ct2.write("")
|
|
|
|
def impct(p):
|
|
|
|
return p
|
|
|
|
conftest = Conftest()
|
|
|
|
monkeypatch.setattr(conftest, 'importconftest', impct)
|
|
|
|
assert conftest.getconftestmodules(sub) == [ct1, ct2]
|
2013-11-12 20:45:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_fixture_dependency(testdir, monkeypatch):
|
|
|
|
ct1 = testdir.makeconftest("")
|
|
|
|
ct1 = testdir.makepyfile("__init__.py")
|
|
|
|
ct1.write("")
|
|
|
|
sub = testdir.mkdir("sub")
|
|
|
|
sub.join("__init__.py").write("")
|
|
|
|
sub.join("conftest.py").write(py.std.textwrap.dedent("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def not_needed():
|
|
|
|
assert False, "Should not be called!"
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
|
|
|
assert False, "Should not be called!"
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bar(foo):
|
|
|
|
return 'bar'
|
|
|
|
"""))
|
|
|
|
subsub = sub.mkdir("subsub")
|
|
|
|
subsub.join("__init__.py").write("")
|
|
|
|
subsub.join("test_bar.py").write(py.std.textwrap.dedent("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bar():
|
|
|
|
return 'sub bar'
|
|
|
|
|
|
|
|
def test_event_fixture(bar):
|
|
|
|
assert bar == 'sub bar'
|
|
|
|
"""))
|
|
|
|
result = testdir.runpytest("sub")
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|