mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Ref #5273: doctest: add test for skipif calling global setup/cleanup
Ensure that `doctest_global_setup` and `doctest_global_cleanup` are executed before and after evaluating each `:skipif:` option.
This commit is contained in:
@@ -7,4 +7,10 @@ exclude_patterns = ['_build']
|
|||||||
|
|
||||||
doctest_global_setup = '''
|
doctest_global_setup = '''
|
||||||
from test_ext_doctest import record
|
from test_ext_doctest import record
|
||||||
|
|
||||||
|
record('doctest_global_setup', 'body', True)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
doctest_global_cleanup = '''
|
||||||
|
record('doctest_global_cleanup', 'body', True)
|
||||||
|
'''
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
:license: BSD, see LICENSE for details.
|
:license: BSD, see LICENSE for details.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from packaging.specifiers import InvalidSpecifier
|
from packaging.specifiers import InvalidSpecifier
|
||||||
@@ -61,7 +62,7 @@ def cleanup_call():
|
|||||||
cleanup_called += 1
|
cleanup_called += 1
|
||||||
|
|
||||||
|
|
||||||
recorded_calls = set()
|
recorded_calls = Counter()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('doctest', testroot='ext-doctest-skipif')
|
@pytest.mark.sphinx('doctest', testroot='ext-doctest-skipif')
|
||||||
@@ -76,33 +77,38 @@ def test_skipif(app, status, warning):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
global recorded_calls
|
global recorded_calls
|
||||||
recorded_calls = set()
|
recorded_calls = Counter()
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
if app.statuscode != 0:
|
if app.statuscode != 0:
|
||||||
assert False, 'failures in doctests:' + status.getvalue()
|
assert False, 'failures in doctests:' + status.getvalue()
|
||||||
# The `:skipif:` expressions are always run.
|
# The `:skipif:` expressions are always run.
|
||||||
# Actual tests and setup/cleanup code is only run if the `:skipif:`
|
# Actual tests and setup/cleanup code is only run if the `:skipif:`
|
||||||
# expression evaluates to a False value.
|
# expression evaluates to a False value.
|
||||||
assert recorded_calls == {('testsetup', ':skipif:', True),
|
# Global setup/cleanup are run before/after evaluating the `:skipif:`
|
||||||
('testsetup', ':skipif:', False),
|
# option in each directive - thus 11 additional invocations for each on top
|
||||||
('testsetup', 'body', False),
|
# of the ones made for the whole test file.
|
||||||
('doctest', ':skipif:', True),
|
assert recorded_calls == {('doctest_global_setup', 'body', True): 13,
|
||||||
('doctest', ':skipif:', False),
|
('testsetup', ':skipif:', True): 1,
|
||||||
('doctest', 'body', False),
|
('testsetup', ':skipif:', False): 1,
|
||||||
('testcode', ':skipif:', True),
|
('testsetup', 'body', False): 1,
|
||||||
('testcode', ':skipif:', False),
|
('doctest', ':skipif:', True): 1,
|
||||||
('testcode', 'body', False),
|
('doctest', ':skipif:', False): 1,
|
||||||
('testoutput-1', ':skipif:', True),
|
('doctest', 'body', False): 1,
|
||||||
('testoutput-2', ':skipif:', True),
|
('testcode', ':skipif:', True): 1,
|
||||||
('testoutput-2', ':skipif:', False),
|
('testcode', ':skipif:', False): 1,
|
||||||
('testcleanup', ':skipif:', True),
|
('testcode', 'body', False): 1,
|
||||||
('testcleanup', ':skipif:', False),
|
('testoutput-1', ':skipif:', True): 1,
|
||||||
('testcleanup', 'body', False)}
|
('testoutput-2', ':skipif:', True): 1,
|
||||||
|
('testoutput-2', ':skipif:', False): 1,
|
||||||
|
('testcleanup', ':skipif:', True): 1,
|
||||||
|
('testcleanup', ':skipif:', False): 1,
|
||||||
|
('testcleanup', 'body', False): 1,
|
||||||
|
('doctest_global_cleanup', 'body', True): 13}
|
||||||
|
|
||||||
|
|
||||||
def record(directive, part, should_skip):
|
def record(directive, part, should_skip):
|
||||||
global recorded_calls
|
global recorded_calls
|
||||||
recorded_calls.add((directive, part, should_skip))
|
recorded_calls[(directive, part, should_skip)] += 1
|
||||||
return 'Recorded {} {} {}'.format(directive, part, should_skip)
|
return 'Recorded {} {} {}'.format(directive, part, should_skip)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user