[py-svn] r7890 - in py/dist/py: . test

hpk at codespeak.net hpk at codespeak.net
Thu Dec 16 17:55:47 CET 2004


Author: hpk
Date: Thu Dec 16 17:55:47 2004
New Revision: 7890

Modified:
   py/dist/py/__init__.py
   py/dist/py/test/collect.py
   py/dist/py/test/compat.py
   py/dist/py/test/config.py
   py/dist/py/test/defaultconfig.py
   py/dist/py/test/item.py
   py/dist/py/test/run.py
   py/dist/py/test/test_collect.py
Log:
allow customization of the collection process from
configfiles (conftest.py).  The idea is that you 
can can locally take over the collection process 
for Directories, Modules or Classes. "Locally" means
that if you have 

    py.test x/y/z  

then py.test will import and look into 

    x/y/z/conftest.py 
    x/y/conftest.py
    ... 

in order to find Directory/Module/Class collectors. 
The first match wins. 



Modified: py/dist/py/__init__.py
==============================================================================
--- py/dist/py/__init__.py	(original)
+++ py/dist/py/__init__.py	Thu Dec 16 17:55:47 2004
@@ -14,6 +14,7 @@
     'test.collect.Collector':  './test/collect.Collector',
     'test.collect.Directory':  './test/collect.Directory',
     'test.collect.Module':     './test/collect.Module',
+    'test.collect.Class':     './test/collect.Class',
     'test.collect.PyCollector':'./test/collect.PyCollector',
     'test.collect.Error':      './test/collect.Error',
     'test.Item':               './test/item.Item', 

Modified: py/dist/py/test/collect.py
==============================================================================
--- py/dist/py/test/collect.py	(original)
+++ py/dist/py/test/collect.py	Thu Dec 16 17:55:47 2004
@@ -3,6 +3,20 @@
 import sys, inspect, types
 from py import path, test 
 import py
+
+def getfscollector(fspath): 
+    if fspath.check(file=1):
+        col = py.test.config.getconfigvalue(fspath, 'Module')
+    elif fspath.check(dir=1):
+        col = py.test.config.getconfigvalue(fspath, 'Directory')
+    else:
+        raise IOError, "%r does not exist" % fspath 
+    return col(fspath) 
+
+def configproperty(name): 
+    def fget(self): 
+        return py.test.config.getconfigvalue(self.fspath, name) 
+    return property(fget) 
         
 class Error(object):
     """ represents a non-fatal exception while collecting. """
@@ -25,6 +39,9 @@
     Item = test.Item 
     GenItem = test.GenItem 
     Error = Error
+    Module = configproperty('Module') 
+    Directory = configproperty('Directory') 
+    Class = configproperty('Class') 
 
     def iterunits(self):
         """ yield all units of the Collector instance. """
@@ -57,13 +74,14 @@
         return (fspath.check(file=1, fnmatch='test_*.py') or 
                 fspath.check(file=1, fnmatch='*_test.py'))
     rec = py.path.checker(dir=1, dotfile=0, link=0)
+
     def __iter__(self):
         try:
             for fspath in self.fspath.listdir(sort=True): 
                 if self.rec(fspath):
-                    yield self.__class__(fspath)
+                    yield self.Directory(fspath) 
                 elif self.fil(fspath):
-                    yield Module(fspath) 
+                    yield self.Module(fspath) 
         except:
             yield self._except()
 
@@ -80,6 +98,8 @@
                             for x in dir(self.__class__) 
                                 if x.startswith('collect_')]
 
+    fspath = property(lambda self: self.extpy.root) 
+
     def __repr__(self):
         return '%s(%r)' %(self.__class__.__name__, str(self.extpy))
 
@@ -93,7 +113,6 @@
                     #print "looking at", extpy
                     for x in meth(extpy):
                         #print "found", x 
-                        x.fspath = self.extpy.root 
                         sortvalue = self.getsortvalue(x) 
                         l.append((sortvalue, x)) 
             l.sort() 
@@ -145,7 +164,7 @@
         if extpy.check(basestarts='Test') and self.extpy.samefile(extpy):
             obj = extpy.resolve()
             if inspect.isclass(obj) and not getattr(obj, 'disabled', 0):
-                yield Class(extpy)
+                yield self.Class(extpy)
 
 class Class(PyCollector):
     def collect_method(self, extpy):
@@ -157,7 +176,10 @@
                 yield Generator(extpy) 
             else: 
                 func = extpy.resolve()
-                yield getattr(func.im_class, 'Item', self.Item)(extpy)
+                try:
+                    yield getattr(func.im_class, 'Item')(extpy) 
+                except AttributeError: 
+                    yield self.Item(extpy) 
 
 class Generator(PyCollector): 
     def dispatch(self, obj): 

