[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
This commit is contained in:
hpk 2008-07-04 09:14:36 +02:00
parent cb53ad6010
commit d4c3b3ce85
4 changed files with 35 additions and 43 deletions

View File

@ -502,13 +502,17 @@ class Generator(FunctionMixin, PyCollectorMixin, Collector):
call, args = obj, () call, args = obj, ()
return call, args 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): def run(self):
return [self.fspath.basename] return [self.fspath.basename]
def join(self, name): def join(self, name):
from py.__.test.doctest import DoctestText
if name == self.fspath.basename: if name == self.fspath.basename:
item = DoctestText(self.fspath.basename, parent=self) from py.__.test.doctest import DoctestFileContent
item._content = self.fspath.read() return DoctestFileContent(name, self)
return item

View File

@ -1,9 +1,6 @@
import py import py
class DoctestText(py.test.collect.Item): class DoctestFileContent(py.test.collect.Item):
def _setcontent(self, content):
self._content = content
#def buildname2items(self): #def buildname2items(self):
# parser = py.compat.doctest.DoctestParser() # parser = py.compat.doctest.DoctestParser()
@ -16,18 +13,12 @@ class DoctestText(py.test.collect.Item):
# d[str(i)] = ex # d[str(i)] = ex
def run(self): def run(self):
mod = py.std.types.ModuleType(self.name) self.execute()
#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): def execute(self):
mod.__doc__ = docstring failed, tot = py.compat.doctest.testfile(str(self.fspath), module_relative=False, verbose=1)
failed, tot = py.compat.doctest.testmod(mod, verbose=1) #mod.__file__ = str(self.fspath)
#failed, tot = py.compat.doctest.testmod(mod, verbose=1)
if failed: if failed:
py.test.fail("doctest %s: %s failed out of %s" %( py.test.fail("doctest %s: %s failed out of %s" %(
self.fspath, failed, tot)) self.fspath, failed, tot))

View File

@ -78,14 +78,6 @@ class BaseReason(object):
def __repr__(self): def __repr__(self):
return self.msg 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.... # whatever comes here....
def skip(msg=BaseReason()): def skip(msg=BaseReason()):

View File

@ -1,25 +1,30 @@
import py import py
from py.__.test.doctest import DoctestText
from py.__.test.outcome import Skipped, Failed, Passed, Outcome from py.__.test.outcome import Skipped, Failed, Passed, Outcome
def setup_module(mod):
mod.tmp = py.test.ensuretemp(__name__)
def test_simple_docteststring(): def test_simple_docteststring():
testitem = DoctestText(name="dummy", parent=None) p = tmp.join("test_simple_docteststring")
testitem._setcontent(""" p.write(py.code.Source("""
>>> i = 0 >>> i = 0
>>> i + 1 >>> i + 1
1 1
""") """))
testitem = py.test.collect.DoctestFile(p).join(p.basename)
res = testitem.run() res = testitem.run()
assert res is None assert res is None
def test_simple_docteststring_failing(): def test_simple_docteststring_failing():
testitem = DoctestText(name="dummy2", parent=None) p = tmp.join("test_simple_docteststring_failing")
testitem._setcontent(""" p.write(py.code.Source("""
>>> i = 0 >>> i = 0
>>> i + 1 >>> i + 1
2 2
""") """))
testitem = py.test.collect.DoctestFile(p).join(p.basename)
py.test.raises(Failed, "testitem.run()") py.test.raises(Failed, "testitem.run()")
@ -39,6 +44,6 @@ def test_collect_doctest_files_with_test_prefix():
col = config._getcollector(x) col = config._getcollector(x)
items = list(col._tryiter(py.test.collect.Item)) items = list(col._tryiter(py.test.collect.Item))
assert len(items) == 1 assert len(items) == 1
assert isinstance(items[0], DoctestText) assert isinstance(items[0].parent, py.test.collect.DoctestFile)