Skip to content

Commit 9185c2c

Browse files
author
Antti Kaihola
committed
Ref sphinx-doc#5273: doctest: add tests for the :skipif: option
1 parent 10f01d5 commit 9185c2c

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extensions = ['sphinx.ext.doctest']
2+
3+
project = 'test project for the doctest :skipif: directive'
4+
master_doc = 'skipif'
5+
source_suffix = '.txt'
6+
exclude_patterns = ['_build']
7+
8+
doctest_global_setup = '''
9+
from test_ext_doctest import record
10+
'''
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Testing the doctest extension's `:skipif:` option
2+
=================================================
3+
4+
testsetup
5+
---------
6+
7+
.. testsetup:: group-skipif
8+
:skipif: record('testsetup', ':skipif:', True) != 'this will be True'
9+
10+
record('testsetup', 'body', True)
11+
12+
.. testsetup:: group-skipif
13+
:skipif: record('testsetup', ':skipif:', False) == 'this will be False'
14+
15+
record('testsetup', 'body', False)
16+
17+
18+
doctest
19+
-------
20+
.. doctest:: group-skipif
21+
:skipif: record('doctest', ':skipif:', True) != 'this will be True'
22+
23+
>>> print(record('doctest', 'body', True))
24+
The test is skipped, and this expected text is ignored
25+
26+
27+
.. doctest::
28+
:skipif: record('doctest', ':skipif:', False) == 'this will be False'
29+
30+
>>> print(record('doctest', 'body', False))
31+
Recorded doctest body False
32+
33+
34+
testcode and testoutput
35+
-----------------------
36+
37+
testcode skipped
38+
~~~~~~~~~~~~~~~~
39+
40+
.. testcode:: group-skipif
41+
:skipif: record('testcode', ':skipif:', True) != 'this will be True'
42+
43+
print(record('testcode', 'body', True))
44+
45+
.. testoutput:: group-skipif
46+
:skipif: record('testoutput-1', ':skipif:', True) != 'this will be True'
47+
48+
The previous testcode is skipped, and the :skipif: condition is True,
49+
so this testoutput is ignored
50+
51+
testcode executed
52+
~~~~~~~~~~~~~~~~~
53+
54+
.. testcode:: group-skipif
55+
:skipif: record('testcode', ':skipif:', False) == 'this will be False'
56+
57+
print(record('testcode', 'body', False))
58+
59+
.. testoutput:: group-skipif
60+
:skipif: record('testoutput-2', ':skipif:', False) == 'this will be False'
61+
62+
Recorded testcode body False
63+
64+
.. testoutput:: group-skipif
65+
:skipif: record('testoutput-2', ':skipif:', True) != 'this will be True'
66+
67+
The :skipif: condition is False, so this testoutput is ignored
68+
69+
70+
testcleanup
71+
-----------
72+
73+
.. testcleanup:: group-skipif
74+
:skipif: record('testcleanup', ':skipif:', True) != 'this will be True'
75+
76+
record('testcleanup', 'body', True)
77+
78+
.. testcleanup:: group-skipif
79+
:skipif: record('testcleanup', ':skipif:', False) == 'this will be False'
80+
81+
record('testcleanup', 'body', False)

tests/test_ext_doctest.py

+45
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,51 @@ def cleanup_call():
6161
cleanup_called += 1
6262

6363

64+
recorded_calls = set()
65+
66+
67+
@pytest.mark.sphinx('doctest', testroot='ext-doctest-skipif')
68+
def test_skipif(app, status, warning):
69+
"""Tests for the :skipif: option
70+
71+
The tests are separated into a different test root directory since the
72+
``app`` object only evaluates options once in its lifetime. If these tests
73+
were combined with the other doctest tests, the ``:skipif:`` evaluations
74+
would be recorded only on the first ``app.builder.build_all()`` run, i.e.
75+
in ``test_build`` above, and the assertion below would fail.
76+
77+
"""
78+
global recorded_calls
79+
recorded_calls = set()
80+
app.builder.build_all()
81+
if app.statuscode != 0:
82+
assert False, 'failures in doctests:' + status.getvalue()
83+
# The `:skipif:` expressions are always run.
84+
# Actual tests and setup/cleanup code is only run if the `:skipif:`
85+
# expression evaluates to a False value.
86+
assert recorded_calls == {('testsetup', ':skipif:', True),
87+
('testsetup', ':skipif:', False),
88+
('testsetup', 'body', False),
89+
('doctest', ':skipif:', True),
90+
('doctest', ':skipif:', False),
91+
('doctest', 'body', False),
92+
('testcode', ':skipif:', True),
93+
('testcode', ':skipif:', False),
94+
('testcode', 'body', False),
95+
('testoutput-1', ':skipif:', True),
96+
('testoutput-2', ':skipif:', True),
97+
('testoutput-2', ':skipif:', False),
98+
('testcleanup', ':skipif:', True),
99+
('testcleanup', ':skipif:', False),
100+
('testcleanup', 'body', False)}
101+
102+
103+
def record(directive, part, should_skip):
104+
global recorded_calls
105+
recorded_calls.add((directive, part, should_skip))
106+
return 'Recorded {} {} {}'.format(directive, part, should_skip)
107+
108+
64109
@pytest.mark.xfail(
65110
PY2, reason='node.source points to document instead of filename',
66111
)

0 commit comments

Comments
 (0)