~landscape/zope3/ztk-1.1.3

« back to all changes in this revision

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

  • Committer: Thomas Hervé
  • Date: 2009-09-21 16:46:07 UTC
  • Revision ID: thomas@canonical.com-20090921164607-sky3xhlt02ji80ka
Revert r8: regression with test failures

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