329
329
We make selective use of doctests__. In general they should provide
330
330
*examples* within the API documentation which can incidentally be tested. We
331
331
don't try to test every important case using doctests |--| regular Python
332
tests are generally a better solution. That is, we just use doctests to
333
make our documentation testable, rather than as a way to make tests.
332
tests are generally a better solution. That is, we just use doctests to make
333
our documentation testable, rather than as a way to make tests. Be aware that
334
doctests are not as well isolated as the unit tests, if you need more
335
isolation, you're likely want to write unit tests anyway if only to get a
336
better control of the test environment.
335
338
Most of these are in ``bzrlib/doc/api``. More additions are welcome.
969
972
Please see bzrlib.treebuilder for more details.
977
PreviewTrees are based on TreeTransforms. This means they can represent
978
virtually any state that a WorkingTree can have, including unversioned files.
979
They can be used to test the output of anything that produces TreeTransforms,
980
such as merge algorithms and revert. They can also be used to test anything
981
that takes arbitrary Trees as its input.
985
# Get an empty tree to base the transform on.
986
b = self.make_branch('.')
987
empty_tree = b.repository.revision_tree(_mod_revision.NULL_REVISION)
988
tt = TransformPreview(empty_tree)
989
self.addCleanup(tt.finalize)
990
# Empty trees don't have a root, so add it first.
991
root = tt.new_directory('', ROOT_PARENT, 'tree-root')
992
# Set the contents of a file.
993
tt.new_file('new-file', root, 'contents', 'file-id')
994
preview = tt.get_preview_tree()
996
self.assertEqual('contents', preview.get_file_text('file-id'))
998
PreviewTrees can stack, with each tree falling back to the previous::
1000
tt2 = TransformPreview(preview)
1001
self.addCleanup(tt2.finalize)
1002
tt2.new_file('new-file2', tt2.root, 'contents2', 'file-id2')
1003
preview2 = tt2.get_preview_tree()
1004
self.assertEqual('contents', preview2.get_file_text('file-id'))
1005
self.assertEqual('contents2', preview2.get_file_text('file-id2'))
972
1008
Temporarily changing state
973
1009
~~~~~~~~~~~~~~~~~~~~~~~~~~
978
1014
self.overrideAttr(osutils, '_cached_user_encoding', 'latin-1')
1016
Temporarily changing environment variables
1017
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1019
If yout test needs to temporarily change some environment variable value
1020
(which generally means you want it restored at the end), you can use::
1022
self.overrideEnv('BZR_ENV_VAR', 'new_value')
1024
If you want to remove a variable from the environment, you should use the
1025
special ``None`` value::
1027
self.overrideEnv('PATH', None)
1029
If you add a new feature which depends on a new environment variable, make
1030
sure it behaves properly when this variable is not defined (if applicable) and
1031
if you need to enforce a specific default value, check the
1032
``TestCase._cleanEnvironment`` in ``bzrlib.tests.__init__.py`` which defines a
1033
proper set of values for all tests.