~ubuntu-branches/ubuntu/natty/zope.testing/natty

« back to all changes in this revision

Viewing changes to src/zope/testing/testrunner/testrunner-edge-cases.txt

  • Committer: Bazaar Package Importer
  • Author(s): Brian Sutherland
  • Date: 2009-06-15 05:56:09 UTC
  • Revision ID: james.westby@ubuntu.com-20090615055609-71993ywo73mrvkud
Tags: upstream-3.7.4
ImportĀ upstreamĀ versionĀ 3.7.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
testrunner Edge Cases
 
2
=====================
 
3
 
 
4
This document has some edge-case examples to test various aspects of
 
5
the test runner.
 
6
 
 
7
Separating Python path and test directories
 
8
-------------------------------------------
 
9
 
 
10
The --path option defines a directory to be searched for tests *and* a
 
11
directory to be added to Python's search path.  The --test-path option
 
12
can be used when you want to set a test search path without also
 
13
affecting the Python path:
 
14
 
 
15
    >>> import os, sys
 
16
    >>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex')
 
17
 
 
18
    >>> from zope.testing import testrunner
 
19
 
 
20
    >>> defaults = [
 
21
    ...     '--test-path', directory_with_tests,
 
22
    ...     '--tests-pattern', '^sampletestsf?$',
 
23
    ...     ]
 
24
    >>> sys.argv = ['test']
 
25
    >>> testrunner.run_internal(defaults)
 
26
    ... # doctest: +ELLIPSIS
 
27
    Test-module import failures:
 
28
    <BLANKLINE>
 
29
    Module: sampletestsf
 
30
    <BLANKLINE>
 
31
    ImportError: No module named sampletestsf
 
32
    ...
 
33
 
 
34
    >>> sys.path.append(directory_with_tests)
 
35
    >>> sys.argv = ['test']
 
36
    >>> testrunner.run_internal(defaults)
 
37
    ... # doctest: +ELLIPSIS
 
38
    Running samplelayers.Layer1 tests:
 
39
      Set up samplelayers.Layer1 in 0.000 seconds.
 
40
      Ran 9 tests with 0 failures and 0 errors in 0.000 seconds.
 
41
    ...
 
42
    Running zope.testing.testrunner.layer.UnitTests tests:
 
43
      Tear down samplelayers.Layer122 in N.NNN seconds.
 
44
      Tear down samplelayers.Layer12 in N.NNN seconds.
 
45
      Tear down samplelayers.Layer1 in N.NNN seconds.
 
46
      Set up zope.testing.testrunner.layer.UnitTests in N.NNN seconds.
 
47
      Ran 192 tests with 0 failures and 0 errors in N.NNN seconds.
 
48
    Tearing down left over layers:
 
49
      Tear down zope.testing.testrunner.layer.UnitTests in N.NNN seconds.
 
50
    Total: 405 tests, 0 failures, 0 errors in N.NNN seconds.
 
51
    False
 
52
 
 
53
Debugging Edge Cases
 
54
--------------------
 
55
 
 
56
    >>> class Input:
 
57
    ...     def __init__(self, src):
 
58
    ...         self.lines = src.split('\n')
 
59
    ...     def readline(self):
 
60
    ...         line = self.lines.pop(0)
 
61
    ...         print line
 
62
    ...         return line+'\n'
 
63
 
 
64
    >>> real_stdin = sys.stdin
 
65
 
 
66
Using pdb.set_trace in a function called by an ordinary test:
 
67
 
 
68
    >>> if sys.version_info[:2] == (2, 3):
 
69
    ...     sys.stdin = Input('n\np x\nc')
 
70
    ... else:
 
71
    ...     sys.stdin = Input('p x\nc')
 
72
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
73
    ...             ' -t set_trace2').split()
 
74
    >>> try: testrunner.run_internal(defaults)
 
75
    ... finally: sys.stdin = real_stdin
 
76
    ... # doctest: +ELLIPSIS
 
77
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
78
    > testrunner-ex/sample3/sampletests_d.py(47)f()
 
79
    -> y = x
 
80
    (Pdb) p x
 
81
    1
 
82
    (Pdb) c
 
83
      Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.
 
84
    ...
 
85
    False
 
86
 
 
87
Using pdb.set_trace in a function called by a doctest in a doc string:
 
88
 
 
89
    >>> sys.stdin = Input('n\np x\nc')
 
90
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
91
    ...             ' -t set_trace4').split()
 
