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_internal(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_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.
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.
57
... def __init__(self, src):
58
... self.lines = src.split('\n')
59
... def readline(self):
60
... line = self.lines.pop(0)
64
>>> real_stdin = sys.stdin
66
Using pdb.set_trace in a function called by an ordinary test:
68
>>> if sys.version_info[:2] == (2, 3):
69
... sys.stdin = Input('n\np x\nc')
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()
83
Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.
87
Using pdb.set_trace in a function called by a doctest in a doc string:
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:...
96
> doctest.py(351)set_trace()->None
97
-> pdb.Pdb.set_trace(self)
99
> testrunner-ex/sample3/sampletests_d.py(42)f()
104
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
108
Using pdb in a docstring-based doctest
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:...
117
> doctest.py(351)set_trace()->None
118
-> pdb.Pdb.set_trace(self)
120
> <doctest sample3.sampletests_d.set_trace3[line 3, example 1]>(3)...()
125
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
129
Using pdb.set_trace in a doc file:
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:...
139
> doctest.py(351)set_trace()->None
140
-> pdb.Pdb.set_trace(self)
142
> <doctest set_trace5.txt[line 2, example 1]>(3)...()
147
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
152
Using pdb.set_trace in a function called by a doctest in a doc file:
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:...
162
> doctest.py(351)set_trace()->None
163
-> pdb.Pdb.set_trace(self)
165
> testrunner-ex/sample3/sampletests_d.py(42)f()
170
Ran 1 tests with 0 failures and 0 errors in 0.002 seconds.
174
Post-mortem debugging function called from ordinary test:
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:...
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
190
File "testrunner-ex/sample3/sampletests_d.py", line 46, in g
194
exceptions.ValueError:
196
> testrunner-ex/sample3/sampletests_d.py(46)g()
204
Post-mortem debugging docstring-based doctest:
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
223
File "zope/testing/doctest.py", line 1737, in report_unexpected_exception
224
raise UnexpectedException(test, example, exc_info)
226
from testrunner-ex/sample3/sampletests_d.py:61 (2 examples)>
228
exceptions.ValueError:
230
> <doctest sample3.sampletests_d.post_mortem3[line 3, example 1]>(1)...()
236
Post-mortem debugging function called from docstring-based doctest:
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
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)>
259
exceptions.ValueError:
261
> testrunner-ex/sample3/sampletests_d.py(46)g()
268
Post-mortem debugging file-based doctest:
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:...
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
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)>
293
exceptions.ValueError:
295
> <doctest post_mortem5.txt[line 2, example 1]>(1)...()
302
Post-mortem debugging function called from file-based doctest:
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:...
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
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)>
327
exceptions.ValueError:
329
> testrunner-ex/sample3/sampletests_d.py(46)g()
336
Post-mortem debugging of a docstring doctest failure:
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:...
347
Error in test post_mortem_failure2 (sample3.sampletests_d)
349
File "testrunner-ex/sample3/sampletests_d.py",
350
line 81, in sample3.sampletests_d.post_mortem_failure2
361
> testrunner-ex/sample3/sampletests_d.py(81)_()
362
exceptions.ValueError:
363
Expected and actual output are different
371
Post-mortem debugging of a docfile doctest failure:
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:...
382
Error in test /home/jim/z3/zope.testing/src/zope/testing/testrunner-ex/sample3/post_mortem_failure.txt
384
File "testrunner-ex/sample3/post_mortem_failure.txt",
385
line 2, in post_mortem_failure.txt
396
> testrunner-ex/sample3/post_mortem_failure.txt(2)_()
397
exceptions.ValueError:
398
Expected and actual output are different
405
Post-mortem debugging with triple verbosity
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.
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.
427
Test Suites with None for suites or tests
428
-----------------------------------------
430
>>> sys.argv = ['test',
431
... '--tests-pattern', '^sampletests_none_suite$',
433
>>> testrunner.run_internal(defaults)
434
Test-module import failures:
436
Module: sample1.sampletests_none_suite
438
TypeError: Invalid test_suite, None, in sample1.sampletests_none_suite
442
Test-modules with import problems:
443
sample1.sampletests_none_suite
444
Total: 0 tests, 0 failures, 0 errors in N.NNN seconds.
448
>>> sys.argv = ['test',
449
... '--tests-pattern', '^sampletests_none_test$',
451
>>> testrunner.run_internal(defaults)
452
Test-module import failures:
454
Module: sample1.sampletests_none_test
460
Test-modules with import problems:
461
sample1.sampletests_none_test
462
Total: 0 tests, 0 failures, 0 errors in N.NNN seconds.
465
You must use --repeat with --report-refcounts
466
---------------------------------------------
468
It is an error to specify --report-refcounts (-r) without specifying a
469
repeat count greater than 1
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)
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)