~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to doc/developers/testing.txt

  • Committer: Vincent Ladeuil
  • Date: 2010-02-11 09:27:55 UTC
  • mfrom: (5017.3.46 test-servers)
  • mto: This revision was merged to the branch mainline in revision 5030.
  • Revision ID: v.ladeuil+lp@free.fr-20100211092755-3vvu4vbwiwjjte3s
Move tests servers from bzrlib.transport to bzrlib.tests.test_server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
=======================
2
 
Guide to Testing Bazaar
3
 
=======================
4
 
 
5
 
.. contents::
6
 
 
7
 
Testing Bazaar
8
 
##############
 
1
====================
 
2
Bazaar Testing Guide
 
3
====================
 
4
 
9
5
 
10
6
The Importance of Testing
11
7
=========================
34
30
down the track do not break new features or bug fixes that you are
35
31
contributing today.
36
32
 
37
 
As of May 2008, Bazaar ships with a test suite containing over 12000 tests
38
 
and growing. We are proud of it and want to remain so. As community
39
 
members, we all benefit from it. Would you trust version control on
40
 
your project to a product *without* a test suite like Bazaar has?
 
33
As of September 2009, Bazaar ships with a test suite containing over
 
34
23,000 tests and growing. We are proud of it and want to remain so. As
 
35
community members, we all benefit from it. Would you trust version control
 
36
on your project to a product *without* a test suite like Bazaar has?
41
37
 
42
38
 
43
39
Running the Test Suite
44
40
======================
45
41
 
 
42
As of Bazaar 2.1, you must have the testtools_ library installed to run
 
43
the bzr test suite.
 
44
 
 
45
.. _testtools: https://launchpad.net/testtools/
 
46
 
 
47
To test all of Bazaar, just run::
 
48
 
 
49
  bzr selftest 
 
50
 
 
51
With ``--verbose`` bzr will print the name of every test as it is run.
 
52
 
 
53
This should always pass, whether run from a source tree or an installed
 
54
copy of Bazaar.  Please investigate and/or report any failures.
 
55
 
 
56
 
 
57
Running particular tests
 
58
------------------------
 
59
 
46
60
Currently, bzr selftest is used to invoke tests.
47
61
You can provide a pattern argument to run a subset. For example,
48
62
to run just the blackbox tests, run::
90
104
--load-list. The later is rarely used but allows to run a subset of a list of
91
105
failing tests for example.
92
106
 
 
107
Disabling plugins
 
108
-----------------
 
109
 
 
110
To test only the bzr core, ignoring any plugins you may have installed,
 
111
use::
 
112
 
 
113
  ./bzr --no-plugins selftest 
 
114
 
 
115
Disabling crash reporting
 
116
-------------------------
 
117
 
 
118
By default Bazaar uses apport_ to report program crashes.  In developing
 
119
Bazaar it's normal and expected to have it crash from time to time, at
 
120
least because a test failed if for no other reason.
 
121
 
 
122
Therefore you should probably add ``debug_flags = no_apport`` to your
 
123
``bazaar.conf`` file (in ``~/.bazaar/`` on Unix), so that failures just
 
124
print a traceback rather than writing a crash file.
 
125
 
 
126
.. _apport: https://launchpad.net/apport/
 
127
 
93
128
 
94
129
Test suite debug flags
95
130
----------------------
101
136
  This can provide useful logging to help debug test failures when used
102
137
  with e.g. ``bzr -Dhpss selftest -E=allow_debug``
103
138
 
 
139
  Note that this will probably cause some tests to fail, because they
 
140
  don't expect to run with any debug flags on.
 
141
 
 
142
 
 
143
Using subunit
 
144
-------------
 
145
 
 
146
Bazaar can optionally produce output in the machine-readable subunit_
 
147
format, so that test output can be post-processed by various tools.
 
148
 
 
149
.. _subunit: https://launchpad.net/subunit/
 
150
 
 
151
 
104
152
 
105
153
Writing Tests
106
154
=============
107
155
 
 
156
Normally you should add or update a test for all bug fixes or new features
 
157
in Bazaar.
 
158
 
 
159
 
108
160
Where should I put a new test?
109
161
------------------------------
110
162
 
158
210
    cmd_object.run() method directly. This is a lot faster than
159
211
    subprocesses and generates the same logging output as running it in a
160
212
    subprocess (which invoking the method directly does not).
161
 
 
 
213
 
162
214
 3. Only test the one command in a single test script. Use the bzrlib
163
215
    library when setting up tests and when evaluating the side-effects of
164
216
    the command. We do this so that the library api has continual pressure
177
229
 
178
230
Per-implementation tests are tests that are defined once and then run
179
231
against multiple implementations of an interface.  For example,
180
 
``test_transport_implementations.py`` defines tests that all Transport
181
 
implementations (local filesystem, HTTP, and so on) must pass.
182
 
 
183
 
They are found in ``bzrlib/tests/*_implementations/test_*.py``,
184
 
``bzrlib/tests/per_*/*.py``, and
185
 
``bzrlib/tests/test_*_implementations.py``.
 