92
    >>> try: testrunner.run_internal(defaults)
 
93
    ... finally: sys.stdin = real_stdin
 
94
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
95
    --Return--
 
96
    > doctest.py(351)set_trace()->None
 
97
    -> pdb.Pdb.set_trace(self)
 
98
    (Pdb) n
 
99
    > testrunner-ex/sample3/sampletests_d.py(42)f()
 
100
    -> y = x
 
101
    (Pdb) p x
 
102
    1
 
103
    (Pdb) c
 
104
      Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
 
105
    ...
 
106
    False
 
107
 
 
108
Using pdb in a docstring-based doctest
 
109
 
 
110
    >>> sys.stdin = Input('n\np x\nc')
 
111
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
112
    ...             ' -t set_trace3').split()
 
113
    >>> try: testrunner.run_internal(defaults)
 
114
    ... finally: sys.stdin = real_stdin
 
115
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
116
    --Return--
 
117
    > doctest.py(351)set_trace()->None
 
118
    -> pdb.Pdb.set_trace(self)
 
119
    (Pdb) n
 
120
    > <doctest sample3.sampletests_d.set_trace3[line 3, example 1]>(3)...()
 
121
    -> y = x
 
122
    (Pdb) p x
 
123
    1
 
124
    (Pdb) c
 
125
      Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
 
126
    ...
 
127
    False
 
128
 
 
129
Using pdb.set_trace in a doc file:
 
130
 
 
131
 
 
132
    >>> sys.stdin = Input('n\np x\nc')
 
133
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
134
    ...             ' -t set_trace5').split()
 
135
    >>> try: testrunner.run_internal(defaults)
 
136
    ... finally: sys.stdin = real_stdin
 
137
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
138
    --Return--
 
139
    > doctest.py(351)set_trace()->None
 
140
    -> pdb.Pdb.set_trace(self)
 
141
    (Pdb) n
 
142
    > <doctest set_trace5.txt[line 2, example 1]>(3)...()
 
143
    -> y = x
 
144
    (Pdb) p x
 
145
    1
 
146
    (Pdb) c
 
147
      Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
 
148
    ...
 
149
    False
 
150
 
 
151
 
 
152
Using pdb.set_trace in a function called by a doctest in a doc file:
 
153
 
 
154
 
 
155
    >>> sys.stdin = Input('n\np x\nc')
 
156
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
157
    ...             ' -t set_trace6').split()
 
158
    >>> try: testrunner.run_internal(defaults)
 
159
    ... finally: sys.stdin = real_stdin
 
160
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
161
    --Return--
 
162
    > doctest.py(351)set_trace()->None
 
163
    -> pdb.Pdb.set_trace(self)
 
164
    (Pdb) n
 
165
    > testrunner-ex/sample3/sampletests_d.py(42)f()
 
166
    -> y = x
 
167
    (Pdb) p x
 
168
    1
 
169
    (Pdb) c
 
170
      Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
 
171
    ...
 
172
    False
 
173
 
 
174
Post-mortem debugging function called from ordinary test:
 
175
 
 
176
    >>> sys.stdin = Input('p x\nc')
 
177
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
178
    ...             ' -t post_mortem2 -D').split()
 
179
    >>> try: testrunner.run_internal(defaults)
 
180
    ... finally: sys.stdin = real_stdin
 
181
    ... # doctest: +NORMALIZE_WHITESPACE
 
182
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
183
    <BLANKLINE>
 
184
    <BLANKLINE>
 
185
    Error in test test_post_mortem2 (sample3.sampletests_d.TestSomething)
 
186
    Traceback (most recent call last):
 
187
      File "testrunner-ex/sample3/sampletests_d.py",
 
188
           line 37, in test_post_mortem2
 
189
        g()
 
190
      File "testrunner-ex/sample3/sampletests_d.py", line 46, in g
 
191
        raise ValueError
 
192
    ValueError
 
193
    <BLANKLINE>
 
194
    exceptions.ValueError:
 
195
    <BLANKLINE>
 
196
    > testrunner-ex/sample3/sampletests_d.py(46)g()
 
197
    -> raise ValueError
 
198
    (Pdb) p x
 
199
    1
 
200
    (Pdb) c
 
201
    True
 
202
 
 
203
 
 
204
Post-mortem debugging docstring-based doctest:
 
205
 
 
206
    >>> sys.stdin = Input('p x\nc')
 
207
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
208
    ...             ' -t post_mortem3 -D').split()
 
