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
|
from xdoctest import doctest_example
from xdoctest import utils
def test_inline_skip_directive():
"""
pytest tests/test_directive.py::test_inline_skip_directive
"""
string = utils.codeblock(
'''
>>> x = 0
>>> assert False, 'should be skipped' # doctest: +SKIP
>>> y = 0
''')
self = doctest_example.DocTest(docsrc=string)
result = self.run(on_error='raise')
# TODO: ensure that lines after the inline are run
assert result['passed']
def test_block_skip_directive():
"""
pytest tests/test_directive.py::test_block_skip_directive
"""
string = utils.codeblock(
'''
>>> x = 0
>>> # doctest: +SKIP
>>> assert False, 'should be skipped'
''')
self = doctest_example.DocTest(docsrc=string)
result = self.run(on_error='raise')
assert result['passed']
def test_multi_requires_directive():
"""
Test semi-complex case with multiple requirements in a single line
xdoctest ~/code/xdoctest/tests/test_directive.py test_multi_requires_directive
"""
string = utils.codeblock(
'''
>>> x = 0
>>> print('not-skipped')
>>> # doctest: +REQUIRES(env:NOT_EXIST, --show, module:xdoctest)
>>> print('is-skipped')
>>> assert False, 'should be skipped'
>>> # doctest: -REQUIRES(env:NOT_EXIST, module:xdoctest)
>>> print('is-skipped')
>>> assert False, 'should be skipped'
>>> # doctest: +REQUIRES(env:NOT_EXIST, --show, module:xdoctest)
>>> print('is-skipped')
>>> assert False, 'should be skipped'
>>> # doctest: -REQUIRES(env:NOT_EXIST)
>>> print('is-skipped')
>>> assert False, 'should be skipped'
>>> # doctest: -REQUIRES(--show)
>>> print('not-skipped')
>>> x = 'this will not be skipped'
>>> # doctest: -REQUIRES(env:NOT_EXIST, --show, module:xdoctest)
>>> print('not-skipped')
>>> assert x == 'this will not be skipped'
''')
self = doctest_example.DocTest(docsrc=string)
result = self.run(on_error='raise')
stdout = ''.join(list(self.logged_stdout.values()))
assert result['passed']
assert stdout.count('not-skipped') == 3
assert stdout.count('is-skipped') == 0
def test_directive_syntax_error():
string = utils.codeblock(
'''
>>> x = 0
>>> # doctest: +REQUIRES(module:xdoctest)
>>> print('not-skipped')
>>> # doctest: +REQUIRES(badsyntax)
>>> print('is-skipped')
''')
self = doctest_example.DocTest(docsrc=string)
result = self.run(on_error='return')
assert not result['passed']
assert 'Failed to parse' in result['exc_info'][1].args[0]
assert 'line 4' in result['exc_info'][1].args[0]
stdout = ''.join(list(self.logged_stdout.values()))
assert stdout.count('not-skipped') == 1
if __name__ == '__main__':
"""
CommandLine:
python ~/code/xdoctest/tests/test_directive.py
pytest ~/code/xdoctest/tests/test_directive.py
"""
import xdoctest
xdoctest.doctest_module(__file__)
|