1 new commit in pytest:
https://bitbucket.org/hpk42/pytest/commits/b2108fd98d3b/
Changeset: b2108fd98d3b
User: danilobellini
Date: 2013-05-17 17:18:22
Summary: zero to many doctests from module instead of one
Affected #: 2 files
diff -r 7db6af80a1defcc27d9016a0258b5be31386d82e -r
b2108fd98d3b4a4ed8ad6f43a1cddebf5a5b7212 _pytest/doctest.py
--- a/_pytest/doctest.py
+++ b/_pytest/doctest.py
@@ -34,6 +34,14 @@
self.reprlocation.toterminal(tw)
class DoctestItem(pytest.Item):
+ def __init__(self, name, parent, runner=None, dtest=None):
+ super(DoctestItem, self).__init__(name, parent)
+ self.runner = runner
+ self.dtest = dtest
+
+ def runtest(self):
+ self.runner.run(self.dtest)
+
def repr_failure(self, excinfo):
doctest = py.std.doctest
if excinfo.errisinstance((doctest.DocTestFailure,
@@ -76,7 +84,7 @@
return super(DoctestItem, self).repr_failure(excinfo)
def reportinfo(self):
- return self.fspath, None, "[doctest]"
+ return self.fspath, None, "[doctest] %s" % self.name
class DoctestTextfile(DoctestItem, pytest.File):
def runtest(self):
@@ -91,8 +99,8 @@
extraglobs=dict(getfixture=fixture_request.getfuncargvalue),
raise_on_error=True, verbose=0)
-class DoctestModule(DoctestItem, pytest.File):
- def runtest(self):
+class DoctestModule(pytest.File):
+ def collect(self):
doctest = py.std.doctest
if self.fspath.basename == "conftest.py":
module = self.config._conftest.importconftest(self.fspath)
@@ -102,7 +110,11 @@
self.funcargs = {}
self._fixtureinfo = FuncFixtureInfo((), [], {})
fixture_request = FixtureRequest(self)
- failed, tot = doctest.testmod(
- module, raise_on_error=True, verbose=0,
- extraglobs=dict(getfixture=fixture_request.getfuncargvalue),
- optionflags=doctest.ELLIPSIS)
+ doctest_globals = dict(getfixture=fixture_request.getfuncargvalue)
+ # uses internal doctest module parsing mechanism
+ finder = doctest.DocTestFinder()
+ runner = doctest.DebugRunner(verbose=0, optionflags=doctest.ELLIPSIS)
+ for test in finder.find(module, module.__name__,
+ extraglobs=doctest_globals):
+ if test.examples: # skip empty doctests
+ yield DoctestItem(test.name, self, runner, test)
diff -r 7db6af80a1defcc27d9016a0258b5be31386d82e -r
b2108fd98d3b4a4ed8ad6f43a1cddebf5a5b7212 testing/test_doctest.py
--- a/testing/test_doctest.py
+++ b/testing/test_doctest.py
@@ -1,4 +1,4 @@
-from _pytest.doctest import DoctestModule, DoctestTextfile
+from _pytest.doctest import DoctestItem, DoctestModule, DoctestTextfile
import py, pytest
class TestDoctests:
@@ -19,13 +19,61 @@
items, reprec = testdir.inline_genitems(w)
assert len(items) == 1
- def test_collect_module(self, testdir):
+ def test_collect_module_empty(self, testdir):
path = testdir.makepyfile(whatever="#")
for p in (path, testdir.tmpdir):
items, reprec = testdir.inline_genitems(p,
'--doctest-modules')
+ assert len(items) == 0
+
+ def test_collect_module_single_modulelevel_doctest(self, testdir):
+ path = testdir.makepyfile(whatever='""">>> pass"""')
+ for p in (path, testdir.tmpdir):
+ items, reprec = testdir.inline_genitems(p,
+ '--doctest-modules')
assert len(items) == 1
- assert isinstance(items[0], DoctestModule)
+ assert isinstance(items[0], DoctestItem)
+ assert isinstance(items[0].parent, DoctestModule)
+
+ def test_collect_module_two_doctest_one_modulelevel(self, testdir):
+ path = testdir.makepyfile(whatever="""
+ '>>> x = None'
+ def my_func():
+ ">>> magic = 42 "
+ """)
+ for p in (path, testdir.tmpdir):
+ items, reprec = testdir.inline_genitems(p,
+ '--doctest-modules')
+ assert len(items) == 2
+ assert isinstance(items[0], DoctestItem)
+ assert isinstance(items[1], DoctestItem)
+ assert isinstance(items[0].parent, DoctestModule)
+ assert items[0].parent is items[1].parent
+
+ def test_collect_module_two_doctest_no_modulelevel(self, testdir):
+ path = testdir.makepyfile(whatever="""
+ '# Empty'
+ def my_func():
+ ">>> magic = 42 "
+ def unuseful():
+ '''
+ # This is a function
+ # >>> # it doesn't have any doctest
+ '''
+ def another():
+ '''
+ # This is another function
+ >>> import os # this one does have a doctest
+ '''
+ """)
+ for p in (path, testdir.tmpdir):
+ items, reprec = testdir.inline_genitems(p,
+ '--doctest-modules')
+ assert len(items) == 2
+ assert isinstance(items[0], DoctestItem)
+ assert isinstance(items[1], DoctestItem)
+ assert isinstance(items[0].parent, DoctestModule)
+ assert items[0].parent is items[1].parent
def test_simple_doctestfile(self, testdir):
p = testdir.maketxtfile(test_doc="""
@@ -164,3 +212,47 @@
""")
reprec = testdir.inline_run(p, "--doctest-modules")
reprec.assertoutcome(passed=1)
+
+ def test_doctestmodule_three_tests(self, testdir):
+ p = testdir.makepyfile("""
+ '''
+ >>> dir = getfixture('tmpdir')
+ >>> type(dir).__name__
+ 'LocalPath'
+ '''
+ def my_func():
+ '''
+ >>> magic = 42
+ >>> magic - 42
+ 0
+ '''
+ def unuseful():
+ pass
+ def another():
+ '''
+ >>> import os
+ >>> os is os
+ True
+ '''
+ """)
+ reprec = testdir.inline_run(p, "--doctest-modules")
+ reprec.assertoutcome(passed=3)
+
+ def test_doctestmodule_two_tests_one_fail(self, testdir):
+ p = testdir.makepyfile("""
+ class MyClass:
+ def bad_meth(self):
+ '''
+ >>> magic = 42
+ >>> magic
+ 0
+ '''
+ def nice_meth(self):
+ '''
+ >>> magic = 42
+ >>> magic - 42
+ 0
+ '''
+ """)
+ reprec = testdir.inline_run(p, "--doctest-modules")
+ reprec.assertoutcome(failed=1, passed=1)
Repository URL: https://bitbucket.org/hpk42/pytest/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
_______________________________________________
pytest-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pytest-commit