209
    >>> try: testrunner.run_internal(defaults)
 
210
    ... finally: sys.stdin = real_stdin
 
211
    ... # doctest: +NORMALIZE_WHITESPACE
 
212
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
213
    Error in test post_mortem3 (sample3.sampletests_d)
 
214
    Traceback (most recent call last):
 
215
      File "zope/testing/doctest.py", line 2276, in debug
 
216
        runner.run(self._dt_test)
 
217
      File "zope/testing/doctest.py", line 1731, in run
 
218
        r = DocTestRunner.run(self, test, compileflags, out, False)
 
219
      File "zope/testing/doctest.py", line 1389, in run
 
220
        return self.__run(test, compileflags, out)
 
221
      File "zope/testing/doctest.py", line 1310, in __run
 
222
        exc_info)
 
223
      File "zope/testing/doctest.py", line 1737, in report_unexpected_exception
 
224
        raise UnexpectedException(test, example, exc_info)
 
225
    UnexpectedException:
 
226
       from testrunner-ex/sample3/sampletests_d.py:61 (2 examples)>
 
227
    <BLANKLINE>
 
228
    exceptions.ValueError:
 
229
    <BLANKLINE>
 
230
    > <doctest sample3.sampletests_d.post_mortem3[line 3, example 1]>(1)...()
 
231
    (Pdb) p x
 
232
    1
 
233
    (Pdb) c
 
234
    True
 
235
 
 
236
Post-mortem debugging function called from docstring-based doctest:
 
237
 
 
238
    >>> sys.stdin = Input('p x\nc')
 
239
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
240
    ...             ' -t post_mortem4 -D').split()
 
241
    >>> try: testrunner.run_internal(defaults)
 
242
    ... finally: sys.stdin = real_stdin
 
243
    ... # doctest: +NORMALIZE_WHITESPACE
 
244
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
245
    Error in test post_mortem4 (sample3.sampletests_d)
 
246
    Traceback (most recent call last):
 
247
      File "zope/testing/doctest.py", line 2276, in debug
 
248
        runner.run(self._dt_test)
 
249
      File "zope/testing/doctest.py", line 1731, in run
 
250
        r = DocTestRunner.run(self, test, compileflags, out, False)
 
251
      File "zope/testing/doctest.py", line 1389, in run
 
252
        return self.__run(test, compileflags, out)
 
253
      File "zope/testing/doctest.py", line 1310, in __run
 
254
        exc_info)
 
255
      File "zope/testing/doctest.py", line 1737, in report_unexpected_exception
 
256
        raise UnexpectedException(test, example, exc_info)
 
257
    UnexpectedException: testrunner-ex/sample3/sampletests_d.py:67 (1 example)>
 
258
    <BLANKLINE>
 
259
    exceptions.ValueError:
 
260
    <BLANKLINE>
 
261
    > testrunner-ex/sample3/sampletests_d.py(46)g()
 
262
    -> raise ValueError
 
263
    (Pdb) p x
 
264
    1
 
265
    (Pdb) c
 
266
    True
 
267
 
 
268
Post-mortem debugging file-based doctest:
 
269
 
 
270
    >>> sys.stdin = Input('p x\nc')
 
271
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
272
    ...             ' -t post_mortem5 -D').split()
 
273
    >>> try: testrunner.run_internal(defaults)
 
274
    ... finally: sys.stdin = real_stdin
 
275
    ... # doctest: +NORMALIZE_WHITESPACE
 
276
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
277
    <BLANKLINE>
 
278
    <BLANKLINE>
 
279
    Error in test zope/testing/testrunner-ex/sample3/post_mortem5.txt
 
280
    Traceback (most recent call last):
 
281
      File "zope/testing/doctest.py", line 2276, in debug
 
282
        runner.run(self._dt_test)
 
283
      File "zope/testing/doctest.py", line 1731, in run
 
284
        r = DocTestRunner.run(self, test, compileflags, out, False)
 
285
      File "zope/testing/doctest.py", line 1389, in run
 
286
        return self.__run(test, compileflags, out)
 
287
      File "zope/testing/doctest.py", line 1310, in __run
 
288
        exc_info)
 
289
      File "zope/testing/doctest.py", line 1737, in report_unexpected_exception
 
290
        raise UnexpectedException(test, example, exc_info)
 
291
    UnexpectedException: testrunner-ex/sample3/post_mortem5.txt:0 (2 examples)>
 
292
    <BLANKLINE>
 
