163
163
self.unsupported = {}
165
165
self._overall_start_time = time.time()
167
167
def _extractBenchmarkTime(self, testCase):
168
168
"""Add a benchmark time for the current test case."""
169
169
return getattr(testCase, "_benchtime", None)
171
171
def _elapsedTestTimeString(self):
172
172
"""Return a time string for the overall time the current test has taken."""
173
173
return self._formatTime(time.time() - self._start_time)
277
277
"""The test will not be run because of a missing feature.
279
279
# this can be called in two different ways: it may be that the
280
# test started running, and then raised (through addError)
280
# test started running, and then raised (through addError)
281
281
# UnavailableFeature. Alternatively this method can be called
282
282
# while probing for features before running the tests; in that
283
283
# case we will see startTest and stopTest, but the test will never
396
396
self._progress_prefix_text()
398
398
+ self._shortened_test_description(test))
400
400
def _test_description(self, test):
401
401
return self._shortened_test_description(test)
403
403
def report_error(self, test, err):
404
self.pb.note('ERROR: %s\n %s\n',
404
self.pb.note('ERROR: %s\n %s\n',
405
405
self._test_description(test),
409
409
def report_failure(self, test, err):
410
self.pb.note('FAIL: %s\n %s\n',
410
self.pb.note('FAIL: %s\n %s\n',
411
411
self._test_description(test),
425
425
def report_unsupported(self, test, feature):
426
426
"""test cannot be run because feature is missing."""
428
428
def report_cleaning_up(self):
429
429
self.pb.update('cleaning up...')
602
602
class TestNotApplicable(TestSkipped):
603
603
"""A test is not applicable to the situation where it was run.
605
This is only normally raised by parameterized tests, if they find that
606
the instance they're constructed upon does not support one aspect
605
This is only normally raised by parameterized tests, if they find that
606
the instance they're constructed upon does not support one aspect
607
607
of its interface.
632
632
class StringIOWrapper(object):
633
633
"""A wrapper around cStringIO which just adds an encoding attribute.
635
635
Internally we can check sys.stdout to see what the output encoding
636
636
should be. However, cStringIO has no encoding attribute that we can
637
637
set. So we wrap it instead.
729
729
class TestCase(unittest.TestCase):
730
730
"""Base class for bzr unit tests.
732
Tests that need access to disk resources should subclass
732
Tests that need access to disk resources should subclass
733
733
TestCaseInTempDir not TestCase.
735
735
Error and debug log messages are redirected from their usual
737
737
retrieved by _get_log(). We use a real OS file, not an in-memory object,
738
738
so that it can also capture file IO. When the test completes this file
739
739
is read into memory and removed from disk.
741
741
There are also convenience functions to invoke bzr's command-line
742
742
routine, and to build and check bzr trees.
744
744
In addition to the usual method of overriding tearDown(), this class also
745
745
allows subclasses to register functions into the _cleanups list, which is
746
746
run in order as the object is torn down. It's less likely this will be
861
861
def assertEqualDiff(self, a, b, message=None):
862
862
"""Assert two texts are equal, if not raise an exception.
864
This is intended for use with multi-line strings where it can
864
This is intended for use with multi-line strings where it can
865
865
be hard to find the differences by eye.
867
867
# TODO: perhaps override assertEquals to call this for strings?
875
875
message = 'second string is missing a final newline.\n'
876
876
raise AssertionError(message +
877
877
self._ndiff_strings(a, b))
879
879
def assertEqualMode(self, mode, mode_test):
880
880
self.assertEqual(mode, mode_test,
881
881
'mode mismatch %o != %o' % (mode, mode_test))
940
940
def assertListRaises(self, excClass, func, *args, **kwargs):
941
941
"""Fail unless excClass is raised when the iterator from func is used.
943
943
Many functions can return generators this makes sure
944
944
to wrap them in a list() call to make sure the whole generator
945
945
is run, and that the proper exception is raised.
994
994
def assertTransportMode(self, transport, path, mode):
995
995
"""Fail if a path does not have mode mode.
997
997
If modes are not supported on this transport, the assertion is ignored.
999
999
if not transport._can_roundtrip_unix_modebits():
1170
1170
def callDeprecated(self, expected, callable, *args, **kwargs):
1171
1171
"""Assert that a callable is deprecated in a particular way.
1173
This is a very precise test for unusual requirements. The
1173
This is a very precise test for unusual requirements. The
1174
1174
applyDeprecated helper function is probably more suited for most tests
1175
1175
as it allows you to simply specify the deprecation format being used
1176
1176
and will ensure that that is issued for the function being called.
1221
1221
def addCleanup(self, callable, *args, **kwargs):
1222
1222
"""Arrange to run a callable when this case is torn down.
1224
Callables are run in the reverse of the order they are registered,
1224
Callables are run in the reverse of the order they are registered,
1225
1225
ie last-in first-out.
1227
1227
self._cleanups.append((callable, args, kwargs))
1311
1311
def time(self, callable, *args, **kwargs):
1312
1312
"""Run callable and accrue the time it takes to the benchmark time.
1314
1314
If lsprofiling is enabled (i.e. by --lsprof-time to bzr selftest) then
1315
1315
this will cause lsprofile statistics to be gathered and stored in
1316
1316
self._benchcalls.
1331
1331
self._benchtime += time.time() - start
1333
1333
def _runCleanups(self):
1334
"""Run registered cleanup functions.
1334
"""Run registered cleanup functions.
1336
1336
This should only be called from TestCase.tearDown.
1338
# TODO: Perhaps this should keep running cleanups even if
1338
# TODO: Perhaps this should keep running cleanups even if
1339
1339
# one of them fails?
1341
1341
# Actually pop the cleanups from the list so tearDown running
1457
1457
passed in three ways:
1459
1459
1- A list of strings, eg ["commit", "a"]. This is recommended
1460
when the command contains whitespace or metacharacters, or
1460
when the command contains whitespace or metacharacters, or
1461
1461
is built up at run time.
1463
2- A single string, eg "add a". This is the most convenient
1463
2- A single string, eg "add a". This is the most convenient
1464
1464
for hardcoded commands.
1466
1466
This runs bzr through the interface that catches and reports
1525
1525
def run_bzr_subprocess(self, *args, **kwargs):
1526
1526
"""Run bzr in a subprocess for testing.
1528
This starts a new Python interpreter and runs bzr in there.
1528
This starts a new Python interpreter and runs bzr in there.
1529
1529
This should only be used for tests that have a justifiable need for
1530
1530
this isolation: e.g. they are testing startup time, or signal
1531
handling, or early startup code, etc. Subprocess code can't be
1531
handling, or early startup code, etc. Subprocess code can't be
1532
1532
profiled or debugged so easily.
1534
1534
:keyword retcode: The status code that is expected. Defaults to 0. If
1775
1775
def __init__(self, methodName='runTest'):
1776
1776
# allow test parameterization after test construction and before test
1777
# execution. Variables that the parameterizer sets need to be
1777
# execution. Variables that the parameterizer sets need to be
1778
1778
# ones that are not set by setUp, or setUp will trash them.
1779
1779
super(TestCaseWithMemoryTransport, self).__init__(methodName)
1780
1780
self.vfs_transport_factory = default_transport
1797
1797
def get_readonly_transport(self, relpath=None):
1798
1798
"""Return a readonly transport for the test scratch space
1800
1800
This can be used to test that operations which should only need
1801
1801
readonly access in fact do not try to write.
1833
1833
def get_readonly_url(self, relpath=None):
1834
1834
"""Get a URL for the readonly transport.
1836
This will either be backed by '.' or a decorator to the transport
1836
This will either be backed by '.' or a decorator to the transport
1837
1837
used by self.get_url()
1838
1838
relpath provides for clients to get a path relative to the base url.
1839
1839
These should only be downwards relative, not upwards.
1973
1973
def makeAndChdirToTestDir(self):
1974
1974
"""Create a temporary directories for this one test.
1976
1976
This must set self.test_home_dir and self.test_dir and chdir to
1979
1979
For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1981
1981
os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1982
1982
self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1983
1983
self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1985
1985
def make_branch(self, relpath, format=None):
1986
1986
"""Create a branch on the transport at relpath."""
1987
1987
repo = self.make_repository(relpath, format=format)
2006
2006
def make_repository(self, relpath, shared=False, format=None):
2007
2007
"""Create a repository on our default transport at relpath.
2009
2009
Note that relpath must be a relative path, not a full url.
2011
2011
# FIXME: If you create a remoterepository this returns the underlying
2012
# real format, which is incorrect. Actually we should make sure that
2012
# real format, which is incorrect. Actually we should make sure that
2013
2013
# RemoteBzrDir returns a RemoteRepository.
2014
2014
# maybe mbp 20070410
2015
2015
made_control = self.make_bzrdir(relpath, format=format)
2028
2028
def overrideEnvironmentForTesting(self):
2029
2029
os.environ['HOME'] = self.test_home_dir
2030
2030
os.environ['BZR_HOME'] = self.test_home_dir
2032
2032
def setUp(self):
2033
2033
super(TestCaseWithMemoryTransport, self).setUp()
2034
2034
self._make_test_root()
2066
2066
All test cases create their own directory within that. If the
2067
2067
tests complete successfully, the directory is removed.
2069
:ivar test_base_dir: The path of the top-level directory for this
2069
:ivar test_base_dir: The path of the top-level directory for this
2070
2070
test, which contains a home directory and a work directory.
2072
2072
:ivar test_home_dir: An initially empty directory under test_base_dir
2099
2099
def makeAndChdirToTestDir(self):
2100
2100
"""See TestCaseWithMemoryTransport.makeAndChdirToTestDir().
2102
2102
For TestCaseInTempDir we create a temporary directory based on the test
2103
2103
name and then create two subdirs - test and home under it.
2203
2203
ReadonlyTransportDecorator is used instead which allows the use of non disk
2204
2204
based read write transports.
2206
If an explicit class is provided for readonly access, that server and the
2206
If an explicit class is provided for readonly access, that server and the
2207
2207
readwrite one must both define get_url() as resolving to os.getcwd().
2321
2321
def condition_isinstance(klass_or_klass_list):
2322
2322
"""Create a condition filter which returns isinstance(param, klass).
2324
2324
:return: A callable which when called with one parameter obj return the
2325
2325
result of isinstance(obj, klass_or_klass_list).
2332
2332
def condition_id_in_list(id_list):
2333
2333
"""Create a condition filter which verify that test's id in a list.
2335
2335
:param id_list: A TestIdList object.
2336
2336
:return: A callable that returns True if the test's id appears in the list.
2343
2343
def condition_id_startswith(starts):
2344
2344
"""Create a condition filter verifying that test's id starts with a string.
2346
2346
:param starts: A list of string.
2347
:return: A callable that returns True if the test's id starts with one of
2347
:return: A callable that returns True if the test's id starts with one of
2348
2348
the given strings.
2350
2350
def condition(test):
2374
2374
def filter_suite_by_condition(suite, condition):
2375
2375
"""Create a test suite by filtering another one.
2377
2377
:param suite: The source suite.
2378
2378
:param condition: A callable whose result evaluates True when called with a
2379
2379
test case which should be included in the result.
2390
2390
def filter_suite_by_re(suite, pattern):
2391
2391
"""Create a test suite by filtering another one.
2393
2393
:param suite: the source suite
2394
2394
:param pattern: pattern that names must match
2395
2395
:returns: the newly created suite
2448
2448
def randomize_suite(suite):
2449
2449
"""Return a new TestSuite with suite's tests in random order.
2451
2451
The tests in the input suite are flattened into a single suite in order to
2452
2452
accomplish this. Any nested TestSuites are removed to provide global
2460
2460
def split_suite_by_condition(suite, condition):
2461
2461
"""Split a test suite into two by a condition.
2463
2463
:param suite: The suite to split.
2464
2464
:param condition: The condition to match on. Tests that match this
2465
2465
condition are returned in the first test suite, ones that do not match
2482
2482
def split_suite_by_re(suite, pattern):
2483
2483
"""Split a test suite into two by a regular expression.
2485
2485
:param suite: The suite to split.
2486
2486
:param pattern: A regular expression string. Test ids that match this
2487
2487
pattern will be in the first test suite returned, and the others in the
2650
2650
"""Warns about tests not appearing or appearing more than once.
2652
2652
:param test_suite: A TestSuite object.
2653
:param test_id_list: The list of test ids that should be found in
2653
:param test_id_list: The list of test ids that should be found in
2656
2656
:return: (absents, duplicates) absents is a list containing the test found
3005
3005
# No tests to keep here, move along
3008
# note that this really does mean "report only" -- doctest
3008
# note that this really does mean "report only" -- doctest
3009
3009
# still runs the rest of the examples
3010
3010
doc_suite = doctest.DocTestSuite(mod,
3011
3011
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
3066
3066
This is the recommended public interface for test parameterization.
3067
3067
Typically the test_suite() method for a per-implementation test
3068
suite will call multiply_tests_from_modules and return the
3068
suite will call multiply_tests_from_modules and return the
3071
3071
:param module_name_list: List of fully-qualified names of test
3073
:param scenario_iter: Iterable of pairs of (scenario_name,
3073
:param scenario_iter: Iterable of pairs of (scenario_name,
3074
3074
scenario_param_dict).
3075
:param loader: If provided, will be used instead of a new
3075
:param loader: If provided, will be used instead of a new
3076
3076
bzrlib.tests.TestLoader() instance.
3078
3078
This returns a new TestSuite containing the cross product of
3079
3079
all the tests in all the modules, each repeated for each scenario.
3080
Each test is adapted by adding the scenario name at the end
3080
Each test is adapted by adding the scenario name at the end
3081
3081
of its name, and updating the test object's __dict__ with the
3082
3082
scenario_param_dict.
3084
3084
>>> r = multiply_tests_from_modules(
3085
3085
... ['bzrlib.tests.test_sampler'],
3086
... [('one', dict(param=1)),
3086
... [('one', dict(param=1)),
3087
3087
... ('two', dict(param=2))])
3088
3088
>>> tests = list(iter_suite_tests(r))