232
``per_transport.py`` defines tests that all Transport implementations
 
233
(local filesystem, HTTP, and so on) must pass. They are found in
 
234
``bzrlib/tests/per_*/*.py``, and ``bzrlib/tests/per_*.py``.
186
235
 
187
236
These are really a sub-category of unit tests, but an important one.
188
237
 
 
238
Along the same lines are tests for extension modules. We generally have
 
239
both a pure-python and a compiled implementation for each module. As such,
 
240
we want to run the same tests against both implementations. These can
 
241
generally be found in ``bzrlib/tests/*__*.py`` since extension modules are
 
242
usually prefixed with an underscore. Since there are only two
 
243
implementations, we have a helper function
 
244
``bzrlib.tests.permute_for_extension``, which can simplify the
 
245
``load_tests`` implementation.
 
246
 
189
247
 
190
248
Doctests
191
249
~~~~~~~~
201
259
  __ http://docs.python.org/lib/module-doctest.html
202
260
 
203
261
 
204
 
.. Effort tests
205
 
.. ~~~~~~~~~~~~
206
 
 
 
262
Shell-like tests
 
263
----------------
 
264
 
 
265
``bzrlib/tests/script.py`` allows users to write tests in a syntax very close to a shell session,
 
266
using a restricted and limited set of commands that should be enough to mimic
 
267
most of the behaviours.
 
268
 
 
269
A script is a set of commands, each command is composed of:
 
270
 
 
271
 * one mandatory command line,
 
272
 * one optional set of input lines to feed the command,
 
273
 * one optional set of output expected lines,
 
274
 * one optional set of error expected lines.
 
275
 
 
276
Input, output and error lines can be specified in any order.
 
277
 
 
278
Except for the expected output, all lines start with a special
 
279
string (based on their origin when used under a Unix shell):
 
280
 
 
281
 * '$ ' for the command,
 
282
 * '<' for input,
 
283
 * nothing for output,
 
284
 * '2>' for errors,
 
285
 
 
286
Comments can be added anywhere, they start with '#' and end with
 
287
the line.
 
288
 
 
289
The execution stops as soon as an expected output or an expected error is not
 
290
matched.
 
291
 
 
292
When no output is specified, any ouput from the command is accepted
 
293
and execution continue.
 
294
 
 
295
If an error occurs and no expected error is specified, the execution stops.
 
296
 
 
297
An error is defined by a returned status different from zero, not by the
 
298
presence of text on the error stream.
 
299
 
 
300
The matching is done on a full string comparison basis unless '...' is used, in
 
301
which case expected output/errors can be less precise.
 
302
 
 
303
Examples:
 
304
 
 
305
The following will succeeds only if 'bzr add' outputs 'adding file'::
 
306
 
 
307
  $ bzr add file
 
308
  >adding file
 
309
 
 
310
If you want the command to succeed for any output, just use::
 
311
 
 
312
  $ bzr add file
 
313
 
 
314
The following will stop with an error::
 
315
 
 
316
  $ bzr not-a-command
 
317
 
 
318
If you want it to succeed, use::
 
319
 
 
320
  $ bzr not-a-command
 
321
  2> bzr: ERROR: unknown command "not-a-command"
 
322
 
 
323
You can use ellipsis (...) to replace any piece of text you don't want to be
 
324
matched exactly::
 
325
 
 
326
  $ bzr branch not-a-branch
 
327
  2>bzr: ERROR: Not a branch...not-a-branch/".
 
328
 
 
329
This can be used to ignore entire lines too::
 
330
 
 
331
  $ cat
 
332
  <first line
 
333
  <second line
 
334
  <third line
 
335
  # And here we explain that surprising fourth line
 
336
  <fourth line
 
337
  <last line
 
338
  >first line
 
339
  >...
 
340
  >last line
 
341
 
 
342
You can check the content of a file with cat::
 
343
 
 
344
  $ cat <file
 
345
  >expected content
 
346
 
 
347
You can also check the existence of a file with cat, the following will fail if
 
348
the file doesn't exist::
 
349
 
 
350
  $ cat file
 
351
 
 
352
The actual use of ScriptRunner within a TestCase looks something like
 
353
this::
 
354
 
 
355
        def test_unshelve_keep(self):
 
356
                # some setup here
 
357
                sr = ScriptRunner()
 
358
                sr.run_script(self, '''
 
359
        $ bzr add file
 
360
        $ bzr shelve --all -m Foo
 
361
        $ bzr shelve --list
 
362
        1: Foo
 
363
        $ bzr unshelve --keep
 
364
        $ bzr shelve --list
 
365
        1: Foo
 
366
        $ cat file
 
367
        contents of file
 
368
        ''')
 
369
 
 
370
 
 
371
Import tariff tests
 
372
-------------------
 
373
 
 
374
`bzrlib.tests.test_import_tariff` has some tests that measure how many
 
375
Python modules are loaded to run some representative commands.
 
376
 
 
377
We want to avoid loading code unnecessarily, for reasons including:
 
378
 
 
379
* Python modules are interpreted when they're loaded, either to define
 
380
  classes or modules or perhaps to initialize some structures.
 
381
 
 
382
* With a cold cache we may incur blocking real disk IO for each module.
 
383
 
 
384
* Some modules depend on many others.
 
385
 
 
386
* Some optional modules such as `testtools` are meant to be soft
 
387
  dependencies and only needed for particular cases.  If they're loaded in
 
388
  other cases then bzr may break for people who don't have those modules.
 
389
  
 
390
`test_import_tarrif` allows us to check that removal of imports doesn't
 
391
regress.
 
392
 
 
393
This is done by running the command in a subprocess with
 
394
``--profile-imports``.  Starting a whole Python interpreter is pretty
 
395
slow, so we don't want exhaustive testing here, but just enough to guard
 
396
against distinct fixed problems.
 
397
 
 
398
Assertions about precisely what is loaded tend to be brittle so we instead
 
399
make assertions that particular things aren't loaded.
 
400
 
 
401
Unless selftest is run with ``--no-plugins``, modules will be loaded in
 
402
the usual way and checks made on what they cause to be loaded.  This is
 
403
probably worth checking into, because many bzr users have at least some
 
404
plugins installed (and they're included in binary installers).
 
405
 
 
406
In theory, plugins might have a good reason to load almost anything:
 
407
someone might write a plugin that opens a network connection or pops up a
 
408
gui window every time you run 'bzr status'.  However, it's more likely
 
409
that the code to do these things is just being loaded accidentally.  We
 
410
might eventually need to have a way to make exceptions for particular
 
411
plugins.
 
412
 
 
413
Some things to check:
 
414
 
 
415
* non-GUI commands shouldn't load GUI libraries
 
416
 
 
417
* operations on bzr native formats sholudn't load foreign branch libraries
 
418
 
 
419
* network code shouldn't be loaded for purely local operations
 
420
 
 
421
* particularly expensive Python built-in modules shouldn't be loaded
 
422
  unless there is a good reason
207
423
 
208
424
 
209
425
Skipping tests
255
471
        The test exists but is known to fail, for example this might be
256
472
        appropriate to raise if you've committed a test for a bug but not
257
473
        the fix for it, or if something works on Unix but not on Windows.
258
 
        
 
474
 
259
475
        Raising this allows you to distinguish these failures from the
260
476
        ones that are not expected to fail.  If the test would fail
261
477
        because of something we don't expect or intend to fix,
265
481
        KnownFailure should be used with care as we don't want a
266
482
        proliferation of quietly broken tests.
267
483
 
 
484
 
 
485
 
268
486
We plan to support three modes for running the test suite to control the
269
487
interpretation of these results.  Strict mode is for use in situations
270
488
like merges to the mainline and releases where we want to make sure that
281
499
UnavailableFeature      fail    pass    pass
282
500
KnownFailure            fail    pass    pass
283
501
======================= ======= ======= ========
284
 
     
 
502
 
285
503
 
286
504
Test feature dependencies
287
505
-------------------------
310
528
 
311
529
    self.requireFeature(StraceFeature)
312
530
 
313
 
Features already defined in bzrlib.tests include:
314
 
 
315
 
 - SymlinkFeature,
316
 
 - HardlinkFeature,
317
 
 - OsFifoFeature,
318
 
 - UnicodeFilenameFeature,
319
 
 - FTPServerFeature, and
 
531
The old naming style for features is CamelCase, but because they're
 
532
actually instances not classses they're now given instance-style names
 
533
like ``apport``.
 
534
 
 
535
Features already defined in ``bzrlib.tests`` and ``bzrlib.tests.features``
 
536
include:
 
537
 
 
538
 - apport
 
539
 - paramiko
 
540
 - SymlinkFeature
 
541
 - HardlinkFeature
 
542
 - OsFifoFeature
 
543
 - UnicodeFilenameFeature
 
544
 - FTPServerFeature
320
545
 - CaseInsensitiveFilesystemFeature.
321
546
 
322
547
 
328
553
``_probe`` and ``feature_name`` methods.  For example::
329
554
 
330
555
    class _SymlinkFeature(Feature):
331
 
    
 
556
 
332
557
        def _probe(self):
333
558
            return osutils.has_symlinks()
334
 
    
 
559
 
335
560
        def feature_name(self):
336
561
            return 'symlinks'
337
 
    
 
562
 
338
563
    SymlinkFeature = _SymlinkFeature()
339
564
 
 
565
A helper for handling running tests based on whether a python
 
566
module is available. This can handle 3rd-party dependencies (is
 
567
``paramiko`` available?) as well as stdlib (``termios``) or
 
568
extension modules (``bzrlib._groupcompress_pyx``). You create a
 
569
new feature instance with::
 
570
 
 
571
    # in bzrlib/tests/features.py
 
572
    apport = tests.ModuleAvailableFeature('apport')
 
573
 
 
574
 
 
575
    # then in bzrlib/tests/test_apport.py
 
576
    class TestApportReporting(TestCaseInTempDir):
 
577
 
 
578
        _test_needs_features = [features.apport]
 
579
 
340
580
 
341
581
Testing exceptions and errors
342
582
-----------------------------