[py-svn] r29726 - in py/dist/py: . documentation test test/testing

hpk at codespeak.net hpk at codespeak.net
Fri Jul 7 12:01:02 CEST 2006


Author: hpk
Date: Fri Jul  7 12:00:58 2006
New Revision: 29726

Added:
   py/dist/py/test/doctest.py   (contents, props changed)
   py/dist/py/test/testing/test_doctest.py   (contents, props changed)
Modified:
   py/dist/py/__init__.py
   py/dist/py/documentation/test.txt
   py/dist/py/test/collect.py
   py/dist/py/test/defaultconftest.py
Log:
(guido, hpk) added experimental suppport for doctests - 
currently they need to be in files named test_*.txt. 
See documentation. 



Modified: py/dist/py/__init__.py
==============================================================================
--- py/dist/py/__init__.py	(original)
+++ py/dist/py/__init__.py	Fri Jul  7 12:00:58 2006
@@ -41,6 +41,7 @@
     'test.collect.Collector' : ('./test/collect.py', 'Collector'),
     'test.collect.Directory' : ('./test/collect.py', 'Directory'),
     'test.collect.Module'    : ('./test/collect.py', 'Module'),
+    'test.collect.DoctestFile' : ('./test/collect.py', 'DoctestFile'),
     'test.collect.Class'     : ('./test/collect.py', 'Class'),
     'test.collect.Instance'  : ('./test/collect.py', 'Instance'),
     'test.collect.Generator' : ('./test/collect.py', 'Generator'),

Modified: py/dist/py/documentation/test.txt
==============================================================================
--- py/dist/py/documentation/test.txt	(original)
+++ py/dist/py/documentation/test.txt	Fri Jul  7 12:00:58 2006
@@ -290,6 +290,15 @@
 ``teardown_module`` pair more than once during the execution of a test
 module.
 
+Experimental doctest support 
+------------------------------------------------------------
+
+If you want to integrate doctests, ``py.test`` now by default
+picks up files matching the ``test_*.txt`` pattern and
+processes them as text files containing doctests. 
+This is an experimental feature and likely to change
+its implementation. 
+
 Working Examples
 ================
 

Modified: py/dist/py/test/collect.py
==============================================================================
--- py/dist/py/test/collect.py	(original)
+++ py/dist/py/test/collect.py	Fri Jul  7 12:00:58 2006
@@ -60,6 +60,7 @@
         self.fspath = getattr(parent, 'fspath', None) 
 
     Module = configproperty('Module')
+    DoctestFile = configproperty('DoctestFile')
     Directory = configproperty('Directory')
     Class = configproperty('Class')
     Instance = configproperty('Instance')
@@ -223,7 +224,8 @@
 class Directory(FSCollector): 
     def filefilter(self, path): 
         b = path.basename 
-        return b.startswith('test_') and b.endswith('.py')
+        ext = path.ext
+        return b.startswith('test_') and ext in ('.txt', '.py')
     
     def recfilter(self, path): 
         return path.check(dotfile=0) and \
@@ -240,7 +242,10 @@
     def makeitem(self, basename, filefilter=None, recfilter=None): 
         p = self.fspath.join(basename)
         if p.check(file=1) and (not filefilter or filefilter(p)): 
-            return self.Module(p, parent=self) 
+            if p.ext == '.py':
+                return self.Module(p, parent=self) 
+            elif p.ext == '.txt':
+                return self.DoctestFile(p, parent=self)
         elif p.check(dir=1) and (not recfilter or recfilter(p)): 
             Directory = py.test.Config.getvalue('Directory', p) 
             return Directory(p, parent=self) 
@@ -389,3 +394,14 @@
 
     def getsortvalue(self):  
         return self.getpathlineno() 
+
+class DoctestFile(PyCollectorMixin, FSCollector): 
+    def run(self):
+        return [self.fspath.basename]
+
+    def join(self, name):
+        from py.__.test.doctest import DoctestText
+        if name == self.fspath.basename: 
+            item = DoctestText(self.fspath.basename, parent=self)
+            item._content = self.fspath.read()
+            return item

Modified: py/dist/py/test/defaultconftest.py
==============================================================================
--- py/dist/py/test/defaultconftest.py	(original)
+++ py/dist/py/test/defaultconftest.py	Fri Jul  7 12:00:58 2006
@@ -1,6 +1,7 @@
 import py
 
 Module = py.test.collect.Module 
+DoctestFile = py.test.collect.DoctestFile 
 Directory = py.test.collect.Directory
 Class = py.test.collect.Class
 Generator = py.test.collect.Generator 

Added: py/dist/py/test/doctest.py
==============================================================================
--- (empty file)
+++ py/dist/py/test/doctest.py	Fri Jul  7 12:00:58 2006
@@ -0,0 +1,33 @@
+import py
+
+class DoctestText(py.test.Item):
+
+    def _setcontent(self, content):
+        self._content = content 
+
+    #def buildname2items(self):
+    #    parser = py.compat.doctest.DoctestParser()
+    #    l = parser.get_examples(self._content)
+    #    d = {}
+    #    globs = {}
+    #    locs
+    #    for i, example in py.builtin.enumerate(l):
+    #        ex = ExampleItem(example)
+    #        d[str(i)] = ex
+
+    def run(self):
+        mod = py.std.types.ModuleType(self.name) 
+        #for line in s.split('\n'): 
+        #    if line.startswith(prefix): 
+        #        exec py.code.Source(line[len(prefix):]).compile() in mod.__dict__ 
+        #        line = ""
+        #    else: 
+        #        l.append(line)
+        self.execute(mod, self._content) 
+       
+    def execute(self, mod, docstring):
+        mod.__doc__ = docstring 
+        failed, tot = py.compat.doctest.testmod(mod, verbose=1)
+        if failed: 
+            py.test.fail("doctest %s: %s failed out of %s" %(
+                         self.fspath, failed, tot))

Added: py/dist/py/test/testing/test_doctest.py
==============================================================================
--- (empty file)
+++ py/dist/py/test/testing/test_doctest.py	Fri Jul  7 12:00:58 2006
@@ -0,0 +1,43 @@
+
+import py
+from py.__.test.doctest import DoctestText
+
+def test_simple_docteststring():
+    testitem = DoctestText(name="dummy", parent=None)
+    testitem._setcontent("""
+    >>> i = 0
+    >>> i + 1
+    1
+    """)
+    res = testitem.run()
+    assert res is None
+    
+def test_simple_docteststring_failing():
+    testitem = DoctestText(name="dummy2", parent=None)
+    testitem._setcontent("""
+    >>> i = 0
+    >>> i + 1
+    2
+    """)
+    py.test.raises(py.test.Item.Failed, "testitem.run()")
+   
+
+def test_collect_doctest_files_with_test_prefix():
+    o = py.test.ensuretemp("testdoctest")
+    checkfile = o.ensure("test_something.txt")
+    o.ensure("whatever.txt")
+    checkfile.write(py.code.Source("""
+        alskdjalsdk
+        >>> i = 5
+        >>> i-1
+        4
+    """))
+    from py.__.test.collect import getfscollector
+    for x in (o, checkfile): 
+        #print "checking that %s returns custom items" % (x,) 
+        col = getfscollector(x)
+        items = list(col.tryiter(py.test.Item))
+        assert len(items) == 1
+        assert isinstance(items[0], DoctestText)
+   
+     



More information about the pytest-commit mailing list