commit
17762db128
|
@ -79,13 +79,6 @@ class Node(object):
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash((self.name, self.parent))
|
return hash((self.name, self.parent))
|
||||||
|
|
||||||
def __cmp__(self, other):
|
|
||||||
if not isinstance(other, Node):
|
|
||||||
return -1
|
|
||||||
s1 = self._getsortvalue()
|
|
||||||
s2 = other._getsortvalue()
|
|
||||||
return cmp(s1, s2)
|
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
pass
|
pass
|
||||||
|
@ -127,6 +120,12 @@ class Node(object):
|
||||||
def listnames(self):
|
def listnames(self):
|
||||||
return [x.name for x in self.listchain()]
|
return [x.name for x in self.listchain()]
|
||||||
|
|
||||||
|
def getparent(self, cls):
|
||||||
|
current = self
|
||||||
|
while current and not isinstance(current, cls):
|
||||||
|
current = current.parent
|
||||||
|
return current
|
||||||
|
|
||||||
def _getitembynames(self, namelist):
|
def _getitembynames(self, namelist):
|
||||||
cur = self
|
cur = self
|
||||||
for name in namelist:
|
for name in namelist:
|
||||||
|
@ -224,9 +223,6 @@ class Node(object):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _getsortvalue(self):
|
|
||||||
return self.name
|
|
||||||
|
|
||||||
def _prunetraceback(self, traceback):
|
def _prunetraceback(self, traceback):
|
||||||
return traceback
|
return traceback
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,17 @@ class FunctionCollector(py.test.collect.Collector):
|
||||||
function = self.parent.Function(name=name, parent=self,
|
function = self.parent.Function(name=name, parent=self,
|
||||||
callspec=callspec, callobj=self.obj)
|
callspec=callspec, callobj=self.obj)
|
||||||
l.append(function)
|
l.append(function)
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
def reportinfo(self):
|
||||||
|
try:
|
||||||
|
return self._fslineno, self.name
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
fspath, lineno = py.code.getfslineno(self.obj)
|
||||||
|
self._fslineno = fspath, lineno
|
||||||
|
return fspath, lineno, self.name
|
||||||
|
|
||||||
|
|
||||||
class FuncargRequest:
|
class FuncargRequest:
|
||||||
_argprefix = "pytest_funcarg__"
|
_argprefix = "pytest_funcarg__"
|
||||||
|
@ -76,7 +86,7 @@ class FuncargRequest:
|
||||||
self._pyfuncitem = pyfuncitem
|
self._pyfuncitem = pyfuncitem
|
||||||
self.argname = argname
|
self.argname = argname
|
||||||
self.function = pyfuncitem.obj
|
self.function = pyfuncitem.obj
|
||||||
self.module = pyfuncitem._getparent(py.test.collect.Module).obj
|
self.module = pyfuncitem.getparent(py.test.collect.Module).obj
|
||||||
self.cls = getattr(self.function, 'im_class', None)
|
self.cls = getattr(self.function, 'im_class', None)
|
||||||
self.instance = getattr(self.function, 'im_self', None)
|
self.instance = getattr(self.function, 'im_self', None)
|
||||||
self.config = pyfuncitem.config
|
self.config = pyfuncitem.config
|
||||||
|
@ -116,7 +126,7 @@ class FuncargRequest:
|
||||||
if scope == "function":
|
if scope == "function":
|
||||||
return self._pyfuncitem
|
return self._pyfuncitem
|
||||||
elif scope == "module":
|
elif scope == "module":
|
||||||
return self._pyfuncitem._getparent(py.test.collect.Module)
|
return self._pyfuncitem.getparent(py.test.collect.Module)
|
||||||
raise ValueError("unknown finalization scope %r" %(scope,))
|
raise ValueError("unknown finalization scope %r" %(scope,))
|
||||||
|
|
||||||
def addfinalizer(self, finalizer, scope="function"):
|
def addfinalizer(self, finalizer, scope="function"):
|
||||||
|
|
|
@ -37,12 +37,6 @@ class PyobjMixin(object):
|
||||||
def _getobj(self):
|
def _getobj(self):
|
||||||
return getattr(self.parent.obj, self.name)
|
return getattr(self.parent.obj, self.name)
|
||||||
|
|
||||||
def _getparent(self, cls):
|
|
||||||
current = self
|
|
||||||
while current and not isinstance(current, cls):
|
|
||||||
current = current.parent
|
|
||||||
return current
|
|
||||||
|
|
||||||
def getmodpath(self, stopatmodule=True, includemodule=False):
|
def getmodpath(self, stopatmodule=True, includemodule=False):
|
||||||
""" return python path relative to the containing module. """
|
""" return python path relative to the containing module. """
|
||||||
chain = self.listchain()
|
chain = self.listchain()
|
||||||
|
@ -99,7 +93,7 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector):
|
||||||
return l
|
return l
|
||||||
name2items = self._buildname2items()
|
name2items = self._buildname2items()
|
||||||
colitems = name2items.values()
|
colitems = name2items.values()
|
||||||
colitems.sort()
|
colitems.sort(key=lambda item: item.reportinfo()[:2])
|
||||||
return colitems
|
return colitems
|
||||||
|
|
||||||
def _buildname2items(self):
|
def _buildname2items(self):
|
||||||
|
@ -146,10 +140,10 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector):
|
||||||
return self._genfunctions(name, obj)
|
return self._genfunctions(name, obj)
|
||||||
|
|
||||||
def _genfunctions(self, name, funcobj):
|
def _genfunctions(self, name, funcobj):
|
||||||
module = self._getparent(Module).obj
|
module = self.getparent(Module).obj
|
||||||
# due to _buildname2items funcobj is the raw function, we need
|
# due to _buildname2items funcobj is the raw function, we need
|
||||||
# to work to get at the class
|
# to work to get at the class
|
||||||
clscol = self._getparent(Class)
|
clscol = self.getparent(Class)
|
||||||
cls = clscol and clscol.obj or None
|
cls = clscol and clscol.obj or None
|
||||||
metafunc = funcargs.Metafunc(funcobj, config=self.config, cls=cls, module=module)
|
metafunc = funcargs.Metafunc(funcobj, config=self.config, cls=cls, module=module)
|
||||||
gentesthook = self.config.hook.pytest_generate_tests.clone(extralookup=module)
|
gentesthook = self.config.hook.pytest_generate_tests.clone(extralookup=module)
|
||||||
|
@ -211,9 +205,6 @@ class Class(PyCollectorMixin, py.test.collect.Collector):
|
||||||
teardown_class = getattr(teardown_class, 'im_func', teardown_class)
|
teardown_class = getattr(teardown_class, 'im_func', teardown_class)
|
||||||
teardown_class(self.obj)
|
teardown_class(self.obj)
|
||||||
|
|
||||||
def _getsortvalue(self):
|
|
||||||
return self._getfslineno()
|
|
||||||
|
|
||||||
class Instance(PyCollectorMixin, py.test.collect.Collector):
|
class Instance(PyCollectorMixin, py.test.collect.Collector):
|
||||||
def _getobj(self):
|
def _getobj(self):
|
||||||
return self.parent.obj()
|
return self.parent.obj()
|
||||||
|
@ -236,9 +227,6 @@ class FunctionMixin(PyobjMixin):
|
||||||
""" mixin for the code common to Function and Generator.
|
""" mixin for the code common to Function and Generator.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _getsortvalue(self):
|
|
||||||
return self._getfslineno()
|
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
""" perform setup for this test function. """
|
""" perform setup for this test function. """
|
||||||
if hasattr(self.obj, 'im_self'):
|
if hasattr(self.obj, 'im_self'):
|
||||||
|
|
|
@ -6,7 +6,7 @@ class TestCollector:
|
||||||
assert not issubclass(Collector, Item)
|
assert not issubclass(Collector, Item)
|
||||||
assert not issubclass(Item, Collector)
|
assert not issubclass(Item, Collector)
|
||||||
|
|
||||||
def test_check_equality_and_cmp_basic(self, testdir):
|
def test_check_equality(self, testdir):
|
||||||
modcol = testdir.getmodulecol("""
|
modcol = testdir.getmodulecol("""
|
||||||
def test_pass(): pass
|
def test_pass(): pass
|
||||||
def test_fail(): assert 0
|
def test_fail(): assert 0
|
||||||
|
@ -25,11 +25,7 @@ class TestCollector:
|
||||||
assert isinstance(fn3, py.test.collect.Function)
|
assert isinstance(fn3, py.test.collect.Function)
|
||||||
assert not (fn1 == fn3)
|
assert not (fn1 == fn3)
|
||||||
assert fn1 != fn3
|
assert fn1 != fn3
|
||||||
assert cmp(fn1, fn3) == -1
|
|
||||||
|
|
||||||
assert cmp(fn1, 10) == -1
|
|
||||||
assert cmp(fn2, 10) == -1
|
|
||||||
assert cmp(fn3, 10) == -1
|
|
||||||
for fn in fn1,fn2,fn3:
|
for fn in fn1,fn2,fn3:
|
||||||
assert fn != 3
|
assert fn != 3
|
||||||
assert fn != modcol
|
assert fn != modcol
|
||||||
|
@ -37,6 +33,24 @@ class TestCollector:
|
||||||
assert [1,2,3] != fn
|
assert [1,2,3] != fn
|
||||||
assert modcol != fn
|
assert modcol != fn
|
||||||
|
|
||||||
|
def test_getparent(self, testdir):
|
||||||
|
modcol = testdir.getmodulecol("""
|
||||||
|
class TestClass:
|
||||||
|
def test_foo():
|
||||||
|
pass
|
||||||
|
""")
|
||||||
|
cls = modcol.collect_by_name("TestClass")
|
||||||
|
fn = cls.collect_by_name("()").collect_by_name("test_foo")
|
||||||
|
|
||||||
|
parent = fn.getparent(py.test.collect.Module)
|
||||||
|
assert parent is modcol
|
||||||
|
|
||||||
|
parent = fn.getparent(py.test.collect.Function)
|
||||||
|
assert parent is fn
|
||||||
|
|
||||||
|
parent = fn.getparent(py.test.collect.Class)
|
||||||
|
assert parent is cls
|
||||||
|
|
||||||
def test_totrail_and_back(self, tmpdir):
|
def test_totrail_and_back(self, tmpdir):
|
||||||
a = tmpdir.ensure("a", dir=1)
|
a = tmpdir.ensure("a", dir=1)
|
||||||
tmpdir.ensure("a", "__init__.py")
|
tmpdir.ensure("a", "__init__.py")
|
||||||
|
|
|
@ -217,7 +217,7 @@ class TestGenerator:
|
||||||
class TestFunction:
|
class TestFunction:
|
||||||
def test_getmodulecollector(self, testdir):
|
def test_getmodulecollector(self, testdir):
|
||||||
item = testdir.getitem("def test_func(): pass")
|
item = testdir.getitem("def test_func(): pass")
|
||||||
modcol = item._getparent(py.test.collect.Module)
|
modcol = item.getparent(py.test.collect.Module)
|
||||||
assert isinstance(modcol, py.test.collect.Module)
|
assert isinstance(modcol, py.test.collect.Module)
|
||||||
assert hasattr(modcol.obj, 'test_func')
|
assert hasattr(modcol.obj, 'test_func')
|
||||||
|
|
||||||
|
@ -269,7 +269,7 @@ class TestFunction:
|
||||||
assert not (f5 == f5b)
|
assert not (f5 == f5b)
|
||||||
|
|
||||||
class TestSorting:
|
class TestSorting:
|
||||||
def test_check_equality_and_cmp_basic(self, testdir):
|
def test_check_equality(self, testdir):
|
||||||
modcol = testdir.getmodulecol("""
|
modcol = testdir.getmodulecol("""
|
||||||
def test_pass(): pass
|
def test_pass(): pass
|
||||||
def test_fail(): assert 0
|
def test_fail(): assert 0
|
||||||
|
@ -288,11 +288,7 @@ class TestSorting:
|
||||||
assert isinstance(fn3, py.test.collect.Function)
|
assert isinstance(fn3, py.test.collect.Function)
|
||||||
assert not (fn1 == fn3)
|
assert not (fn1 == fn3)
|
||||||
assert fn1 != fn3
|
assert fn1 != fn3
|
||||||
assert cmp(fn1, fn3) == -1
|
|
||||||
|
|
||||||
assert cmp(fn1, 10) == -1
|
|
||||||
assert cmp(fn2, 10) == -1
|
|
||||||
assert cmp(fn3, 10) == -1
|
|
||||||
for fn in fn1,fn2,fn3:
|
for fn in fn1,fn2,fn3:
|
||||||
assert fn != 3
|
assert fn != 3
|
||||||
assert fn != modcol
|
assert fn != modcol
|
||||||
|
@ -308,18 +304,18 @@ class TestSorting:
|
||||||
return g
|
return g
|
||||||
|
|
||||||
|
|
||||||
def test_a(y):
|
|
||||||
pass
|
|
||||||
test_a = dec(test_a)
|
|
||||||
|
|
||||||
def test_b(y):
|
def test_b(y):
|
||||||
pass
|
pass
|
||||||
test_b = dec(test_b)
|
test_b = dec(test_b)
|
||||||
|
|
||||||
|
def test_a(y):
|
||||||
|
pass
|
||||||
|
test_a = dec(test_a)
|
||||||
""")
|
""")
|
||||||
colitems = modcol.collect()
|
colitems = modcol.collect()
|
||||||
assert len(colitems) == 2
|
assert len(colitems) == 2
|
||||||
f1, f2 = colitems
|
assert [item.name for item in colitems] == ['test_b', 'test_a']
|
||||||
assert cmp(f2, f1) > 0
|
|
||||||
|
|
||||||
class TestConftestCustomization:
|
class TestConftestCustomization:
|
||||||
def test_extra_python_files_and_functions(self, testdir):
|
def test_extra_python_files_and_functions(self, testdir):
|
||||||
|
|
Loading…
Reference in New Issue