~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to doc/developers/testing.txt

  • Committer: Martin Pool
  • Date: 2011-01-20 23:07:25 UTC
  • mfrom: (5626 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5630.
  • Revision ID: mbp@canonical.com-20110120230725-12l7ltnko5x3fgnz
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
339
339
 
340
340
  __ http://docs.python.org/lib/module-doctest.html
341
341
 
342
 
There is an `assertDoctestExampleMatches` method in
343
 
`bzrlib.tests.TestCase` that allows you to match against doctest-style
344
 
string templates (including ``...`` to skip sections) from regular Python
345
 
tests.
346
 
 
347
342
 
348
343
Shell-like tests
349
344
----------------
516
511
regress.
517
512
 
518
513
This is done by running the command in a subprocess with
519
 
``PYTHON_VERBOSE=1``.  Starting a whole Python interpreter is pretty slow,
520
 
so we don't want exhaustive testing here, but just enough to guard against
521
 
distinct fixed problems.
 
514
``--profile-imports``.  Starting a whole Python interpreter is pretty
 
515
slow, so we don't want exhaustive testing here, but just enough to guard
 
516
against distinct fixed problems.
522
517
 
523
518
Assertions about precisely what is loaded tend to be brittle so we instead
524
519
make assertions that particular things aren't loaded.
745
740
        _test_needs_features = [features.apport]
746
741
 
747
742
 
748
 
Testing translations
749
 
-----------------------
750
 
 
751
 
Translations are disabled by default in tests.  If you want to test
752
 
that code is translated you can use the ``ZzzTranslations`` class from
753
 
``test_i18n``::
754
 
 
755
 
    self.overrideAttr(i18n, '_translations', ZzzTranslations())
756
 
 
757
 
And check the output strings look like ``u"zz\xe5{{output}}"``.
758
 
 
759
 
To test the gettext setup and usage you override i18n.installed back
760
 
to self.i18nInstalled and _translations to None, see
761
 
test_i18n.TestInstall.
762
 
 
763
 
 
764
743
Testing deprecated code
765
744
-----------------------
766
745
 
992
971
 
993
972
Please see bzrlib.treebuilder for more details.
994
973
 
995
 
PreviewTree
996
 
~~~~~~~~~~~
997
 
 
998
 
PreviewTrees are based on TreeTransforms.  This means they can represent
999
 
virtually any state that a WorkingTree can have, including unversioned files.
1000
 
They can be used to test the output of anything that produces TreeTransforms,
1001
 
such as merge algorithms and revert.  They can also be used to test anything
1002
 
that takes arbitrary Trees as its input.
1003
 
 
1004
 
::
1005
 
 
1006
 
  # Get an empty tree to base the transform on.
1007
 
  b = self.make_branch('.')
1008
 
  empty_tree = b.repository.revision_tree(_mod_revision.NULL_REVISION)
1009
 
  tt = TransformPreview(empty_tree)
1010
 
  self.addCleanup(tt.finalize)
1011
 
  # Empty trees don't have a root, so add it first.
1012
 
  root = tt.new_directory('', ROOT_PARENT, 'tree-root')
1013
 
  # Set the contents of a file.
1014
 
  tt.new_file('new-file', root, 'contents', 'file-id')
1015
 
  preview = tt.get_preview_tree()
1016
 
  # Test the contents.
1017
 
  self.assertEqual('contents', preview.get_file_text('file-id'))
1018
 
 
1019
 
PreviewTrees can stack, with each tree falling back to the previous::
1020
 
 
1021
 
  tt2 = TransformPreview(preview)
1022
 
  self.addCleanup(tt2.finalize)
1023
 
  tt2.new_file('new-file2', tt2.root, 'contents2', 'file-id2')
1024
 
  preview2 = tt2.get_preview_tree()
1025
 
  self.assertEqual('contents', preview2.get_file_text('file-id'))
1026
 
  self.assertEqual('contents2', preview2.get_file_text('file-id2'))
1027
 
 
1028
974
 
1029
975
Temporarily changing state
1030
976
~~~~~~~~~~~~~~~~~~~~~~~~~~
1034
980
 
1035
981
    self.overrideAttr(osutils, '_cached_user_encoding', 'latin-1')
1036
982
 
1037
 
This should be used with discretion; sometimes it's better to make the
1038
 
underlying code more testable so that you don't need to rely on monkey
1039
 
patching.
1040
 
 
1041
 
 
1042
 
Observing calls to a function
1043
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1044
 
 
1045
 
Sometimes it's useful to observe how a function is called, typically when
1046
 
calling it has side effects but the side effects are not easy to observe
1047
 
from a test case.  For instance the function may be expensive and we want
1048
 
to assert it is not called too many times, or it has effects on the
1049
 
machine that are safe to run during a test but not easy to measure.  In
1050
 
these cases, you can use `recordCalls` which will monkey-patch in a
1051
 
wrapper that records when the function is called.
1052
 
 
1053
 
 
1054
983
Temporarily changing environment variables
1055
984
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1056
985
 
1105
1034
 
1106
1035
    tc qdisc add dev lo root handle 1: prio
1107
1036
    tc qdisc add dev lo parent 1:3 handle 30: netem delay 500ms 
1108
 
    tc filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip dport 4155 0xffff flowid 1:3 
1109
 
    tc filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip sport 4155 0xffff flowid 1:3 
 
1037
    tc qdisc add dev lo parent 30:1 handle 40: prio
 
1038
    tc filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip dport 4155 0xffff flowid 1:3 handle 800::800
 
1039
    tc filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip sport 4155 0xffff flowid 1:3 handle 800::801
1110
1040
 
1111
1041
and to remove this::
1112
1042