293
    exceptions.ValueError:
 
294
    <BLANKLINE>
 
295
    > <doctest post_mortem5.txt[line 2, example 1]>(1)...()
 
296
    (Pdb) p x
 
297
    1
 
298
    (Pdb) c
 
299
    True
 
300
 
 
301
 
 
302
Post-mortem debugging function called from file-based doctest:
 
303
 
 
304
    >>> sys.stdin = Input('p x\nc')
 
305
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
306
    ...             ' -t post_mortem6 -D').split()
 
307
    >>> try: testrunner.run_internal(defaults)
 
308
    ... finally: sys.stdin = real_stdin
 
309
    ... # doctest: +NORMALIZE_WHITESPACE
 
310
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
311
    <BLANKLINE>
 
312
    <BLANKLINE>
 
313
    Error in test zope/testing/testrunner-ex/sample3/post_mortem6.txt
 
314
    Traceback (most recent call last):
 
315
      File "zope/testing/doctest.py", line 2276, in debug
 
316
        runner.run(self._dt_test)
 
317
      File "zope/testing/doctest.py", line 1731, in run
 
318
        r = DocTestRunner.run(self, test, compileflags, out, False)
 
319
      File "zope/testing/doctest.py", line 1389, in run
 
320
        return self.__run(test, compileflags, out)
 
321
      File "zope/testing/doctest.py", line 1310, in __run
 
322
        exc_info)
 
323
      File "zope/testing/doctest.py", line 1737, in report_unexpected_exception
 
324
        raise UnexpectedException(test, example, exc_info)
 
325
    UnexpectedException: testrunner-ex/sample3/post_mortem6.txt:0 (2 examples)>
 
326
    <BLANKLINE>
 
327
    exceptions.ValueError:
 
328
    <BLANKLINE>
 
329
    > testrunner-ex/sample3/sampletests_d.py(46)g()
 
330
    -> raise ValueError
 
331
    (Pdb) p x
 
332
    1
 
333
    (Pdb) c
 
334
    True
 
335
 
 
336
Post-mortem debugging of a docstring doctest failure:
 
337
 
 
338
    >>> sys.stdin = Input('p x\nc')
 
339
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
340
    ...             ' -t post_mortem_failure2 -D').split()
 
341
    >>> try: testrunner.run_internal(defaults)
 
342
    ... finally: sys.stdin = real_stdin
 
343
    ... # doctest: +NORMALIZE_WHITESPACE
 
344
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
345
    <BLANKLINE>
 
346
    <BLANKLINE>
 
347
    Error in test post_mortem_failure2 (sample3.sampletests_d)
 
348
    <BLANKLINE>
 
349
    File "testrunner-ex/sample3/sampletests_d.py",
 
350
                   line 81, in sample3.sampletests_d.post_mortem_failure2
 
351
    <BLANKLINE>
 
352
    x
 
353
    <BLANKLINE>
 
354
    Want:
 
355
    2
 
356
    <BLANKLINE>
 
357
    Got:
 
358
    1
 
359
    <BLANKLINE>
 
360
    <BLANKLINE>
 
361
    > testrunner-ex/sample3/sampletests_d.py(81)_()
 
362
    exceptions.ValueError:
 
363
    Expected and actual output are different
 
364
    > <string>(1)...()
 
365
    (Pdb) p x
 
366
    1
 
367
    (Pdb) c
 
368
    True
 
369
 
 
370
 
 
371
Post-mortem debugging of a docfile doctest failure:
 
372
 
 
373
    >>> sys.stdin = Input('p x\nc')
 
374
    >>> sys.argv = ('test -ssample3 --tests-pattern ^sampletests_d$'
 
375
    ...             ' -t post_mortem_failure.txt -D').split()
 
376
    >>> try: testrunner.run_internal(defaults)
 
377
    ... finally: sys.stdin = real_stdin
 
378
    ... # doctest: +NORMALIZE_WHITESPACE
 
379
    Running zope.testing.testrunner.layer.UnitTests tests:...
 
380
    <BLANKLINE>
 
381
    <BLANKLINE>
 
382
    Error in test /home/jim/z3/zope.testing/src/zope/testing/testrunner-ex/sample3/post_mortem_failure.txt
 
383
    <BLANKLINE>
 
384
    File "testrunner-ex/sample3/post_mortem_failure.txt",
 
385
                                      line 2, in post_mortem_failure.txt
 
386
    <BLANKLINE>
 
387
    x
 
