2015-02-27 04:56:44 +08:00
|
|
|
from textwrap import dedent
|
2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2015-04-22 20:15:42 +08:00
|
|
|
from _pytest.config import PytestPluginManager
|
2009-05-19 00:59:45 +08:00
|
|
|
|
2015-02-27 04:56:44 +08:00
|
|
|
|
2014-04-03 04:30:45 +08:00
|
|
|
@pytest.fixture(scope="module", params=["global", "inpackage"])
|
2015-07-16 07:03:58 +08:00
|
|
|
def basedir(request, tmpdir_factory):
|
2014-04-03 04:30:45 +08:00
|
|
|
from _pytest.tmpdir import tmpdir
|
2015-07-16 07:03:58 +08:00
|
|
|
tmpdir = tmpdir(request, tmpdir_factory)
|
2014-04-03 04:30:45 +08:00
|
|
|
tmpdir.ensure("adir/conftest.py").write("a=1 ; Directory = 3")
|
|
|
|
tmpdir.ensure("adir/b/conftest.py").write("b=2 ; a = 1.5")
|
|
|
|
if request.param == "inpackage":
|
|
|
|
tmpdir.ensure("adir/__init__.py")
|
|
|
|
tmpdir.ensure("adir/b/__init__.py")
|
|
|
|
return tmpdir
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-12-29 00:49:46 +08:00
|
|
|
def ConftestWithSetinitial(path):
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-02 17:29:23 +08:00
|
|
|
conftest_setinitial(conftest, [path])
|
2009-12-29 00:49:46 +08:00
|
|
|
return conftest
|
|
|
|
|
2014-04-02 17:29:23 +08:00
|
|
|
def conftest_setinitial(conftest, args, confcutdir=None):
|
|
|
|
class Namespace:
|
|
|
|
def __init__(self):
|
|
|
|
self.file_or_dir = args
|
|
|
|
self.confcutdir = str(confcutdir)
|
2015-06-23 13:53:32 +08:00
|
|
|
self.noconftest = False
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest._set_initial_conftests(Namespace())
|
2014-04-02 17:29:23 +08:00
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
class TestConftestValueAccessGlobal:
|
|
|
|
def test_basic_init(self, basedir):
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-03 04:30:45 +08:00
|
|
|
p = basedir.join("adir")
|
2015-04-22 20:15:42 +08:00
|
|
|
assert conftest._rget_with_confmod("a", p)[1] == 1
|
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):
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2013-10-12 21:39:22 +08:00
|
|
|
len(conftest._path2confmods)
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest._getconftestmodules(basedir)
|
2008-08-16 23:26:59 +08:00
|
|
|
snap1 = len(conftest._path2confmods)
|
|
|
|
#assert len(conftest._path2confmods) == snap1 + 1
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest._getconftestmodules(basedir.join('adir'))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(conftest._path2confmods) == snap1 + 1
|
2015-04-22 20:15:42 +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_value_access_not_existing(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir)
|
2014-04-03 04:30:45 +08:00
|
|
|
with pytest.raises(KeyError):
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest._rget_with_confmod('a', basedir)
|
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)
|
2014-04-03 04:30:45 +08:00
|
|
|
adir = basedir.join("adir")
|
2015-04-22 20:15:42 +08:00
|
|
|
assert conftest._rget_with_confmod("a", adir)[1] == 1
|
|
|
|
assert conftest._rget_with_confmod("a", adir.join("b"))[1] == 1.5
|
2008-08-16 23:26:59 +08:00
|
|
|
|
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)
|
2015-04-22 20:15:42 +08:00
|
|
|
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
|
|
|
|
2014-04-02 17:29:23 +08:00
|
|
|
def test_doubledash_considered(testdir):
|
2010-07-02 01:27:40 +08:00
|
|
|
conf = testdir.mkdir("--option")
|
|
|
|
conf.join("conftest.py").ensure()
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-02 17:29:23 +08:00
|
|
|
conftest_setinitial(conftest, [conf.basename, conf.basename])
|
2015-04-22 20:15:42 +08:00
|
|
|
l = conftest._getconftestmodules(conf)
|
2014-04-02 17:29:23 +08:00
|
|
|
assert len(l) == 1
|
2010-07-02 01:27:40 +08:00
|
|
|
|
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")
|
|
|
|
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-02 17:29:23 +08:00
|
|
|
conftest_setinitial(conftest, names)
|
2012-06-27 03:56:03 +08:00
|
|
|
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
|
2015-04-22 20:15:42 +08:00
|
|
|
from _pytest.config import PytestPluginManager
|
|
|
|
conf = PytestPluginManager()
|
|
|
|
mod = conf._importconftest(py.path.local("conftest.py"))
|
2010-10-14 00:45:07 +08:00
|
|
|
assert mod.x == 3
|
|
|
|
import conftest
|
|
|
|
assert conftest is mod, (conftest, mod)
|
|
|
|
subconf = py.path.local().ensure("sub", "conftest.py")
|
|
|
|
subconf.write("y=4")
|
2015-04-22 20:15:42 +08:00
|
|
|
mod2 = conf._importconftest(subconf)
|
2010-10-14 00:45:07 +08:00
|
|
|
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")
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-02 17:29:23 +08:00
|
|
|
conftest_setinitial(conftest, [testdir.tmpdir], confcutdir=p)
|
2015-04-22 20:15:42 +08:00
|
|
|
l = conftest._getconftestmodules(p)
|
2009-12-31 22:10:32 +08:00
|
|
|
assert len(l) == 0
|
2015-04-22 20:15:42 +08:00
|
|
|
l = conftest._getconftestmodules(conf.dirpath())
|
2009-12-31 22:10:32 +08:00
|
|
|
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
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest._importconftest(conf)
|
|
|
|
l = conftest._getconftestmodules(conf.dirpath())
|
2009-12-31 22:10:32 +08:00
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
# and all sub paths get updated properly
|
2015-04-22 20:15:42 +08:00
|
|
|
l = conftest._getconftestmodules(p)
|
2009-12-31 22:10:32 +08:00
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
|
|
|
|
def test_conftestcutdir_inplace_considered(testdir):
|
|
|
|
conf = testdir.makeconftest("")
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-02 17:29:23 +08:00
|
|
|
conftest_setinitial(conftest, [conf.dirpath()], confcutdir=conf.dirpath())
|
2015-04-22 20:15:42 +08:00
|
|
|
l = conftest._getconftestmodules(conf.dirpath())
|
2009-12-31 22:10:32 +08:00
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("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")
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-02 17:29:23 +08:00
|
|
|
conftest_setinitial(conftest, [sub.dirpath()], confcutdir=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*"])
|
2015-06-23 15:56:25 +08:00
|
|
|
assert 'warning: could not load initial' not in result.stdout.str()
|
2013-04-18 19:24:53 +08:00
|
|
|
|
2015-06-23 13:53:32 +08:00
|
|
|
def test_no_conftest(testdir):
|
|
|
|
testdir.makeconftest("assert 0")
|
|
|
|
result = testdir.runpytest("--noconftest")
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2014-04-02 17:29:23 +08:00
|
|
|
def test_conftest_existing_resultlog(testdir):
|
|
|
|
x = testdir.mkdir("tests")
|
|
|
|
x.join("conftest.py").write(py.code.Source("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", action="store_true")
|
|
|
|
"""))
|
|
|
|
testdir.makefile(ext=".log", result="") # Writes result.log
|
|
|
|
result = testdir.runpytest("-h", "--resultlog", "result.log")
|
|
|
|
result.stdout.fnmatch_lines(["*--xyz*"])
|
|
|
|
|
|
|
|
def test_conftest_existing_junitxml(testdir):
|
|
|
|
x = testdir.mkdir("tests")
|
|
|
|
x.join("conftest.py").write(py.code.Source("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", action="store_true")
|
|
|
|
"""))
|
|
|
|
testdir.makefile(ext=".xml", junit="") # Writes junit.xml
|
|
|
|
result = testdir.runpytest("-h", "--junitxml", "junit.xml")
|
|
|
|
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
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
|
|
|
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*"])
|
2014-07-28 17:48:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_conftest_found_with_double_dash(testdir):
|
|
|
|
sub = testdir.mkdir("sub")
|
|
|
|
sub.join("conftest.py").write(py.std.textwrap.dedent("""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello-world", action="store_true")
|
|
|
|
"""))
|
|
|
|
p = sub.join("test_hello.py")
|
|
|
|
p.write(py.std.textwrap.dedent("""
|
|
|
|
import pytest
|
|
|
|
def test_hello(found):
|
|
|
|
assert found == 1
|
|
|
|
"""))
|
2014-08-01 07:29:35 +08:00
|
|
|
result = testdir.runpytest(str(p) + "::test_hello", "-h")
|
2014-07-28 17:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*--hello-world*
|
|
|
|
""")
|
2015-02-27 04:56:44 +08:00
|
|
|
|
|
|
|
|
2015-02-27 16:51:53 +08:00
|
|
|
class TestConftestVisibility:
|
|
|
|
def _setup_tree(self, testdir): # for issue616
|
|
|
|
# example mostly taken from:
|
|
|
|
# https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html
|
|
|
|
runner = testdir.mkdir("empty")
|
|
|
|
package = testdir.mkdir("package")
|
|
|
|
|
|
|
|
package.join("conftest.py").write(dedent("""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fxtr():
|
|
|
|
return "from-package"
|
|
|
|
"""))
|
|
|
|
package.join("test_pkgroot.py").write(dedent("""\
|
|
|
|
def test_pkgroot(fxtr):
|
|
|
|
assert fxtr == "from-package"
|
|
|
|
"""))
|
|
|
|
|
|
|
|
swc = package.mkdir("swc")
|
|
|
|
swc.join("__init__.py").ensure()
|
|
|
|
swc.join("conftest.py").write(dedent("""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fxtr():
|
|
|
|
return "from-swc"
|
|
|
|
"""))
|
|
|
|
swc.join("test_with_conftest.py").write(dedent("""\
|
|
|
|
def test_with_conftest(fxtr):
|
|
|
|
assert fxtr == "from-swc"
|
|
|
|
|
|
|
|
"""))
|
|
|
|
|
|
|
|
snc = package.mkdir("snc")
|
|
|
|
snc.join("__init__.py").ensure()
|
|
|
|
snc.join("test_no_conftest.py").write(dedent("""\
|
|
|
|
def test_no_conftest(fxtr):
|
|
|
|
assert fxtr == "from-package" # No local conftest.py, so should
|
|
|
|
# use value from parent dir's
|
|
|
|
|
|
|
|
"""))
|
|
|
|
print ("created directory structure:")
|
|
|
|
for x in testdir.tmpdir.visit():
|
|
|
|
print (" " + x.relto(testdir.tmpdir))
|
|
|
|
|
|
|
|
return {
|
|
|
|
"runner": runner,
|
|
|
|
"package": package,
|
|
|
|
"swc": swc,
|
|
|
|
"snc": snc}
|
|
|
|
|
|
|
|
# N.B.: "swc" stands for "subdir with conftest.py"
|
|
|
|
# "snc" stands for "subdir no [i.e. without] conftest.py"
|
|
|
|
@pytest.mark.parametrize("chdir,testarg,expect_ntests_passed", [
|
2015-03-02 06:32:00 +08:00
|
|
|
# Effective target: package/..
|
2015-02-27 16:51:53 +08:00
|
|
|
("runner", "..", 3),
|
|
|
|
("package", "..", 3),
|
|
|
|
("swc", "../..", 3),
|
|
|
|
("snc", "../..", 3),
|
|
|
|
|
2015-03-02 06:32:00 +08:00
|
|
|
# Effective target: package
|
2015-02-27 16:51:53 +08:00
|
|
|
("runner", "../package", 3),
|
|
|
|
("package", ".", 3),
|
|
|
|
("swc", "..", 3),
|
|
|
|
("snc", "..", 3),
|
|
|
|
|
2015-03-02 06:32:00 +08:00
|
|
|
# Effective target: package/swc
|
2015-02-27 16:51:53 +08:00
|
|
|
("runner", "../package/swc", 1),
|
|
|
|
("package", "./swc", 1),
|
|
|
|
("swc", ".", 1),
|
|
|
|
("snc", "../swc", 1),
|
|
|
|
|
2015-03-02 06:32:00 +08:00
|
|
|
# Effective target: package/snc
|
2015-02-27 16:51:53 +08:00
|
|
|
("runner", "../package/snc", 1),
|
|
|
|
("package", "./snc", 1),
|
|
|
|
("swc", "../snc", 1),
|
|
|
|
("snc", ".", 1),
|
|
|
|
])
|
|
|
|
@pytest.mark.issue616
|
|
|
|
def test_parsefactories_relative_node_ids(
|
|
|
|
self, testdir, chdir,testarg, expect_ntests_passed):
|
|
|
|
dirs = self._setup_tree(testdir)
|
|
|
|
print("pytest run in cwd: %s" %(
|
|
|
|
dirs[chdir].relto(testdir.tmpdir)))
|
|
|
|
print("pytestarg : %s" %(testarg))
|
|
|
|
print("expected pass : %s" %(expect_ntests_passed))
|
|
|
|
with dirs[chdir].as_cwd():
|
|
|
|
reprec = testdir.inline_run(testarg, "-q", "--traceconfig")
|
|
|
|
reprec.assertoutcome(passed=expect_ntests_passed)
|