File: test_runner.py

package info (click to toggle)
xdoctest 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,512 kB
  • sloc: python: 10,963; sh: 815; cpp: 33; makefile: 19
file content (484 lines) | stat: -rw-r--r-- 12,449 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
from os.path import join
from xdoctest import utils


def test_zero_args():
    """
    python tests/test_runner.py test_zero_args
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        # --- HELPERS ---
        def zero_args1(a=1):
            pass


        def zero_args2(*args):
            pass


        def zero_args3(**kwargs):
            pass


        def zero_args4(a=1, b=2, *args, **kwargs):
            pass


        def non_zero_args1(a):
            pass


        def non_zero_args2(a, b):
            pass


        def non_zero_args3(a, b, *args):
            pass


        def non_zero_args4(a, b, **kwargs):
            pass


        def non_zero_args5(a, b=1, **kwargs):
            pass
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_zero_args.py')

        with open(modpath, 'w') as file:
            file.write(source)

        zero_func_names = {
            example.callname
            for example in runner._gather_zero_arg_examples(modpath)
        }
        assert zero_func_names == set(['zero_args1', 'zero_args2',
                                       'zero_args3', 'zero_args4'])


def test_list():
    from xdoctest import runner

    source = utils.codeblock(
        '''
        # --- HELPERS ---
        def real_test1(a=1):
            """
                Example:
                    >>> pass
            """
            pass

        def fake_test1(a=1):
            pass

        def real_test2():
            """
                Example:
                    >>> pass
            """
            pass

        def fake_test2():
            pass
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_list.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'list', argv=[''])

        assert 'real_test1' in cap.text
        assert 'real_test2' in cap.text
        assert 'fake_test1' not in cap.text
        assert 'fake_test2' not in cap.text

        # test command=None
        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, None, argv=[''])

        assert 'real_test1' in cap.text
        assert 'real_test2' in cap.text
        assert 'fake_test1' not in cap.text
        assert 'fake_test2' not in cap.text


def test_example_run():
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> print('i wanna see this')
            """
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''])

    assert 'i wanna see this' in cap.text


def test_durations():
    from xdoctest import runner
    source = utils.codeblock(
        '''
        def func1():
            """
            Example:
                >>> print(1)
            """

        def func2():
            """
            Example:
                >>> print(123)
            """
        ''')
    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_durations.py')
        with open(modpath, 'w') as file:
            file.write(source)
        with utils.CaptureStdout() as cap1:
            runner.doctest_module(modpath, 'all', argv=[''], durations=10)
        with utils.CaptureStdout() as cap2:
            runner.doctest_module(modpath, 'all', argv=[''], durations=1)
    assert cap1.text.count('time: ') == 2, '2 tests should have 2 durations'
    assert cap2.text.count('time: ') == 1, 'should only have gotten 1 durration'


def test_dump():
    from xdoctest import runner
    source = utils.codeblock(
        '''
        def func1():
            """
            Example:
                >>> these = 'tests will be converted to unit tests'
                >>> print(these + ' because sometimes you wanna')
            """

        def func2():
            """
            Example:
                >>> for i in range(10):
                >>>     these = 'and sometimes your doctests should have been '
                >>> for j in range(10):
                ...     print(these + ' unit tests all along')
            """
        ''')
    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_durations.py')
        with open(modpath, 'w') as file:
            file.write(source)
        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'dump', argv=[''])
    print(cap.text)


def test_all_disabled():
    """
    pytest tests/test_runner.py::test_all_disabled -s -vv
    python tests/test_runner.py test_all_disabled
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> # DISABLE_DOCTEST
                    >>> print('all will' + ' not print this')
            """

        def bar():
            """
                Example:
                    >>> print('all will' + ' print this')
            """
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_all_disabled.py')

        with open(modpath, 'w') as file:
            file.write(source)

        # disabled tests dont run in "all" mode
        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'all', argv=[''])
        assert 'all will print this' in cap.text
        # print('    ' + cap.text.replace('\n', '\n    '))
        assert 'all will not print this' not in cap.text

        # Running an disabled example explicitly should work
        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''])
        # print('    ' + cap.text.replace('\n', '\n    '))
        assert 'all will not print this' in cap.text


