[py-svn] r12304 - py/dist/py/documentation

hpk at codespeak.net hpk at codespeak.net
Sun May 15 18:31:58 CEST 2005


Author: hpk
Date: Sun May 15 18:31:58 2005
New Revision: 12304

Added:
   py/dist/py/documentation/test_conftest.py
Modified:
   py/dist/py/documentation/conftest.py
Log:
allow to have doctest snippets in '.txt' files in
the documentation directory. 

Additionally you can specify "doctestmodulescope: dotted.name" 
and the dotted name will get imported and it's dict items 
merged with the doctest pseudo-module. 
    


Modified: py/dist/py/documentation/conftest.py
==============================================================================
--- py/dist/py/documentation/conftest.py	(original)
+++ py/dist/py/documentation/conftest.py	Sun May 15 18:31:58 2005
@@ -59,18 +59,44 @@
     def teardown(self): 
         pass 
     def run(self): 
-        return [self.fspath.basename, 'checklinks']
+        return [self.fspath.basename, 'checklinks', 'doctest']
     def join(self, name): 
         if name == self.fspath.basename: 
             return ReSTSyntaxTest(name, parent=self) 
         elif name == 'checklinks': 
             return LinkCheckerMaker(name, self) 
+        elif name == 'doctest': 
+            return DoctestTextModule(name, self) 
     
 class ReSTSyntaxTest(py.test.Item): 
     def run(self): 
         mypath = self.fspath 
         restcheck(py.path.svnwc(mypath))
 
+class DoctestTextModule(py.test.Item): 
+    rex = py.std.re.compile(r'doctestmodulescope: ([\w\.]+)')
+
+    def mergescopes(self, mod, scopes): 
+        """ merge the vars of the imported module from importname 
+            into the given 'mod'
+        """
+        for modname in scopes: 
+            scopemod = __import__(modname, None, None, ['__doc__'])
+            for name, value in vars(scopemod).items(): 
+                if name[:2] != '__' and name[-2:] != '__': 
+                    setattr(mod, name, value) 
+    
+    def run(self): 
+        s = self.fspath.read()
+        scopes = self.rex.findall(s)
+        mod = py.std.types.ModuleType(self.fspath.basename, s)
+        if s.find('>>>') != -1: 
+            self.mergescopes(mod, scopes) 
+            failed, tot = py.std.doctest.testmod(mod, verbose=1)
+            if failed: 
+                py.test.fail("doctest %s: %s failed out of %s" %(
+                             self.fspath, failed, tot))
+        
 class LinkCheckerMaker(py.test.collect.Collector): 
     def run(self): 
         l = []

Added: py/dist/py/documentation/test_conftest.py
==============================================================================
--- (empty file)
+++ py/dist/py/documentation/test_conftest.py	Sun May 15 18:31:58 2005
@@ -0,0 +1,36 @@
+
+import py
+
+def setup_module(mod): 
+    mod.tmpdir = py.test.ensuretemp('docdoctest')
+
+def test_doctest_basic(): 
+    # XXX get rid of the next line: 
+    py.magic.autopath().dirpath('conftest.py').copy(tmpdir.join('conftest.py'))
+    tmpdir.ensure('__init__.py')
+
+    xtxt = tmpdir.join('x.txt')
+    xtxt.write(py.code.Source("""
+        ..  doctestmodulescope: os.path
+
+        hello world 
+
+            >>> assert abspath 
+            >>> i=3
+            >>> print i
+            3
+
+        yes yes
+
+            >>> i
+            3
+
+        end
+    """))
+    config, args = py.test.Config.parse([str(xtxt)]) 
+    session = config.getsessionclass()(config, py.std.sys.stdout)
+    session.main([xtxt]) 
+    l = session.getitemoutcomepairs(py.test.Item.Failed)
+    assert len(l) == 0 
+    l = session.getitemoutcomepairs(py.test.Item.Passed)
+    assert len(l) == 2



More information about the pytest-commit mailing list