From d4c3b3ce85dcb054a49b8e7d207ea0e07eba149c Mon Sep 17 00:00:00 2001 From: hpk Date: Fri, 4 Jul 2008 09:14:36 +0200 Subject: [PATCH] [svn r56285] sanitize doctesting a bit. this also fixes the problem that you could not have "import" working in doctests previously. thanks nshepperd. --HG-- branch : trunk --- py/test/collect.py | 16 ++++++++++------ py/test/doctest.py | 21 ++++++--------------- py/test/item.py | 8 -------- py/test/testing/test_doctest.py | 33 +++++++++++++++++++-------------- 4 files changed, 35 insertions(+), 43 deletions(-) diff --git a/py/test/collect.py b/py/test/collect.py index 7b79b096f..bd3944d40 100644 --- a/py/test/collect.py +++ b/py/test/collect.py @@ -502,13 +502,17 @@ class Generator(FunctionMixin, PyCollectorMixin, Collector): call, args = obj, () return call, args -class DoctestFile(PyCollectorMixin, FSCollector): +class DoctestFile(Module): + # XXX fix py.test reporting + # we subclass Module here in order to get py.test's reporting + # show the ".txt" filename in the test run much like a + # python module shows up. instead py.test needs to + # support more direct means of influencing reporting. 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 + if name == self.fspath.basename: + from py.__.test.doctest import DoctestFileContent + return DoctestFileContent(name, self) + diff --git a/py/test/doctest.py b/py/test/doctest.py index c2df712da..48e40a747 100644 --- a/py/test/doctest.py +++ b/py/test/doctest.py @@ -1,9 +1,6 @@ import py -class DoctestText(py.test.collect.Item): - - def _setcontent(self, content): - self._content = content +class DoctestFileContent(py.test.collect.Item): #def buildname2items(self): # parser = py.compat.doctest.DoctestParser() @@ -16,18 +13,12 @@ class DoctestText(py.test.collect.Item): # 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) + self.execute() - def execute(self, mod, docstring): - mod.__doc__ = docstring - failed, tot = py.compat.doctest.testmod(mod, verbose=1) + def execute(self): + failed, tot = py.compat.doctest.testfile(str(self.fspath), module_relative=False, verbose=1) + #mod.__file__ = str(self.fspath) + #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)) diff --git a/py/test/item.py b/py/test/item.py index 3d7052c6b..f823cbf09 100644 --- a/py/test/item.py +++ b/py/test/item.py @@ -78,14 +78,6 @@ class BaseReason(object): def __repr__(self): return self.msg -class Broken(BaseReason): - def __repr__(self): - return "Broken: %s" % (self.msg,) - -class _NotImplemented(BaseReason): - def __repr__(self): - return "Not implemented: %s" % (self.msg,) - # whatever comes here.... def skip(msg=BaseReason()): diff --git a/py/test/testing/test_doctest.py b/py/test/testing/test_doctest.py index bd44a559b..551066ce7 100644 --- a/py/test/testing/test_doctest.py +++ b/py/test/testing/test_doctest.py @@ -1,25 +1,30 @@ import py -from py.__.test.doctest import DoctestText from py.__.test.outcome import Skipped, Failed, Passed, Outcome + +def setup_module(mod): + mod.tmp = py.test.ensuretemp(__name__) + def test_simple_docteststring(): - testitem = DoctestText(name="dummy", parent=None) - testitem._setcontent(""" - >>> i = 0 - >>> i + 1 - 1 - """) + p = tmp.join("test_simple_docteststring") + p.write(py.code.Source(""" + >>> i = 0 + >>> i + 1 + 1 + """)) + testitem = py.test.collect.DoctestFile(p).join(p.basename) 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 - """) + p = tmp.join("test_simple_docteststring_failing") + p.write(py.code.Source(""" + >>> i = 0 + >>> i + 1 + 2 + """)) + testitem = py.test.collect.DoctestFile(p).join(p.basename) py.test.raises(Failed, "testitem.run()") @@ -39,6 +44,6 @@ def test_collect_doctest_files_with_test_prefix(): col = config._getcollector(x) items = list(col._tryiter(py.test.collect.Item)) assert len(items) == 1 - assert isinstance(items[0], DoctestText) + assert isinstance(items[0].parent, py.test.collect.DoctestFile)