Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Modified test_utils to work with unittest2 discovery.

  • Loading branch information...
commit 751b007c9840de9cb4e5fc622e8ea9f44e5554ca 1 parent cdf520e
Preston Timmons authored April 06, 2013 carljm committed April 12, 2013
19  django/test/simple.py
@@ -42,6 +42,13 @@ def get_tests(app_module):
42 42
     return test_module
43 43
 
44 44
 
  45
+def make_doctest(module):
  46
+    return doctest.DocTestSuite(module,
  47
+       checker=doctestOutputChecker,
  48
+       runner=DocTestRunner,
  49
+    )
  50
+
  51
+
45 52
 def build_suite(app_module):
46 53
     """
47 54
     Create a complete Django test suite for the provided application module.
@@ -56,9 +63,7 @@ def build_suite(app_module):
56 63
         suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
57 64
             app_module))
58 65
         try:
59  
-            suite.addTest(doctest.DocTestSuite(app_module,
60  
-                                               checker=doctestOutputChecker,
61  
-                                               runner=DocTestRunner))
  66
+            suite.addTest(make_doctest(app_module))
62 67
         except ValueError:
63 68
             # No doc tests in models.py
64 69
             pass
@@ -75,9 +80,7 @@ def build_suite(app_module):
75 80
             suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
76 81
                 test_module))
77 82
             try:
78  
-                suite.addTest(doctest.DocTestSuite(
79  
-                    test_module, checker=doctestOutputChecker,
80  
-                    runner=DocTestRunner))
  83
+                suite.addTest(make_doctest(test_module))
81 84
             except ValueError:
82 85
                 # No doc tests in tests.py
83 86
                 pass
@@ -130,9 +133,7 @@ def build_test(label):
130 133
     tests = []
131 134
     for module in app_module, test_module:
132 135
         try:
133  
-            doctests = doctest.DocTestSuite(module,
134  
-                                            checker=doctestOutputChecker,
135  
-                                            runner=DocTestRunner)
  136
+            doctests = make_doctest(module)
136 137
             # Now iterate over the suite, looking for doctests whose name
137 138
             # matches the pattern that was given
138 139
             for test in doctests:
77  tests/test_utils/doctest_output.py
... ...
@@ -0,0 +1,77 @@
  1
+from django.utils import six
  2
+
  3
+__test__ = {"API_TEST": r"""
  4
+# Some checks of the doctest output normalizer.
  5
+# Standard doctests do fairly
  6
+>>> import json
  7
+>>> from django.utils.xmlutils import SimplerXMLGenerator
  8
+>>> from django.utils.six import StringIO
  9
+
  10
+>>> def produce_json():
  11
+...     return json.dumps(['foo', {'bar': ('baz', None, 1.0, 2), 'whiz': 42}])
  12
+
  13
+>>> def produce_xml():
  14
+...     stream = StringIO()
  15
+...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
  16
+...     xml.startDocument()
  17
+...     xml.startElement("foo", {"aaa" : "1.0", "bbb": "2.0"})
  18
+...     xml.startElement("bar", {"ccc" : "3.0"})
  19
+...     xml.characters("Hello")
  20
+...     xml.endElement("bar")
  21
+...     xml.startElement("whiz", {})
  22
+...     xml.characters("Goodbye")
  23
+...     xml.endElement("whiz")
  24
+...     xml.endElement("foo")
  25
+...     xml.endDocument()
  26
+...     return stream.getvalue()
  27
+
  28
+>>> def produce_xml_fragment():
  29
+...     stream = StringIO()
  30
+...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
  31
+...     xml.startElement("foo", {"aaa": "1.0", "bbb": "2.0"})
  32
+...     xml.characters("Hello")
  33
+...     xml.endElement("foo")
  34
+...     xml.startElement("bar", {"ccc": "3.0", "ddd": "4.0"})
  35
+...     xml.endElement("bar")
  36
+...     return stream.getvalue()
  37
+
  38
+# JSON output is normalized for field order, so it doesn't matter
  39
+# which order json dictionary attributes are listed in output
  40
+>>> produce_json()
  41
+'["foo", {"bar": ["baz", null, 1.0, 2], "whiz": 42}]'
  42
+
  43
+>>> produce_json()
  44
+'["foo", {"whiz": 42, "bar": ["baz", null, 1.0, 2]}]'
  45
+
  46
+# XML output is normalized for attribute order, so it doesn't matter
  47
+# which order XML element attributes are listed in output
  48
+>>> produce_xml()
  49
+'<?xml version="1.0" encoding="UTF-8"?>\n<foo aaa="1.0" bbb="2.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
  50
+
  51
+>>> produce_xml()
  52
+'<?xml version="1.0" encoding="UTF-8"?>\n<foo bbb="2.0" aaa="1.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
  53
+
  54
+>>> produce_xml_fragment()
  55
+'<foo aaa="1.0" bbb="2.0">Hello</foo><bar ccc="3.0" ddd="4.0"></bar>'
  56
+
  57
+>>> produce_xml_fragment()
  58