388
    <BLANKLINE>
 
389
    Want:
 
390
    2
 
391
    <BLANKLINE>
 
392
    Got:
 
393
    1
 
394
    <BLANKLINE>
 
395
    <BLANKLINE>
 
396
    > testrunner-ex/sample3/post_mortem_failure.txt(2)_()
 
397
    exceptions.ValueError:
 
398
    Expected and actual output are different
 
399
    > <string>(1)...()
 
400
    (Pdb) p x
 
401
    1
 
402
    (Pdb) c
 
403
    True
 
404
 
 
405
Post-mortem debugging with triple verbosity
 
406
 
 
407
    >>> sys.argv = 'test --layer samplelayers.Layer1$ -vvv -D'.split()
 
408
    >>> testrunner.run_internal(defaults)
 
409
    Running tests at level 1
 
410
    Running samplelayers.Layer1 tests:
 
411
      Set up samplelayers.Layer1 in 0.000 seconds.
 
412
      Running:
 
413
        test_x1 (sampletestsf.TestA1) (0.000 s)
 
414
        test_y0 (sampletestsf.TestA1) (0.000 s)
 
415
        test_z0 (sampletestsf.TestA1) (0.000 s)
 
416
        test_x0 (sampletestsf.TestB1) (0.000 s)
 
417
        test_y1 (sampletestsf.TestB1) (0.000 s)
 
418
        test_z0 (sampletestsf.TestB1) (0.000 s)
 
419
        test_1 (sampletestsf.TestNotMuch1) (0.000 s)
 
420
        test_2 (sampletestsf.TestNotMuch1) (0.000 s)
 
421
        test_3 (sampletestsf.TestNotMuch1) (0.000 s)
 
422
      Ran 9 tests with 0 failures and 0 errors in 0.001 seconds.
 
423
    Tearing down left over layers:
 
424
      Tear down samplelayers.Layer1 in 0.000 seconds.
 
425
    False
 
426
 
 
427
Test Suites with None for suites or tests
 
428
-----------------------------------------
 
429
 
 
430
    >>> sys.argv = ['test',
 
431
    ...             '--tests-pattern', '^sampletests_none_suite$',
 
432
    ...     ]
 
433
    >>> testrunner.run_internal(defaults)
 
434
    Test-module import failures:
 
435
    <BLANKLINE>
 
436
    Module: sample1.sampletests_none_suite
 
437
    <BLANKLINE>
 
438
    TypeError: Invalid test_suite, None, in sample1.sampletests_none_suite
 
439
    <BLANKLINE>
 
440
    <BLANKLINE>
 
441
    <BLANKLINE>
 
442
    Test-modules with import problems:
 
443
      sample1.sampletests_none_suite
 
444
    Total: 0 tests, 0 failures, 0 errors in N.NNN seconds.
 
445
    True
 
446
 
 
447
 
 
448
    >>> sys.argv = ['test',
 
449
    ...             '--tests-pattern', '^sampletests_none_test$',
 
450
    ...     ]
 
451
    >>> testrunner.run_internal(defaults)
 
452
    Test-module import failures:
 
453
    <BLANKLINE>
 
454
    Module: sample1.sampletests_none_test
 
455
    <BLANKLINE>
 
456
    TypeError: ...
 
457
    <BLANKLINE>
 
458
    <BLANKLINE>
 
459
    <BLANKLINE>
 
460
    Test-modules with import problems:
 
461
      sample1.sampletests_none_test
 
462
    Total: 0 tests, 0 failures, 0 errors in N.NNN seconds.
 
463
    True
 
464
 
 
465
You must use --repeat with --report-refcounts
 
466
---------------------------------------------
 
467
 
 
468
It is an error to specify --report-refcounts (-r) without specifying a
 
469
repeat count greater than 1
 
470
 
 
471
    >>> sys.argv = 'test -r'.split()
 
472
    >>> testrunner.run_internal(defaults)
 
473
            You must use the --repeat (-N) option to specify a repeat
 
474
            count greater than 1 when using the --report_refcounts (-r)
 
475
            option.
 
476
    <BLANKLINE>
 
477
    True
 
478
 
 
479
    >>> sys.argv = 'test -r -N1'.split()
 
480
    >>> testrunner.run_internal(defaults)
 
481
            You must use the --repeat (-N) option to specify a repeat
 
482
            count greater than 1 when using the --report_refcounts (-r)
 
483
            option.
 
484
    <BLANKLINE>
 
485
    True