Rev 4796: First cut at testtools support: rename, remove TestCase.run() and change testcase tests to not assume the same instance runs (for cleaner testing at this point). in http://bazaar.launchpad.net/~lifeless/bzr/subunit
Robert Collins
robertc at robertcollins.net
Sat Dec 5 08:36:20 GMT 2009
At http://bazaar.launchpad.net/~lifeless/bzr/subunit
------------------------------------------------------------
revno: 4796
revision-id: robertc at robertcollins.net-20091205083555-6bvltc95cwsl27v4
parent: robertc at robertcollins.net-20091112073822-regtsaqzfbonmafc
committer: Robert Collins <robertc at robertcollins.net>
branch nick: subunit
timestamp: Sat 2009-12-05 19:35:55 +1100
message:
First cut at testtools support: rename, remove TestCase.run() and change testcase tests to not assume the same instance runs (for cleaner testing at this point).
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py 2009-11-12 07:38:22 +0000
+++ b/bzrlib/tests/__init__.py 2009-12-05 08:35:55 +0000
@@ -657,8 +657,7 @@
% (type(suite), suite))
-class TestSkipped(Exception):
- """Indicates that a test was intentionally skipped, rather than failing."""
+TestSkipped = testtools.testcase.TestSkipped
class TestNotApplicable(TestSkipped):
@@ -788,20 +787,22 @@
_keep_log_file = False
# record lsprof data when performing benchmark calls.
_gather_lsprof_in_benchmarks = False
- attrs_to_keep = ('id', '_testMethodName', '_testMethodDoc',
- '_log_contents', '_log_file_name', '_benchtime',
- '_TestCase__testMethodName', '_TestCase__testMethodDoc',)
def __init__(self, methodName='testMethod'):
super(TestCase, self).__init__(methodName)
self._cleanups = []
- self._bzr_test_setUp_run = False
- self._bzr_test_tearDown_run = False
self._directory_isolation = True
+ self.exception_handlers.insert(0,
+ (UnavailableFeature, self._do_unsupported_or_skip))
+ self.exception_handlers.insert(0,
+ (TestNotApplicable, self._do_not_applicable))
+ self.exception_handlers.insert(0,
+ (KnownFailure, self._do_known_failure))
def setUp(self):
- unittest.TestCase.setUp(self)
- self._bzr_test_setUp_run = True
+ super(TestCase, self).setUp()
+ for feature in getattr(self, '_test_needs_features', []):
+ self.requireFeature(feature)
self._cleanEnvironment()
self._silenceUI()
self._startLogFile()
@@ -1571,7 +1572,8 @@
else:
addSkip(self, reason)
- def _do_known_failure(self, result):
+ @staticmethod
+ def _do_known_failure(self, result, e):
err = sys.exc_info()
addExpectedFailure = getattr(result, 'addExpectedFailure', None)
if addExpectedFailure is not None:
@@ -1579,6 +1581,7 @@
else:
result.addSuccess(self)
+ @staticmethod
def _do_not_applicable(self, result, e):
if not e.args:
reason = 'No reason given'
@@ -1590,14 +1593,16 @@
else:
self._do_skip(result, reason)
- def _do_unsupported_or_skip(self, result, reason):
+ @staticmethod
+ def _do_unsupported_or_skip(self, result, e):
+ reason = e.args[0]
addNotSupported = getattr(result, 'addNotSupported', None)
if addNotSupported is not None:
result.addNotSupported(self, reason)
else:
self._do_skip(result, reason)
- def run(self, result=None):
+ def _run(self, result=None):
if result is None: result = self.defaultTestResult()
result.startTest(self)
try:
@@ -1607,9 +1612,6 @@
result.stopTest(self)
def _run(self, result):
- for feature in getattr(self, '_test_needs_features', []):
- if not feature.available():
- return self._do_unsupported_or_skip(result, feature)
try:
absent_attr = object()
# Python 2.5
@@ -1692,17 +1694,11 @@
self._runCleanups()
raise
finally:
- saved_attrs = {}
- for attr_name in self.attrs_to_keep:
- if attr_name in self.__dict__:
- saved_attrs[attr_name] = self.__dict__[attr_name]
- self.__dict__ = saved_attrs
+ pass
def tearDown(self):
- self._runCleanups()
self._log_contents = ''
- self._bzr_test_tearDown_run = True
- unittest.TestCase.tearDown(self)
+ super(TestCase, self).tearDown()
def time(self, callable, *args, **kwargs):
"""Run callable and accrue the time it takes to the benchmark time.
@@ -1726,7 +1722,7 @@
finally:
self._benchtime += time.time() - start
- def _runCleanups(self):
+ def __runCleanups(self):
"""Run registered cleanup functions.
This should only be called from TestCase.tearDown.
=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py 2009-11-12 07:38:22 +0000
+++ b/bzrlib/tests/test_selftest.py 2009-12-05 08:35:55 +0000
@@ -24,6 +24,9 @@
import unittest
import warnings
+import testtools.tests.helpers
+from testtools import MultiTestResult
+
import bzrlib
from bzrlib import (
branchbuilder,
@@ -679,7 +682,7 @@
def test_profiles_tests(self):
self.requireFeature(test_lsprof.LSProfFeature)
- terminal = unittest.TestResult()
+ terminal = testtools.tests.helpers.ExtendedTestResult()
result = tests.ProfileResult(terminal)
class Sample(tests.TestCase):
def a(self):
@@ -687,11 +690,11 @@
def sample_function(self):
pass
test = Sample("a")
- test.attrs_to_keep = test.attrs_to_keep + ('_benchcalls',)
test.run(result)
- self.assertLength(1, test._benchcalls)
+ case = terminal._events[0][1]
+ self.assertLength(1, case._benchcalls)
# We must be able to unpack it as the test reporting code wants
- (_, _, _), stats = test._benchcalls[0]
+ (_, _, _), stats = case._benchcalls[0]
self.assertTrue(callable(stats.pprint))
@@ -702,8 +705,10 @@
descriptions=0,
verbosity=1,
)
- test_case.run(result)
- timed_string = result._testTimeString(test_case)
+ capture = testtools.tests.helpers.ExtendedTestResult()
+ test_case.run(MultiTestResult(result, capture))
+ run_case = capture._events[0][1]
+ timed_string = result._testTimeString(run_case)
self.assertContainsRe(timed_string, expected_re)
def test_test_reporting(self):
@@ -834,7 +839,7 @@
test.run(result)
# it should invoke 'report_known_failure'.
self.assertEqual(2, len(result._call))
- self.assertEqual(test, result._call[0])
+ self.assertEqual(test.id(), result._call[0].id())
self.assertEqual(tests.KnownFailure, result._call[1][0])
self.assertIsInstance(result._call[1][1], tests.KnownFailure)
# we dont introspec the traceback, if the rest is ok, it would be
@@ -934,7 +939,7 @@
test.run(result)
# it should invoke 'addNotSupported'.
self.assertEqual(2, len(result._call))
- self.assertEqual(test, result._call[0])
+ self.assertEqual(test.id(), result._call[0].id())
self.assertEqual(feature, result._call[1])
# and not count as an error
self.assertEqual(0, result.error_count)
@@ -1268,8 +1273,8 @@
self.assertLogDeleted(test)
- def test_fail_log_kept(self):
- """Failed tests have their log kept"""
+ def test_fail_log_shown(self):
+ """Failed tests have their log shown"""
class LogTester(tests.TestCase):
@@ -1286,12 +1291,8 @@
self.assertContainsRe(text, 'this will be kept')
self.assertContainsRe(text, 'this test fails')
- log = test._get_log()
- self.assertContainsRe(log, 'this will be kept')
- self.assertEqual(log, test._log_contents)
-
- def test_error_log_kept(self):
- """Tests with errors have their log kept"""
+ def test_error_log_shown(self):
+ """Tests with errors have their log shown"""
class LogTester(tests.TestCase):
@@ -1308,10 +1309,6 @@
self.assertContainsRe(text, 'this will be kept')
self.assertContainsRe(text, 'random exception raised')
- log = test._get_log()
- self.assertContainsRe(log, 'this will be kept')
- self.assertEqual(log, test._log_contents)
-
def test_startTestRun(self):
"""run should call result.startTestRun()"""
calls = []
@@ -1494,6 +1491,7 @@
result = self.make_test_result()
self.inner_test.run(result)
note("outer finish")
+ self.addCleanup(osutils.delete_any, self._log_file_name)
def test_trace_nesting(self):
# this tests that each test case nests its trace facility correctly.
@@ -1511,7 +1509,6 @@
outer_test = TestTestCase("outer_child")
result = self.make_test_result()
outer_test.run(result)
- self.addCleanup(osutils.delete_any, outer_test._log_file_name)
self.assertEqual(original_trace, bzrlib.trace._trace_file)
def method_that_times_a_bit_twice(self):
@@ -1632,6 +1629,8 @@
"""Test disabled tests behaviour with support aware results."""
test = SampleTestCase('_test_pass')
class DisabledFeature(object):
+ def __eq__(self, other):
+ return isinstance(other, DisabledFeature)
def available(self):
return False
the_feature = DisabledFeature()
@@ -1648,10 +1647,11 @@
self.calls.append(('addNotSupported', test, feature))
result = InstrumentedTestResult()
test.run(result)
+ case = result.calls[0][1]
self.assertEqual([
- ('startTest', test),
- ('addNotSupported', test, the_feature),
- ('stopTest', test),
+ ('startTest', case),
+ ('addNotSupported', case, the_feature),
+ ('stopTest', case),
],
result.calls)
More information about the bazaar-commits
mailing list