+'<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>'
  59
+
  60
+"""}
  61
+
  62
+if not six.PY3:
  63
+    __test__["API_TEST"] += """
  64
+>>> def produce_long():
  65
+...     return 42L
  66
+
  67
+>>> def produce_int():
  68
+...     return 42
  69
+
  70
+# Long values are normalized and are comparable to normal integers ...
  71
+>>> produce_long()
  72
+42
  73
+
  74
+# ... and vice versa
  75
+>>> produce_int()
  76
+42L
  77
+"""
86  tests/test_utils/tests.py
... ...
@@ -1,5 +1,6 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 from __future__ import absolute_import, unicode_literals
  3
+from StringIO import StringIO
3 4
 import warnings
4 5
 
5 6
 from django.db import connection
@@ -8,8 +9,10 @@
8 9
 from django.template.loader import render_to_string
9 10
 from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
10 11
 from django.test.html import HTMLParseError, parse_html
  12
+from django.test.simple import make_doctest
11 13
 from django.test.utils import CaptureQueriesContext
12 14
 from django.utils import six
  15
+from django.utils import unittest
13 16
 from django.utils.unittest import skip
14 17
 
15 18
 from .models import Person
@@ -621,79 +624,10 @@ class MyCustomField(IntegerField):
621 624
             }
622 625
         self.assertFieldOutput(MyCustomField, {}, {}, empty_value=None)
623 626
 
624  
-__test__ = {"API_TEST": r"""
625  
-# Some checks of the doctest output normalizer.
626  
-# Standard doctests do fairly
627  
->>> import json
628  
->>> from django.utils.xmlutils import SimplerXMLGenerator
629  
->>> from django.utils.six import StringIO
630  
-
631  
->>> def produce_json():
632  
-...     return json.dumps(['foo', {'bar': ('baz', None, 1.0, 2), 'whiz': 42}])
633  
-
634  
->>> def produce_xml():
635  
-...     stream = StringIO()
636  
-...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
637  
-...     xml.startDocument()
638  
-...     xml.startElement("foo", {"aaa" : "1.0", "bbb": "2.0"})
639  
-...     xml.startElement("bar", {"ccc" : "3.0"})
640  
-...     xml.characters("Hello")
641  
-...     xml.endElement("bar")
642  
-...     xml.startElement("whiz", {})
643  
-...     xml.characters("Goodbye")
644  
-...     xml.endElement("whiz")
645  
-...     xml.endElement("foo")
646  
-...     xml.endDocument()
647  
-...     return stream.getvalue()
648  
-
649  
->>> def produce_xml_fragment():
650  
-...     stream = StringIO()
651  
-...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
652  
-...     xml.startElement("foo", {"aaa": "1.0", "bbb": "2.0"})
653  
-...     xml.characters("Hello")
654  
-...     xml.endElement("foo")
655  
-...     xml.startElement("bar", {"ccc": "3.0", "ddd": "4.0"})
656  
-...     xml.endElement("bar")
657  
-...     return stream.getvalue()
658  
-
659  
-# JSON output is normalized for field order, so it doesn't matter
660  
-# which order json dictionary attributes are listed in output
661  
->>> produce_json()
662  
-'["foo", {"bar": ["baz", null, 1.0, 2], "whiz": 42}]'
663  
-
664  
->>> produce_json()
665  
-'["foo", {"whiz": 42, "bar": ["baz", null, 1.0, 2]}]'
666  
-
667  
-# XML output is normalized for attribute order, so it doesn't matter
668  
-# which order XML element attributes are listed in output
669  
->>> produce_xml()
670  
-'<?xml version="1.0" encoding="UTF-8"?>\n<foo aaa="1.0" bbb="2.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
671  
-
672  
->>> produce_xml()
673  
-'<?xml version="1.0" encoding="UTF-8"?>\n<foo bbb="2.0" aaa="1.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
674  
-
675  
->>> produce_xml_fragment()
676  
-'<foo aaa="1.0" bbb="2.0">Hello</foo><bar ccc="3.0" ddd="4.0"></bar>'
677  
-
678  
->>> produce_xml_fragment()
679  
-'<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>'
680  
-
681  
-"""}
682  
-
683  
-if not six.PY3:
684  
-    __test__["API_TEST"] += """
685  
->>> def produce_long():
686  
-...     return 42L
687  
-
688  
->>> def produce_int():
689  
-...     return 42
690  
-
691  
-# Long values are normalized and are comparable to normal integers ...
692  
->>> produce_long()
693  
-42
694  
-
695  
-# ... and vice versa
696  
->>> produce_int()
697  
-42L
698  
-
699  
-"""
  627
+
  628
+class DoctestNormalizerTest(SimpleTestCase):
  629
+
  630
+    def test_normalizer(self):
  631
+        suite = make_doctest("test_utils.doctest_output")
  632
+        failures = unittest.TextTestRunner(stream=StringIO()).run(suite)
  633
+        self.assertEqual(failures.failures, [])

0 notes on commit 751b007

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