def test_runner_failures():
    """
    python tests/test_runner.py  test_runner_failures
    pytest tests/test_runner.py::test_runner_failures -s
    pytest tests/test_runner.py::test_all_disabled -s
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def test1():
            """
                Example:
                    >>> pass
            """

        def test2():
            """
                Example:
                    >>> assert False, 'test 2.1'

                Example:
                    >>> assert False, 'test 2.2'
            """

        def test3():
            """
                Example:
                    >>> pass

                Example:
                    >>> pass
            """

        def test4():
            """
                Example:
                    >>> assert False, 'test 3'
            """
        ''')

    temp = utils.TempDir()
    temp.ensure()
    # with utils.TempDir() as temp:
    dpath = temp.dpath
    modpath = join(dpath, 'test_runner_failures.py')

    with open(modpath, 'w') as file:
        file.write(source)

    # disabled tests dont run in "all" mode
    with utils.CaptureStdout(suppress=True) as cap:
        try:
            runner.doctest_module(modpath, 'all', argv=[''], verbose=1)
        except Exception:
            pass

    print('\nNOTE: the following output is part of a test')
    print(utils.indent(cap.text, '... '))
    print('NOTE: above output is part of a test')

    # assert '.FFF' in cap.text
    assert '3 / 6 passed' in cap.text
    assert '3 failed' in cap.text
    assert '3 passed' in cap.text


def test_run_zero_arg():
    """
    pytest tests/test_runner.py::test_run_zero_arg -s
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def zero_arg_print():
            print('running zero arg')
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_run_zero_arg.py')

        with open(modpath, 'w') as file:
            file.write(source)

        # disabled tests dont run in "all" mode
        with utils.CaptureStdout() as cap:
            try:
                runner.doctest_module(modpath, 'all', argv=[''], verbose=3)
            except Exception:
                pass
        assert 'running zero arg' not in cap.text

        with utils.CaptureStdout() as cap:
            try:
                runner.doctest_module(modpath, 'zero_arg_print', argv=[''], verbose=3)
            except Exception:
                pass
        # print(cap.text)
        assert 'running zero arg' in cap.text


def test_parse_cmdline():
    """
    pytest tests/test_runner.py::test_parse_cmdline -s
    """
    from xdoctest import runner
    # sys.argv could be anything, so just run this for coverage to make sure it doesnt crash
    runner._parse_commandline(command=None, style=None, verbose=None, argv=None)
    # check specifying argv changes style
    assert 'freeform' == runner._parse_commandline(command=None, style=None, verbose=None, argv=['--freeform'])[1]
    assert 'google' == runner._parse_commandline(command=None, style=None, verbose=None, argv=['--google'])[1]
    assert None is runner._parse_commandline(command=None, style=None, verbose=None, argv=['--google'])[0]


def test_runner_config():
    """
    pytest tests/test_runner.py::test_runner_config -s
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> print('i wanna see this')
            """
        ''')

    config = {
        'default_runtime_state': {'SKIP': True},
    }

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''], config=config)

    assert 'SKIPPED' in cap.text


def test_global_exec():
    """
    pytest tests/test_runner.py::test_global_exec -s
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> print(a)
            """
        ''')

    config = {
        'global_exec': 'a=1',
    }

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''], config=config)

    assert '1 passed' in cap.text


def test_hack_the_sys_argv():
    """
    Tests hacky solution to issue #76

    NOTE: in version 1.0.2 this hack no longer works!

    pytest tests/test_runner.py::test_hack_the_sys_argv -s

    References:
        https://github.com/Erotemic/xdoctest/issues/76
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> # xdoctest: +REQUIRES(--hackedflag)
                    >>> print('This will run if global_exec specified')
            """
        ''')

    import sys
    NEEDS_FIX = '--hackedflag' not in sys.argv

    config = {
        'global_exec': 'import sys; sys.argv.append("--hackedflag")'
    }

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''], config=config)

    if 0 and NEEDS_FIX:
        # Fix the global state
        sys.argv.remove('--hackedflag')

    # print(cap.text)
    assert '1 skipped' in cap.text
    # assert '1 passed' in cap.text


if __name__ == '__main__':
    """
    CommandLine:
        pytest tests/test_runner.py -s
        pytest tests/test_runner.py -s
        python tests/test_runner.py test_zero_args
    """
    # import pytest
    # pytest.main([__file__])
    import xdoctest
    xdoctest.doctest_module(__file__)