Rev 5580: Test tests.DocTestSuite, using doctest.DocTestSuite as a reference point. in file:///home/vila/src/bzr/bugs/test-isolation/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Sat Dec 18 15:21:54 GMT 2010
At file:///home/vila/src/bzr/bugs/test-isolation/
------------------------------------------------------------
revno: 5580
revision-id: v.ladeuil+lp at free.fr-20101218152154-1iav50e5udef6tjt
parent: v.ladeuil+lp at free.fr-20101218124315-mvi2ohaixgtk1o6o
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: even-better-isolation
timestamp: Sat 2010-12-18 16:21:54 +0100
message:
Test tests.DocTestSuite, using doctest.DocTestSuite as a reference point.
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py 2010-12-18 12:43:15 +0000
+++ b/bzrlib/tests/__init__.py 2010-12-18 15:21:54 +0000
@@ -887,8 +887,8 @@
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
+ kwargs['setUp'] = override_os_environ
+ kwargs['tearDown'] = restore_os_environ
return doctest.DocTestSuite(*args, **kwargs)
=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py 2010-12-18 12:43:15 +0000
+++ b/bzrlib/tests/test_selftest.py 2010-12-18 15:21:54 +0000
@@ -3511,8 +3511,19 @@
class TestDocTestSuiteIsolation(tests.TestCase):
-
- def get_doctest_suite(self, klass, string):
+ """Test that `tests.DocTestSuite` isolates doc tests from os.environ.
+
+ Since tests.TestCase alreay provides an isolation from os.environ, we use
+ the clean environment as a base for testing. To precisely capture the
+ isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
+ compare against.
+
+ We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
+ not `os.environ` so each test overrides it to suit its needs.
+
+ """
+
+ def get_doctest_suite_for_string(self, klass, string):
class Finder(doctest.DocTestFinder):
def find(*args, **kwargs):
@@ -3523,23 +3534,42 @@
suite = klass(test_finder=Finder())
return suite
+ def run_doctest_suite_for_string(self, klass, string):
+ suite = self.get_doctest_suite_for_string(klass, string)
+ output = StringIO()
+ result = tests.TextTestResult(output, 0, 1)
+ suite.run(result)
+ return result, output
+
def assertDocTestStringSucceds(self, klass, string):
- suite = self.get_doctest_suite(klass, string)
- output = StringIO()
- result = tests.TextTestResult(output, 0, 1)
- suite.run(result)
+ result, output = self.run_doctest_suite_for_string(klass, string)
if not result.wasStrictlySuccessful():
self.fail(output.getvalue())
+ def assertDocTestStringFails(self, klass, string):
+ result, output = self.run_doctest_suite_for_string(klass, string)
+ if result.wasStrictlySuccessful():
+ self.fail(output.getvalue())
+
def test_injected_variable(self):
- self.assertDocTestStringSucceds(tests.DocTestSuite, """
+ self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
+ test = """
>>> import os
>>> os.environ['LINES']
- '25'
- """)
+ '42'
+ """
+ # doctest.DocTestSuite fails as it sees '25'
+ self.assertDocTestStringFails(doctest.DocTestSuite, test)
+ # tests.DocTestSuite sees '42'
+ self.assertDocTestStringSucceds(tests.DocTestSuite, test)
def test_deleted_variable(self):
- self.assertDocTestStringSucceds(tests.DocTestSuite, """
+ self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
+ test = """
>>> import os
- >>> os.environ.get('BZR_HOME')
- """)
+ >>> os.environ.get('LINES')
+ """
+ # doctest.DocTestSuite fails as it sees '25'
+ self.assertDocTestStringFails(doctest.DocTestSuite, test)
+ # tests.DocTestSuite sees None
+ self.assertDocTestStringSucceds(tests.DocTestSuite, test)
More information about the bazaar-commits
mailing list