Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #64 from bukzor/master

support for treating doctests as "real" tests
  • Loading branch information...
commit d2aaadfdb7f0a5eece4f69a4eaa449fa9159193a 2 parents 684a84a + 796abab
Amber Yust authored April 04, 2012

Showing 1 changed file with 48 additions and 0 deletions. Show diff stats Hide diff stats

  1. 48  testify/contrib/doctestcase.py
48  testify/contrib/doctestcase.py
... ...
@@ -0,0 +1,48 @@
  1
+import sys
  2
+
  3
+from doctest import DocTestFinder, DocTestRunner, REPORT_NDIFF
  4
+from StringIO import StringIO
  5
+from testify import MetaTestCase
  6
+from types import MethodType
  7
+
  8
+class DocMetaTestCase(MetaTestCase):
  9
+	"""
  10
+	A testify TestCase that turns doctests into unit tests.
  11
+	"""
  12
+	def __init__(cls, name, bases, dct):
  13
+		super(DocMetaTestCase, cls).__init__(name, bases, dct)
  14
+
  15
+		module = dct['module']
  16
+		globs = dct.get('globs', None)
  17
+		extraglobs = dct.get('extraglobs', None)
  18
+
  19
+		if isinstance(module, basestring):
  20
+			# transform a module name into a module
  21
+			module = sys.modules[module]
  22
+
  23
+		for doctest in DocTestFinder(recurse=True).find(module, name='test_doc', globs=globs, extraglobs=extraglobs):
  24
+			cls.add_test(doctest)
  25
+
  26
+	def add_test(cls, doctest):
  27
+		"add a test to this TestCase"
  28
+		if not doctest.examples:
  29
+			# There's no tests in this doctest. Don't bother.
  30
+			return
  31
+
  32
+		test = lambda self: run_test(doctest)
  33
+
  34
+		# Need to change dots to colons so that testify doesn't try to interpret them.
  35
+		testname = doctest.name.replace('.', ':')
  36
+		test.__name__ = doctest.name = testname
  37
+
  38
+		test = MethodType(test, None, cls)
  39
+		vars(test)['_suites'] = set()
  40
+
  41
+		setattr(cls, test.__name__, test)
  42
+
  43
+def run_test(doctest):
  44
+	summary = StringIO()
  45
+	runner = DocTestRunner(optionflags=REPORT_NDIFF)
  46
+	runner.run(doctest, out=summary.write)
  47
+
  48
+	assert runner.failures == 0, '\n' + summary.getvalue()

0 notes on commit d2aaadf

Please sign in to comment.
Something went wrong with that request. Please try again.