commit
17762db128
|
@ -80,13 +80,6 @@ class Node(object):
|
|||
def __hash__(self):
|
||||
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):
|
||||
pass
|
||||
|
||||
|
@ -127,6 +120,12 @@ class Node(object):
|
|||
def listnames(self):
|
||||
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):
|
||||
cur = self
|
||||
for name in namelist:
|
||||
|
@ -224,9 +223,6 @@ class Node(object):
|
|||
return True
|
||||
return False
|
||||
|
||||
def _getsortvalue(self):
|
||||
return self.name
|
||||
|
||||
def _prunetraceback(self, traceback):
|
||||
return traceback
|
||||
|
||||
|
|
|
@ -66,6 +66,16 @@ class FunctionCollector(py.test.collect.Collector):
|
|||
l.append(function)
|
||||
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:
|
||||
_argprefix = "pytest_funcarg__"
|
||||
|
||||
|
@ -76,7 +86,7 @@ class FuncargRequest:
|
|||
self._pyfuncitem = pyfuncitem
|
||||
self.argname = argname
|
||||
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.instance = getattr(self.function, 'im_self', None)
|
||||
self.config = pyfuncitem.config
|
||||
|
@ -116,7 +126,7 @@ class FuncargRequest:
|
|||
if scope == "function":
|
||||
return self._pyfuncitem
|
||||
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,))
|
||||
|
||||
def addfinalizer(self, finalizer, scope="function"):
|
||||
|
|
|
@ -37,12 +37,6 @@ class PyobjMixin(object):
|
|||
def _getobj(self):
|
||||
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):
|
||||
""" return python path relative to the containing module. """
|
||||
chain = self.listchain()
|
||||
|
@ -99,7 +93,7 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector):
|
|||
return l
|
||||
name2items = self._buildname2items()
|
||||
colitems = name2items.values()
|
||||
colitems.sort()
|
||||
colitems.sort(key=lambda item: item.reportinfo()[:2])
|
||||
return colitems
|
||||
|
||||
def _buildname2items(self):
|
||||
|
@ -146,10 +140,10 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector):
|
|||
return self._genfunctions(name, obj)
|
||||
|
||||
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
|
||||
# to work to get at the class
|
||||
clscol = self._getparent(Class)
|
||||
clscol = self.getparent(Class)
|
||||
cls = clscol and clscol.obj or None
|
||||
metafunc = funcargs.Metafunc(funcobj, config=self.config, cls=cls, module=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(self.obj)
|
||||
|
||||
def _getsortvalue(self):
|
||||
return self._getfslineno()
|
||||
|
||||
class Instance(PyCollectorMixin, py.test.collect.Collector):
|
||||
def _getobj(self):
|
||||
return self.parent.obj()
|
||||
|
@ -236,9 +227,6 @@ class FunctionMixin(PyobjMixin):
|
|||
""" mixin for the code common to Function and Generator.
|
||||
"""
|
||||
|
||||
def _getsortvalue(self):
|
||||
return self._getfslineno()
|
||||
|
||||
def setup(self):
|
||||
""" perform setup for this test function. """
|
||||
if hasattr(self.obj, 'im_self'):
|
||||
|
|
|
@ -6,7 +6,7 @@ class TestCollector:
|
|||
assert not issubclass(Collector, Item)
|
||||
assert not issubclass(Item, Collector)
|
||||
|
||||
def test_check_equality_and_cmp_basic(self, testdir):
|
||||
def test_check_equality(self, testdir):
|
||||
modcol = testdir.getmodulecol("""
|
||||
def test_pass(): pass
|
||||
def test_fail(): assert 0
|
||||
|
@ -25,11 +25,7 @@ class TestCollector:
|
|||
assert isinstance(fn3, py.test.collect.Function)
|
||||
assert not (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:
|
||||
assert fn != 3
|
||||
assert fn != modcol
|
||||
|
@ -37,6 +33,24 @@ class TestCollector:
|
|||
assert [1,2,3] != 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):
|
||||
a = tmpdir.ensure("a", dir=1)
|
||||
tmpdir.ensure("a", "__init__.py")
|
||||
|
|
|
@ -217,7 +217,7 @@ class TestGenerator:
|
|||
class TestFunction:
|
||||
def test_getmodulecollector(self, testdir):
|
||||
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 hasattr(modcol.obj, 'test_func')
|
||||
|
||||
|
@ -269,7 +269,7 @@ class TestFunction:
|
|||
assert not (f5 == f5b)
|
||||
|
||||
class TestSorting:
|
||||
def test_check_equality_and_cmp_basic(self, testdir):
|
||||
def test_check_equality(self, testdir):
|
||||
modcol = testdir.getmodulecol("""
|
||||
def test_pass(): pass
|
||||
def test_fail(): assert 0
|
||||
|
@ -288,11 +288,7 @@ class TestSorting:
|
|||
assert isinstance(fn3, py.test.collect.Function)
|
||||
assert not (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:
|
||||
assert fn != 3
|
||||
assert fn != modcol
|
||||
|
@ -308,18 +304,18 @@ class TestSorting:
|
|||
return g
|
||||
|
||||
|
||||
def test_a(y):
|
||||
pass
|
||||
test_a = dec(test_a)
|
||||
|
||||
def test_b(y):
|
||||
pass
|
||||
test_b = dec(test_b)
|
||||
|
||||
def test_a(y):
|
||||
pass
|
||||
test_a = dec(test_a)
|
||||
""")
|
||||
colitems = modcol.collect()
|
||||
assert len(colitems) == 2
|
||||
f1, f2 = colitems
|
||||
assert cmp(f2, f1) > 0
|
||||
assert [item.name for item in colitems] == ['test_b', 'test_a']
|
||||
|
||||
|
||||
class TestConftestCustomization:
|
||||
def test_extra_python_files_and_functions(self, testdir):
|
||||
|
|
Loading…
Reference in New Issue