Modified: py/dist/py/test/compat.py
==============================================================================
--- py/dist/py/test/compat.py	(original)
+++ py/dist/py/test/compat.py	Thu Dec 16 17:55:47 2004
@@ -5,7 +5,7 @@
     """ compatibility Unit executor for TestCase methods
         honouring setUp and tearDown semantics. 
     """
-    def execute(self, runner):
+    def execute(self, driver):
         unboundmethod = self.extpy.resolve() 
         cls = unboundmethod.im_class
         instance = cls()

Modified: py/dist/py/test/config.py
==============================================================================
--- py/dist/py/test/config.py	(original)
+++ py/dist/py/test/config.py	Thu Dec 16 17:55:47 2004
@@ -21,7 +21,7 @@
         try:
             return sessiondir[0]
         except IndexError:
-            d = py.path.local.make_numbered_dir(base='utest-')
+            d = py.path.local.make_numbered_dir(base='pytest-')
             sessiondir.append(d)
             return d
     tmpdir = property(_gettmpdir, None, None, "Temporary Directory")

Modified: py/dist/py/test/defaultconfig.py
==============================================================================
--- py/dist/py/test/defaultconfig.py	(original)
+++ py/dist/py/test/defaultconfig.py	Thu Dec 16 17:55:47 2004
@@ -1,6 +1,10 @@
 import py 
 Option = py.test.Option
 
+Module = py.test.collect.Module 
+Directory = py.test.collect.Directory 
+Class = py.test.collect.Class
+
 def getreporter():
     return py.test.TextReporter()
 

Modified: py/dist/py/test/item.py
==============================================================================
--- py/dist/py/test/item.py	(original)
+++ py/dist/py/test/item.py	Thu Dec 16 17:55:47 2004
@@ -90,7 +90,7 @@
         """ return a string representing a call to the underlying 
             test function. 
         """ 
-        return "%r%r" % (self.extpy.modpath, self.args) 
+        return "%s%r" % (self.extpy.modpath, self.args) 
             
     class Outcome: 
         def __init__(self, **kwargs):

Modified: py/dist/py/test/run.py
==============================================================================
--- py/dist/py/test/run.py	(original)
+++ py/dist/py/test/run.py	Thu Dec 16 17:55:47 2004
@@ -2,6 +2,7 @@
 import py 
 from py.__impl__.execnet.channel import ChannelFile, receive2file
 from py.__impl__.test.config import configbasename
+from py.__impl__.test.collect import getfscollector
 import sys
 
 def checkpyfilechange(rootdir, statcache): 
@@ -140,12 +141,7 @@
     current = py.path.local()
     for fn in filenames:
         fullfn = current.join(fn, abs=1)
-        if fullfn.check(file=1):
-            yield py.test.collect.Module(fullfn)
-        elif fullfn.check(dir=1):
-            yield py.test.collect.Directory(fullfn)
-        else:
-            raise IOError, "%r does not exist" % fn 
+        yield getfscollector(fullfn) 
         
 def getanchors(args):
     """ yield "anchors" from skimming the args for existing files/dirs. """

Modified: py/dist/py/test/test_collect.py
==============================================================================
--- py/dist/py/test/test_collect.py	(original)
+++ py/dist/py/test/test_collect.py	Thu Dec 16 17:55:47 2004
@@ -111,3 +111,33 @@
 
     def test_previous_generator_worked(self): 
         assert self.l3 == [4,5]
+
+def test_custom_collection_from_conftest(): 
+    o = py.test.config.tmpdir.ensure('customconfigtest', dir=1)
+    o.ensure('conftest.py').write("""if 1: 
+        import py
+        class MyItem(py.test.Item): 
+            pass
+        class Directory(py.test.collect.Directory): 
+            def fil(self, fspath): 
+                return fspath.check(basestarts='check_')
+        class Module(py.test.collect.Module): 
+            def collect_function(self, extpy): 
+                if extpy.check(basestarts='check_', func=1): 
+                    yield self.Item(extpy) 
+        class Class(py.test.collect.Class): 
+            def collect_method(self, extpy): 
+                if extpy.check(basestarts='check_', func=1):
+                    yield MyItem(extpy) 
+        """)
+    o.ensure('somedir', 'check_something').write("""if 1: 
+        def check_func(): 
+            assert 42 == 42 
+        class TestClass: 
+            def check_method(self): 
+                assert 23 == 23
+        """)
+    from py.__impl__.test.collect import getfscollector 
+    units = list(getfscollector(o).iterunits()) 
+    assert len(units) == 2
+    assert units[1].__class__.__name__ == 'MyItem' 



More information about the pytest-commit mailing list