[Checkins] SVN: zope.testing/branches/regebro-python3/src/zope/testing/testrunner/ ported marker-parsing functions from z3c.testsetup
Wolfgang Schnerring
wosc at wosc.de
Mon Sep 14 01:43:36 EDT 2009
Log message for revision 103906:
ported marker-parsing functions from z3c.testsetup
Changed:
U zope.testing/branches/regebro-python3/src/zope/testing/testrunner/doctest.py
U zope.testing/branches/regebro-python3/src/zope/testing/testrunner/options.py
A zope.testing/branches/regebro-python3/src/zope/testing/testrunner/testrunner-doctestfinder.txt
U zope.testing/branches/regebro-python3/src/zope/testing/testrunner/tests.py
-=-
Modified: zope.testing/branches/regebro-python3/src/zope/testing/testrunner/doctest.py
===================================================================
--- zope.testing/branches/regebro-python3/src/zope/testing/testrunner/doctest.py 2009-09-14 05:42:01 UTC (rev 103905)
+++ zope.testing/branches/regebro-python3/src/zope/testing/testrunner/doctest.py 2009-09-14 05:43:35 UTC (rev 103906)
@@ -17,6 +17,7 @@
"""
from zope.testing import doctest
+import re
import zope.testing.testrunner.feature
@@ -50,3 +51,43 @@
def global_shutdown(self):
doctest.set_unittest_reportflags(self.old_reporting_flags)
+
+
+class DocFileFind(zope.testing.testrunner.feature.Feature):
+ """Finds doctest files and registers them with the test runner."""
+
+ active = True
+
+ def global_setup(self):
+ tests = self._find_doctest_files()
+ #self.runner.register_tests(tests)
+
+ def _find_doctest_files(self):
+ pass
+
+
+def parse_directive_from_string(directive, text):
+ """Looks for a reST directive in a string.
+
+ Returns the found value or `None`. A directive has the form::
+
+ .. <directive>:: <value>
+ """
+
+ directive_pattern = re.compile(
+ r'^(\.\.\s+)%s\s*::(.*)$' % (directive,), re.IGNORECASE)
+ for line in text.split('\n'):
+ line = line.strip()
+ result = directive_pattern.match(line)
+ if result is None:
+ continue
+ result = result.groups()[1].strip()
+ return unicode(result)
+ return None
+
+
+def parse_directive_from_file(directive, filepath):
+ """Looks for a reST directive in a file. (see `parse_directive_from_string`)
+ """
+
+ return parse_directive_from_string(directive, open(filepath, 'rb').read())
Modified: zope.testing/branches/regebro-python3/src/zope/testing/testrunner/options.py
===================================================================
--- zope.testing/branches/regebro-python3/src/zope/testing/testrunner/options.py 2009-09-14 05:42:01 UTC (rev 103905)
+++ zope.testing/branches/regebro-python3/src/zope/testing/testrunner/options.py 2009-09-14 05:43:35 UTC (rev 103906)
@@ -379,6 +379,17 @@
""")
setup.add_option(
+ '--doctests-pattern', action="store", dest='doctests_pattern',
+ help="""\
+The test runner looks for files containing doctests. It uses this
+pattern to identify these files.
+
+Only doctests that contain a `testcase` directive
+(e. g. `.. testcase:: some.package.DocTestCase`) will be picked up
+and registered as doctests, using the given TestCase.
+""")
+
+setup.add_option(
'--suite-name', action="store", dest='suite_name',
help="""\
Specify the name of the object in each test_module that contains the
@@ -451,6 +462,7 @@
parser.set_defaults(
ignore_dir=['.svn', 'CVS', '{arch}', '.arch-ids', '_darcs'],
tests_pattern='^tests$',
+ doctests_pattern='\.txt$'
at_level=1,
test_file_pattern='^test',
suite_name='test_suite',
@@ -547,6 +559,7 @@
options.ignore_dir = dict([(d,1) for d in options.ignore_dir])
options.test_file_pattern = re.compile(options.test_file_pattern).search
options.tests_pattern = re.compile(options.tests_pattern).search
+ options.doctests_pattern = re.compile(options.tests_pattern).search
options.test = map(compile_filter, options.test or ('.'))
options.module = map(compile_filter, options.module or ('.'))
Added: zope.testing/branches/regebro-python3/src/zope/testing/testrunner/testrunner-doctestfinder.txt
===================================================================
--- zope.testing/branches/regebro-python3/src/zope/testing/testrunner/testrunner-doctestfinder.txt (rev 0)
+++ zope.testing/branches/regebro-python3/src/zope/testing/testrunner/testrunner-doctestfinder.txt 2009-09-14 05:43:35 UTC (rev 103906)
@@ -0,0 +1,78 @@
+==================
+ Finding doctests
+==================
+
+parse_directive_from_string
+---------------------------
+
+Looks for a markerstring in a string and returns the found value or
+`None`. A markerstring has the form:
+
+.. <directive>:: <value>
+
+ >>> text = """Some text
+ ...
+ ... .. mydirective:: foo
+ ...
+ ... Some other text
+ ... """
+ >>> from zope.testing.testrunner.doctest import parse_directive_from_string
+ >>> parse_directive_from_string('mydirective', text)
+ u'foo'
+
+ If the directive can't be found, None is returned:
+
+ >>> parse_directive_from_string('not-present', text) is None
+ True
+
+It does not matter whether the marker string starts at the beginning
+of a line. Also several whitespaces between the marker string and the
+applied value are accepted. The tag in the marker can be written with
+upper or lower case letters or both in a wild mix:
+
+ >>> text = """Some text
+ ...
+ ... .. TeSt-lAyEr:: foo
+ ...
+ ... Some other text
+ ... """
+ >>> parse_directive_from_string('test-Layer', text)
+ u'foo'
+
+
+parse_directive_from_file
+-------------------------
+
+Similar to parse_directive_from_string, but searches a file instead of a string.
+
+ >>> text = """Some text
+ ...
+ ... .. mydirective:: foo
+ ...
+ ... Some other text
+ ... """
+ >>> import tempfile
+ >>> ignore, tmpfile = tempfile.mkstemp()
+ >>> open(tmpfile, 'w').write(text)
+
+ >>> from zope.testing.testrunner.doctest import parse_directive_from_file
+ >>> parse_directive_from_file('mydirective', tmpfile)
+ u'foo'
+ >>> parse_directive_from_file('not-present', tmpfile) is None
+ True
+
+
+Finding files
+-------------
+
+ >>> import os.path, sys
+ >>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex')
+ >>> defaults = [
+ ... '--path', directory_with_tests,
+ ... '--tests-pattern', '^sampletestsf?$',
+ ... '--doctests-pattern', '\.txt$',
+ ... ]
+
+ >>> sys.argv = 'test -u'.split()
+ >>> from zope.testing import testrunner
+ >>> testrunner.run_internal(defaults)
Modified: zope.testing/branches/regebro-python3/src/zope/testing/testrunner/tests.py
===================================================================
--- zope.testing/branches/regebro-python3/src/zope/testing/testrunner/tests.py 2009-09-14 05:42:01 UTC (rev 103905)
+++ zope.testing/branches/regebro-python3/src/zope/testing/testrunner/tests.py 2009-09-14 05:43:35 UTC (rev 103906)
@@ -148,6 +148,7 @@
'testrunner-coverage.txt',
'testrunner-debugging-layer-setup.test',
'testrunner-debugging.txt',
+ 'testrunner-doctestfinder.txt',
'testrunner-edge-cases.txt',
'testrunner-errors.txt',
'testrunner-layers-buff.txt',
More information about the checkins
mailing list