2010-11-13 16:05:11 +08:00
|
|
|
import pytest, py
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
from _pytest.main import Session
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
class TestCollector:
|
|
|
|
def test_collect_versus_item(self):
|
2010-11-13 16:05:11 +08:00
|
|
|
from pytest import Collector, Item
|
2010-11-06 16:58:04 +08:00
|
|
|
assert not issubclass(Collector, Item)
|
|
|
|
assert not issubclass(Item, Collector)
|
|
|
|
|
|
|
|
def test_compat_attributes(self, testdir, recwarn):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_pass(): pass
|
|
|
|
def test_fail(): assert 0
|
|
|
|
""")
|
|
|
|
recwarn.clear()
|
2010-11-13 16:05:11 +08:00
|
|
|
assert modcol.Module == pytest.Module
|
|
|
|
assert modcol.Class == pytest.Class
|
|
|
|
assert modcol.Item == pytest.Item
|
|
|
|
assert modcol.File == pytest.File
|
|
|
|
assert modcol.Function == pytest.Function
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
def test_check_equality(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_pass(): pass
|
|
|
|
def test_fail(): assert 0
|
|
|
|
""")
|
|
|
|
fn1 = testdir.collect_by_name(modcol, "test_pass")
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(fn1, pytest.Function)
|
2010-11-06 16:58:04 +08:00
|
|
|
fn2 = testdir.collect_by_name(modcol, "test_pass")
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(fn2, pytest.Function)
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
assert fn1 == fn2
|
|
|
|
assert fn1 != modcol
|
|
|
|
if py.std.sys.version_info < (3, 0):
|
|
|
|
assert cmp(fn1, fn2) == 0
|
|
|
|
assert hash(fn1) == hash(fn2)
|
|
|
|
|
|
|
|
fn3 = testdir.collect_by_name(modcol, "test_fail")
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(fn3, pytest.Function)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert not (fn1 == fn3)
|
|
|
|
assert fn1 != fn3
|
|
|
|
|
|
|
|
for fn in fn1,fn2,fn3:
|
|
|
|
assert fn != 3
|
|
|
|
assert fn != modcol
|
|
|
|
assert fn != [1,2,3]
|
|
|
|
assert [1,2,3] != fn
|
|
|
|
assert modcol != fn
|
|
|
|
|
|
|
|
def test_getparent(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
class TestClass:
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
cls = testdir.collect_by_name(modcol, "TestClass")
|
|
|
|
fn = testdir.collect_by_name(
|
|
|
|
testdir.collect_by_name(cls, "()"), "test_foo")
|
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
parent = fn.getparent(pytest.Module)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert parent is modcol
|
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
parent = fn.getparent(pytest.Function)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert parent is fn
|
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
parent = fn.getparent(pytest.Class)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert parent is cls
|
|
|
|
|
|
|
|
|
|
|
|
def test_getcustomfile_roundtrip(self, testdir):
|
|
|
|
hello = testdir.makefile(".xxx", hello="world")
|
|
|
|
testdir.makepyfile(conftest="""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class CustomFile(pytest.File):
|
2010-11-06 16:58:04 +08:00
|
|
|
pass
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.ext == ".xxx":
|
|
|
|
return CustomFile(path, parent=parent)
|
|
|
|
""")
|
|
|
|
node = testdir.getpathnode(hello)
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(node, pytest.File)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert node.name == "hello.xxx"
|
2010-11-07 17:19:58 +08:00
|
|
|
nodes = node.session.perform_collect([node.nodeid], genitems=False)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert len(nodes) == 1
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(nodes[0], pytest.File)
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
class TestCollectFS:
|
|
|
|
def test_ignored_certain_directories(self, testdir):
|
|
|
|
tmpdir = testdir.tmpdir
|
|
|
|
tmpdir.ensure("_darcs", 'test_notfound.py')
|
|
|
|
tmpdir.ensure("CVS", 'test_notfound.py')
|
|
|
|
tmpdir.ensure("{arch}", 'test_notfound.py')
|
|
|
|
tmpdir.ensure(".whatever", 'test_notfound.py')
|
|
|
|
tmpdir.ensure(".bzr", 'test_notfound.py')
|
|
|
|
tmpdir.ensure("normal", 'test_found.py')
|
2011-03-07 01:32:00 +08:00
|
|
|
for x in tmpdir.visit("test_*.py"):
|
|
|
|
x.write("def test_hello(): pass")
|
2010-11-06 16:58:04 +08:00
|
|
|
|
2013-08-01 23:32:19 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2010-11-06 16:58:04 +08:00
|
|
|
s = result.stdout.str()
|
|
|
|
assert "test_notfound" not in s
|
|
|
|
assert "test_found" in s
|
|
|
|
|
|
|
|
def test_custom_norecursedirs(self, testdir):
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
norecursedirs = mydir xyz*
|
|
|
|
""")
|
|
|
|
tmpdir = testdir.tmpdir
|
|
|
|
tmpdir.ensure("mydir", "test_hello.py").write("def test_1(): pass")
|
|
|
|
tmpdir.ensure("xyz123", "test_2.py").write("def test_2(): 0/0")
|
|
|
|
tmpdir.ensure("xy", "test_ok.py").write("def test_3(): pass")
|
|
|
|
rec = testdir.inline_run()
|
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
rec = testdir.inline_run("xyz123/test_2.py")
|
|
|
|
rec.assertoutcome(failed=1)
|
|
|
|
|
|
|
|
class TestCollectPluginHookRelay:
|
|
|
|
def test_pytest_collect_file(self, testdir):
|
|
|
|
wascalled = []
|
|
|
|
class Plugin:
|
|
|
|
def pytest_collect_file(self, path, parent):
|
|
|
|
wascalled.append(path)
|
|
|
|
testdir.makefile(".abc", "xyz")
|
2013-09-29 04:23:00 +08:00
|
|
|
pytest.main([testdir.tmpdir], plugins=[Plugin()])
|
2010-11-06 16:58:04 +08:00
|
|
|
assert len(wascalled) == 1
|
|
|
|
assert wascalled[0].ext == '.abc'
|
|
|
|
|
|
|
|
def test_pytest_collect_directory(self, testdir):
|
|
|
|
wascalled = []
|
|
|
|
class Plugin:
|
|
|
|
def pytest_collect_directory(self, path, parent):
|
|
|
|
wascalled.append(path.basename)
|
|
|
|
testdir.mkdir("hello")
|
|
|
|
testdir.mkdir("world")
|
2013-09-29 04:23:00 +08:00
|
|
|
pytest.main(testdir.tmpdir, plugins=[Plugin()])
|
2010-11-06 16:58:04 +08:00
|
|
|
assert "hello" in wascalled
|
|
|
|
assert "world" in wascalled
|
|
|
|
|
|
|
|
class TestPrunetraceback:
|
|
|
|
def test_collection_error(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import not_exists
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
assert "__import__" not in result.stdout.str(), "too long traceback"
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ERROR collecting*",
|
|
|
|
"*mport*not_exists*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_custom_repr_failure(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import not_exists
|
|
|
|
""")
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2010-11-06 16:58:04 +08:00
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
return MyFile(path, parent)
|
|
|
|
class MyError(Exception):
|
|
|
|
pass
|
2010-11-13 16:05:11 +08:00
|
|
|
class MyFile(pytest.File):
|
2010-11-06 16:58:04 +08:00
|
|
|
def collect(self):
|
|
|
|
raise MyError()
|
|
|
|
def repr_failure(self, excinfo):
|
|
|
|
if excinfo.errisinstance(MyError):
|
|
|
|
return "hello world"
|
2010-11-13 16:05:11 +08:00
|
|
|
return pytest.File.repr_failure(self, excinfo)
|
2010-11-06 16:58:04 +08:00
|
|
|
""")
|
|
|
|
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ERROR collecting*",
|
|
|
|
"*hello world*",
|
|
|
|
])
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail(reason="other mechanism for adding to reporting needed")
|
2010-11-06 16:58:04 +08:00
|
|
|
def test_collect_report_postprocessing(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import not_exists
|
|
|
|
""")
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2010-11-06 16:58:04 +08:00
|
|
|
def pytest_make_collect_report(__multicall__):
|
|
|
|
rep = __multicall__.execute()
|
|
|
|
rep.headerlines += ["header1"]
|
|
|
|
return rep
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ERROR collecting*",
|
|
|
|
"*header1*",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
class TestCustomConftests:
|
|
|
|
def test_ignore_collect_path(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_ignore_collect(path, config):
|
|
|
|
return path.basename.startswith("x") or \
|
|
|
|
path.basename == "test_one.py"
|
|
|
|
""")
|
2010-11-18 21:56:16 +08:00
|
|
|
sub = testdir.mkdir("xy123")
|
|
|
|
sub.ensure("test_hello.py").write("syntax error")
|
|
|
|
sub.join("conftest.py").write("syntax error")
|
2010-11-06 16:58:04 +08:00
|
|
|
testdir.makepyfile("def test_hello(): pass")
|
|
|
|
testdir.makepyfile(test_one="syntax error")
|
2010-11-18 21:56:16 +08:00
|
|
|
result = testdir.runpytest("--fulltrace")
|
2010-11-06 16:58:04 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2010-11-07 07:22:16 +08:00
|
|
|
def test_ignore_collect_not_called_on_argument(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_ignore_collect(path, config):
|
|
|
|
return True
|
|
|
|
""")
|
|
|
|
p = testdir.makepyfile("def test_hello(): pass")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
assert result.ret == 0
|
|
|
|
assert "1 passed" in result.stdout.str()
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
assert "1 passed" not in result.stdout.str()
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def test_collectignore_exclude_on_option(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
collect_ignore = ['hello', 'test_world.py']
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--XX", action="store_true", default=False)
|
|
|
|
def pytest_configure(config):
|
|
|
|
if config.getvalue("XX"):
|
|
|
|
collect_ignore[:] = []
|
|
|
|
""")
|
|
|
|
testdir.mkdir("hello")
|
|
|
|
testdir.makepyfile(test_world="def test_hello(): pass")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
assert "passed" not in result.stdout.str()
|
|
|
|
result = testdir.runpytest("--XX")
|
|
|
|
assert result.ret == 0
|
|
|
|
assert "passed" in result.stdout.str()
|
|
|
|
|
|
|
|
def test_pytest_fs_collect_hooks_are_seen(self, testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class MyModule(pytest.Module):
|
2010-11-06 16:58:04 +08:00
|
|
|
pass
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.ext == ".py":
|
|
|
|
return MyModule(path, parent)
|
|
|
|
""")
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.mkdir("sub")
|
|
|
|
testdir.makepyfile("def test_x(): pass")
|
2013-08-01 23:32:19 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2010-11-06 16:58:04 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*MyModule*",
|
|
|
|
"*test_x*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_pytest_collect_file_from_sister_dir(self, testdir):
|
|
|
|
sub1 = testdir.mkpydir("sub1")
|
|
|
|
sub2 = testdir.mkpydir("sub2")
|
|
|
|
conf1 = testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class MyModule1(pytest.Module):
|
2010-11-06 16:58:04 +08:00
|
|
|
pass
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.ext == ".py":
|
|
|
|
return MyModule1(path, parent)
|
|
|
|
""")
|
|
|
|
conf1.move(sub1.join(conf1.basename))
|
|
|
|
conf2 = testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class MyModule2(pytest.Module):
|
2010-11-06 16:58:04 +08:00
|
|
|
pass
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.ext == ".py":
|
|
|
|
return MyModule2(path, parent)
|
|
|
|
""")
|
|
|
|
conf2.move(sub2.join(conf2.basename))
|
|
|
|
p = testdir.makepyfile("def test_x(): pass")
|
|
|
|
p.copy(sub1.join(p.basename))
|
|
|
|
p.copy(sub2.join(p.basename))
|
2013-08-01 23:32:19 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2010-11-06 16:58:04 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*MyModule1*",
|
|
|
|
"*MyModule2*",
|
|
|
|
"*test_x*"
|
|
|
|
])
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
class TestSession:
|
2010-09-15 16:30:50 +08:00
|
|
|
def test_parsearg(self, testdir):
|
|
|
|
p = testdir.makepyfile("def test_func(): pass")
|
|
|
|
subdir = testdir.mkdir("sub")
|
|
|
|
subdir.ensure("__init__.py")
|
|
|
|
target = subdir.join(p.basename)
|
|
|
|
p.move(target)
|
|
|
|
subdir.chdir()
|
|
|
|
config = testdir.parseconfig(p.basename)
|
2010-11-07 17:19:58 +08:00
|
|
|
rcol = Session(config=config)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert rcol.fspath == subdir
|
2010-09-15 16:30:50 +08:00
|
|
|
parts = rcol._parsearg(p.basename)
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
assert parts[0] == target
|
|
|
|
assert len(parts) == 1
|
2010-09-15 16:30:50 +08:00
|
|
|
parts = rcol._parsearg(p.basename + "::test_func")
|
2010-11-06 16:58:04 +08:00
|
|
|
assert parts[0] == target
|
|
|
|
assert parts[1] == "test_func"
|
|
|
|
assert len(parts) == 2
|
2010-09-15 16:30:50 +08:00
|
|
|
|
|
|
|
def test_collect_topdir(self, testdir):
|
|
|
|
p = testdir.makepyfile("def test_func(): pass")
|
|
|
|
id = "::".join([p.basename, "test_func"])
|
2015-04-28 17:54:53 +08:00
|
|
|
# XXX migrate to collectonly? (see below)
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfig(id)
|
2010-09-15 16:30:50 +08:00
|
|
|
topdir = testdir.tmpdir
|
2010-11-07 17:19:58 +08:00
|
|
|
rcol = Session(config)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert topdir == rcol.fspath
|
2013-10-12 21:39:22 +08:00
|
|
|
#rootid = rcol.nodeid
|
2010-11-06 16:58:04 +08:00
|
|
|
#root2 = rcol.perform_collect([rcol.nodeid], genitems=False)[0]
|
|
|
|
#assert root2 == rcol, rootid
|
|
|
|
colitems = rcol.perform_collect([rcol.nodeid], genitems=False)
|
|
|
|
assert len(colitems) == 1
|
|
|
|
assert colitems[0].fspath == p
|
|
|
|
|
2010-09-15 16:30:50 +08:00
|
|
|
|
|
|
|
def test_collect_protocol_single_function(self, testdir):
|
|
|
|
p = testdir.makepyfile("def test_func(): pass")
|
|
|
|
id = "::".join([p.basename, "test_func"])
|
2011-11-08 02:08:41 +08:00
|
|
|
items, hookrec = testdir.inline_genitems(id)
|
|
|
|
item, = items
|
2010-09-15 16:30:50 +08:00
|
|
|
assert item.name == "test_func"
|
2010-11-06 16:58:04 +08:00
|
|
|
newid = item.nodeid
|
2010-09-15 16:30:50 +08:00
|
|
|
assert newid == id
|
2014-10-06 19:37:57 +08:00
|
|
|
py.std.pprint.pprint(hookrec.calls)
|
2013-10-12 21:39:22 +08:00
|
|
|
topdir = testdir.tmpdir # noqa
|
2014-10-06 19:37:57 +08:00
|
|
|
hookrec.assert_contains([
|
2010-09-15 16:30:50 +08:00
|
|
|
("pytest_collectstart", "collector.fspath == topdir"),
|
|
|
|
("pytest_make_collect_report", "collector.fspath == topdir"),
|
|
|
|
("pytest_collectstart", "collector.fspath == p"),
|
|
|
|
("pytest_make_collect_report", "collector.fspath == p"),
|
|
|
|
("pytest_pycollect_makeitem", "name == 'test_func'"),
|
2010-11-06 16:58:04 +08:00
|
|
|
("pytest_collectreport", "report.nodeid.startswith(p.basename)"),
|
2015-02-27 04:56:44 +08:00
|
|
|
("pytest_collectreport", "report.nodeid == ''")
|
2010-09-15 16:30:50 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_collect_protocol_method(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
class TestClass:
|
|
|
|
def test_method(self):
|
|
|
|
pass
|
|
|
|
""")
|
2010-11-06 16:58:04 +08:00
|
|
|
normid = p.basename + "::TestClass::()::test_method"
|
2010-09-15 16:30:50 +08:00
|
|
|
for id in [p.basename,
|
|
|
|
p.basename + "::TestClass",
|
|
|
|
p.basename + "::TestClass::()",
|
|
|
|
normid,
|
|
|
|
]:
|
2011-11-08 02:08:41 +08:00
|
|
|
items, hookrec = testdir.inline_genitems(id)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert len(items) == 1
|
|
|
|
assert items[0].name == "test_method"
|
|
|
|
newid = items[0].nodeid
|
2010-09-15 16:30:50 +08:00
|
|
|
assert newid == normid
|
|
|
|
|
|
|
|
def test_collect_custom_nodes_multi_id(self, testdir):
|
|
|
|
p = testdir.makepyfile("def test_func(): pass")
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class SpecialItem(pytest.Item):
|
2010-09-15 16:30:50 +08:00
|
|
|
def runtest(self):
|
|
|
|
return # ok
|
2010-11-13 16:05:11 +08:00
|
|
|
class SpecialFile(pytest.File):
|
2010-09-15 16:30:50 +08:00
|
|
|
def collect(self):
|
|
|
|
return [SpecialItem(name="check", parent=self)]
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.basename == %r:
|
|
|
|
return SpecialFile(fspath=path, parent=parent)
|
|
|
|
""" % p.basename)
|
|
|
|
id = p.basename
|
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
items, hookrec = testdir.inline_genitems(id)
|
2014-10-06 19:37:57 +08:00
|
|
|
py.std.pprint.pprint(hookrec.calls)
|
2010-09-15 16:30:50 +08:00
|
|
|
assert len(items) == 2
|
2014-10-06 19:37:57 +08:00
|
|
|
hookrec.assert_contains([
|
2010-09-15 16:30:50 +08:00
|
|
|
("pytest_collectstart",
|
2010-11-07 17:19:58 +08:00
|
|
|
"collector.fspath == collector.session.fspath"),
|
2010-09-15 16:30:50 +08:00
|
|
|
("pytest_collectstart",
|
|
|
|
"collector.__class__.__name__ == 'SpecialFile'"),
|
|
|
|
("pytest_collectstart",
|
|
|
|
"collector.__class__.__name__ == 'Module'"),
|
|
|
|
("pytest_pycollect_makeitem", "name == 'test_func'"),
|
2010-11-06 16:58:04 +08:00
|
|
|
("pytest_collectreport", "report.nodeid.startswith(p.basename)"),
|
|
|
|
#("pytest_collectreport",
|
|
|
|
# "report.fspath == %r" % str(rcol.fspath)),
|
2010-09-15 16:30:50 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_collect_subdir_event_ordering(self, testdir):
|
|
|
|
p = testdir.makepyfile("def test_func(): pass")
|
|
|
|
aaa = testdir.mkpydir("aaa")
|
|
|
|
test_aaa = aaa.join("test_aaa.py")
|
|
|
|
p.move(test_aaa)
|
2011-11-08 02:08:41 +08:00
|
|
|
|
|
|
|
items, hookrec = testdir.inline_genitems()
|
2010-09-15 16:30:50 +08:00
|
|
|
assert len(items) == 1
|
2014-10-06 19:37:57 +08:00
|
|
|
py.std.pprint.pprint(hookrec.calls)
|
|
|
|
hookrec.assert_contains([
|
2010-09-15 16:30:50 +08:00
|
|
|
("pytest_collectstart", "collector.fspath == test_aaa"),
|
|
|
|
("pytest_pycollect_makeitem", "name == 'test_func'"),
|
2010-11-06 16:58:04 +08:00
|
|
|
("pytest_collectreport",
|
|
|
|
"report.nodeid.startswith('aaa/test_aaa.py')"),
|
2010-09-15 16:30:50 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_collect_two_commandline_args(self, testdir):
|
|
|
|
p = testdir.makepyfile("def test_func(): pass")
|
|
|
|
aaa = testdir.mkpydir("aaa")
|
|
|
|
bbb = testdir.mkpydir("bbb")
|
2010-11-06 16:58:04 +08:00
|
|
|
test_aaa = aaa.join("test_aaa.py")
|
|
|
|
p.copy(test_aaa)
|
|
|
|
test_bbb = bbb.join("test_bbb.py")
|
|
|
|
p.move(test_bbb)
|
2010-09-26 22:23:43 +08:00
|
|
|
|
2010-09-15 16:30:50 +08:00
|
|
|
id = "."
|
2011-11-08 02:08:41 +08:00
|
|
|
|
|
|
|
items, hookrec = testdir.inline_genitems(id)
|
2010-09-15 16:30:50 +08:00
|
|
|
assert len(items) == 2
|
2014-10-06 19:37:57 +08:00
|
|
|
py.std.pprint.pprint(hookrec.calls)
|
|
|
|
hookrec.assert_contains([
|
2010-11-06 16:58:04 +08:00
|
|
|
("pytest_collectstart", "collector.fspath == test_aaa"),
|
2010-09-15 16:30:50 +08:00
|
|
|
("pytest_pycollect_makeitem", "name == 'test_func'"),
|
2010-11-06 16:58:04 +08:00
|
|
|
("pytest_collectreport", "report.nodeid == 'aaa/test_aaa.py'"),
|
|
|
|
("pytest_collectstart", "collector.fspath == test_bbb"),
|
2010-09-15 16:30:50 +08:00
|
|
|
("pytest_pycollect_makeitem", "name == 'test_func'"),
|
2010-11-06 16:58:04 +08:00
|
|
|
("pytest_collectreport", "report.nodeid == 'bbb/test_bbb.py'"),
|
2010-09-15 16:30:50 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_serialization_byid(self, testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile("def test_func(): pass")
|
2011-11-08 02:08:41 +08:00
|
|
|
items, hookrec = testdir.inline_genitems()
|
2010-09-15 16:30:50 +08:00
|
|
|
assert len(items) == 1
|
|
|
|
item, = items
|
2011-11-08 02:08:41 +08:00
|
|
|
items2, hookrec = testdir.inline_genitems(item.nodeid)
|
|
|
|
item2, = items2
|
2010-09-15 16:30:50 +08:00
|
|
|
assert item2.name == item.name
|
|
|
|
assert item2.fspath == item.fspath
|
|
|
|
|
2011-02-07 18:09:42 +08:00
|
|
|
def test_find_byid_without_instance_parents(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
class TestClass:
|
|
|
|
def test_method(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
arg = p.basename + ("::TestClass::test_method")
|
2011-11-08 02:08:41 +08:00
|
|
|
items, hookrec = testdir.inline_genitems(arg)
|
2011-02-07 18:09:42 +08:00
|
|
|
assert len(items) == 1
|
|
|
|
item, = items
|
|
|
|
assert item.nodeid.endswith("TestClass::()::test_method")
|
|
|
|
|
2010-09-15 16:30:50 +08:00
|
|
|
class Test_getinitialnodes:
|
|
|
|
def test_global_file(self, testdir, tmpdir):
|
|
|
|
x = tmpdir.ensure("x.py")
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfigure(x)
|
2010-11-06 16:58:04 +08:00
|
|
|
col = testdir.getnode(config, x)
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(col, pytest.Module)
|
2010-09-15 16:30:50 +08:00
|
|
|
assert col.name == 'x.py'
|
2010-11-06 16:58:04 +08:00
|
|
|
assert col.parent.name == testdir.tmpdir.basename
|
2010-09-15 16:30:50 +08:00
|
|
|
assert col.parent.parent is None
|
|
|
|
for col in col.listchain():
|
|
|
|
assert col.config is config
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def test_pkgfile(self, testdir):
|
|
|
|
tmpdir = testdir.tmpdir
|
|
|
|
subdir = tmpdir.join("subdir")
|
|
|
|
x = subdir.ensure("x.py")
|
|
|
|
subdir.ensure("__init__.py")
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfigure(x)
|
2010-11-06 16:58:04 +08:00
|
|
|
col = testdir.getnode(config, x)
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(col, pytest.Module)
|
2015-02-27 04:56:44 +08:00
|
|
|
assert col.name == 'x.py'
|
2010-11-06 16:58:04 +08:00
|
|
|
assert col.parent.parent is None
|
2010-09-15 16:30:50 +08:00
|
|
|
for col in col.listchain():
|
|
|
|
assert col.config is config
|
|
|
|
|
|
|
|
class Test_genitems:
|
|
|
|
def test_check_collect_hashes(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_1():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_2():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
p.copy(p.dirpath(p.purebasename + "2" + ".py"))
|
|
|
|
items, reprec = testdir.inline_genitems(p.dirpath())
|
|
|
|
assert len(items) == 4
|
|
|
|
for numi, i in enumerate(items):
|
|
|
|
for numj, j in enumerate(items):
|
|
|
|
if numj != numi:
|
|
|
|
assert hash(i) != hash(j)
|
|
|
|
assert i != j
|
|
|
|
|
|
|
|
def test_example_items1(self, testdir):
|
|
|
|
p = testdir.makepyfile('''
|
|
|
|
def testone():
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestX:
|
|
|
|
def testmethod_one(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestY(TestX):
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
items, reprec = testdir.inline_genitems(p)
|
|
|
|
assert len(items) == 3
|
|
|
|
assert items[0].name == 'testone'
|
|
|
|
assert items[1].name == 'testmethod_one'
|
|
|
|
assert items[2].name == 'testmethod_one'
|
|
|
|
|
|
|
|
# let's also test getmodpath here
|
|
|
|
assert items[0].getmodpath() == "testone"
|
|
|
|
assert items[1].getmodpath() == "TestX.testmethod_one"
|
|
|
|
assert items[2].getmodpath() == "TestY.testmethod_one"
|
|
|
|
|
|
|
|
s = items[0].getmodpath(stopatmodule=False)
|
|
|
|
assert s.endswith("test_example_items1.testone")
|
|
|
|
print(s)
|
2010-11-18 01:24:28 +08:00
|
|
|
|
2014-10-17 06:27:10 +08:00
|
|
|
def test_class_and_functions_discovery_using_glob(self, testdir):
|
|
|
|
"""
|
|
|
|
tests that python_classes and python_functions config options work
|
|
|
|
as prefixes and glob-like patterns (issue #600).
|
|
|
|
"""
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
python_classes = *Suite Test
|
|
|
|
python_functions = *_test test
|
|
|
|
""")
|
|
|
|
p = testdir.makepyfile('''
|
|
|
|
class MyTestSuite:
|
|
|
|
def x_test(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestCase:
|
|
|
|
def test_y(self):
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
items, reprec = testdir.inline_genitems(p)
|
|
|
|
ids = [x.getmodpath() for x in items]
|
|
|
|
assert ids == ['MyTestSuite.x_test', 'TestCase.test_y']
|
|
|
|
|
|
|
|
|
2010-11-18 01:24:28 +08:00
|
|
|
def test_matchnodes_two_collections_same_file(testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
def pytest_configure(config):
|
|
|
|
config.pluginmanager.register(Plugin2())
|
|
|
|
|
|
|
|
class Plugin2:
|
|
|
|
def pytest_collect_file(self, path, parent):
|
|
|
|
if path.ext == ".abc":
|
|
|
|
return MyFile2(path, parent)
|
|
|
|
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.ext == ".abc":
|
|
|
|
return MyFile1(path, parent)
|
|
|
|
|
|
|
|
class MyFile1(pytest.Item, pytest.File):
|
|
|
|
def runtest(self):
|
|
|
|
pass
|
|
|
|
class MyFile2(pytest.File):
|
|
|
|
def collect(self):
|
|
|
|
return [Item2("hello", parent=self)]
|
|
|
|
|
|
|
|
class Item2(pytest.Item):
|
|
|
|
def runtest(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
p = testdir.makefile(".abc", "")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*2 passed*",
|
|
|
|
])
|
|
|
|
res = testdir.runpytest("%s::hello" % p.basename)
|
|
|
|
res.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*",
|
|
|
|
])
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-11-18 01:24:28 +08:00
|
|
|
|
2013-10-03 19:53:22 +08:00
|
|
|
class TestNodekeywords:
|
|
|
|
def test_no_under(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_pass(): pass
|
|
|
|
def test_fail(): assert 0
|
|
|
|
""")
|
|
|
|
l = list(modcol.keywords)
|
|
|
|
assert modcol.name in l
|
|
|
|
for x in l:
|
|
|
|
assert not x.startswith("_")
|
|
|
|
assert modcol.name in repr(modcol.keywords)
|
|
|
|
|
|
|
|
def test_issue345(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_should_not_be_selected():
|
|
|
|
assert False, 'I should not have been selected to run'
|
|
|
|
|
|
|
|
def test___repr__():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-k repr")
|
|
|
|
reprec.assertoutcome(passed=1, failed=0)
|