Rev 5579: Some test infrastructure for tests.DocTestSuite. in file:///home/vila/src/bzr/bugs/test-isolation/

Vincent Ladeuil v.ladeuil+lp at free.fr
Sat Dec 18 12:43:15 GMT 2010


At file:///home/vila/src/bzr/bugs/test-isolation/

------------------------------------------------------------
revno: 5579
revision-id: v.ladeuil+lp at free.fr-20101218124315-mvi2ohaixgtk1o6o
parent: v.ladeuil+lp at free.fr-20101218102218-rw1z76p1oosoygus
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: even-better-isolation
timestamp: Sat 2010-12-18 13:43:15 +0100
message:
  Some test infrastructure for tests.DocTestSuite.
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2010-12-18 10:19:25 +0000
+++ b/bzrlib/tests/__init__.py	2010-12-18 12:43:15 +0000
@@ -881,6 +881,17 @@
         return NullProgressView()
 
 
+
+def DocTestSuite(*args, **kwargs):
+    """Overrides doctest.DocTestSuite to handle isolation.
+
+    The method is really a factory and users are expected to use it as such.
+    """
+#    kwargs['setUp'] = override_os_environ
+#    kwargs['tearDown'] = restore_os_environ
+    return doctest.DocTestSuite(*args, **kwargs)
+
+
 class TestCase(testtools.TestCase):
     """Base class for bzr unit tests.
 

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2010-12-18 10:19:25 +0000
+++ b/bzrlib/tests/test_selftest.py	2010-12-18 12:43:15 +0000
@@ -17,7 +17,7 @@
 """Tests for the test framework."""
 
 from cStringIO import StringIO
-from doctest import ELLIPSIS
+import doctest
 import os
 import signal
 import sys
@@ -92,7 +92,7 @@
             "text", "plain", {"charset": "utf8"})))
         self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
         self.assertThat(self.get_log(),
-            DocTestMatches(u"...a test message\n", ELLIPSIS))
+            DocTestMatches(u"...a test message\n", doctest.ELLIPSIS))
 
 
 class TestUnicodeFilename(tests.TestCase):
@@ -3508,3 +3508,38 @@
         self.assertTrue('LINES' not in os.environ)
         tests.restore_os_environ(test)
         self.assertEquals('25', os.environ['LINES'])
+
+
+class TestDocTestSuiteIsolation(tests.TestCase):
+
+    def get_doctest_suite(self, klass, string):
+        class Finder(doctest.DocTestFinder):
+
+            def find(*args, **kwargs):
+                test = doctest.DocTestParser().get_doctest(
+                    string, {}, 'foo', 'foo.py', 0)
+                return [test]
+
+        suite = klass(test_finder=Finder())
+        return suite
+
+    def assertDocTestStringSucceds(self, klass, string):
+        suite = self.get_doctest_suite(klass, string)
+        output = StringIO()
+        result = tests.TextTestResult(output, 0, 1)
+        suite.run(result)
+        if not result.wasStrictlySuccessful():
+            self.fail(output.getvalue())
+
+    def test_injected_variable(self):
+        self.assertDocTestStringSucceds(tests.DocTestSuite, """
+            >>> import os
+            >>> os.environ['LINES']
+            '25'
+            """)
+
+    def test_deleted_variable(self):
+        self.assertDocTestStringSucceds(tests.DocTestSuite, """
+            >>> import os
+            >>> os.environ.get('BZR_HOME')
+            """)



More information about the bazaar-commits mailing list