4
This document has some edge-case examples to test various aspects of
7
Separating Python path and test directories
8
-------------------------------------------
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:
16
>>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex')
18
>>> from zope.testing import testrunner
21
... '--test-path', directory_with_tests,
22
... '--tests-pattern', '^sampletestsf?$',
24
>>> sys.argv = ['test']
25
>>> testrunner.run(defaults)
26
... # doctest: +ELLIPSIS
27
Test-module import failures:
31
ImportError: No module named sampletestsf
34
>>> sys.path.append(directory_with_tests)
35
>>> sys.argv = ['test']
36
>>> testrunner.run(defaults)
37
... # doctest: +ELLIPSIS
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.
49
... def __init__(self, src):
50
... self.lines = src.split('\n')
51
... def readline(self):
52
... line = self.lines.pop(0)
56
>>> real_stdin = sys.stdin
58
Using pdb.set_trace in a function called by an ordinary test:
60
>>> if sys.version_info[:2] == (2, 3):
61
... sys.stdin = Input('n\np x\nc')
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()
75
Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.
78
Using pdb.set_trace in a function called by a doctest in a doc string:
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
87
> doctest.py(351)set_trace()->None
88
-> pdb.Pdb.set_trace(self)
90
> testrunner-ex/sample3/sampletests_d.py(42)f()
95
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
98
Using pdb in a docstring-based doctest
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
107
> doctest.py(351)set_trace()->None
108
-> pdb.Pdb.set_trace(self)
110
> <doctest sample3.sampletests_d.set_trace3[1]>(3)...()
115
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
118
Using pdb.set_trace in a doc file:
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
128
> doctest.py(351)set_trace()->None
129
-> pdb.Pdb.set_trace(self)
131
> <doctest set_trace5.txt[1]>(3)...()
136
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
140
Using pdb.set_trace in a function called by a doctest in a doc file:
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
150
> doctest.py(351)set_trace()->None
151
-> pdb.Pdb.set_trace(self)
153
> testrunner-ex/sample3/sampletests_d.py(42)f()
158
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
161
Post-mortem debugging function called from ordinary test:
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
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
177
File "testrunner-ex/sample3/sampletests_d.py", line 46, in g
181
exceptions.ValueError:
183
> testrunner-ex/sample3/sampletests_d.py(46)g()
191
Post-mortem debugging docstring-based doctest:
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
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
212
File "zope/testing/doctest.py", line 1737, in report_unexpected_exception
213
raise UnexpectedException(test, example, exc_info)
215
from testrunner-ex/sample3/sampletests_d.py:61 (2 examples)>
217
exceptions.ValueError:
219
> <doctest sample3.sampletests_d.post_mortem3[1]>(1)...()
225
Post-mortem debugging function called from docstring-based doctest:
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
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
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)>
250
exceptions.ValueError:
252
> testrunner-ex/sample3/sampletests_d.py(46)g()
259
Post-mortem debugging file-based doctest:
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
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
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)>
284
exceptions.ValueError:
286
> <doctest post_mortem5.txt[1]>(1)...()
293
Post-mortem debugging function called from file-based doctest:
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
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
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)>
318
exceptions.ValueError:
320
> testrunner-ex/sample3/sampletests_d.py(46)g()
327
Post-mortem debugging of a docstring doctest failure:
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
338
Error in test post_mortem_failure2 (sample3.sampletests_d)
340
File "testrunner-ex/sample3/sampletests_d.py",
341
line 81, in sample3.sampletests_d.post_mortem_failure2
352
> testrunner-ex/sample3/sampletests_d.py(81)_()
353
exceptions.ValueError:
354
Expected and actual output are different
362
Post-mortem debugging of a docfile doctest failure:
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
373
Error in test /home/jim/z3/zope.testing/src/zope/testing/testrunner-ex/sample3/post_mortem_failure.txt
375
File "testrunner-ex/sample3/post_mortem_failure.txt",
376
line 2, in post_mortem_failure.txt
387
> testrunner-ex/sample3/post_mortem_failure.txt(2)_()
388
exceptions.ValueError:
389
Expected and actual output are different
396
Post-mortem debugging with triple verbosity
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.
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.
418
Test Suites with None for suites or tests
419
-----------------------------------------
421
>>> sys.argv = ['test',
422
... '--tests-pattern', '^sampletests_none_suite$',
424
>>> testrunner.run(defaults)
425
Test-module import failures:
427
Module: sample1.sampletests_none_suite
429
TypeError: Invalid test_suite, None, in sample1.sampletests_none_suite
432
Total: 0 tests, 0 failures, 0 errors in N.NNN seconds.
434
Test-modules with import problems:
435
sample1.sampletests_none_suite
439
>>> sys.argv = ['test',
440
... '--tests-pattern', '^sampletests_none_test$',
442
>>> testrunner.run(defaults)
443
Test-module import failures:
445
Module: sample1.sampletests_none_test
450
Total: 0 tests, 0 failures, 0 errors in N.NNN seconds.
452
Test-modules with import problems:
453
sample1.sampletests_none_test
456
You must use --repeat with --report-refcounts
457
---------------------------------------------
459
It is an error to specify --report-refcounts (-r) without specifying a
460
repeat count greater than 1
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)
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)