Skip to content

Commit 073d513

Browse files
authored
Merge pull request #72 from Chilipp/use-bs4-for-tests
Use bs4 for tests
2 parents cf82dc3 + e7af083 commit 073d513

File tree

2 files changed

+85
-81
lines changed

2 files changed

+85
-81
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
SPHINX="${SPHINX}==${SPHINX_VERSION}";
3737
JINJA2="${JINJA2}<3.1";
3838
fi
39-
pip install pytest pytest-cov codecov "${SPHINX}" "${JINJA2}" -e .
39+
pip install pytest pytest-cov codecov "${SPHINX}" "${JINJA2}" beautifulsoup4 -e .
4040
- name: Test with pytest
4141
run: |
4242
pytest --cov=autodocsumm --cov-report=xml tests

tests/test_autodocsumm.py

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
limitations under the License.
1919
"""
2020
import re
21+
import bs4
2122
import pytest
2223
import sphinx
2324

@@ -39,22 +40,34 @@ def get_html(app, fname):
3940
return f.read()
4041

4142

43+
def in_autosummary(what, html) -> bool:
44+
soup = bs4.BeautifulSoup(html)
45+
autosummaries = soup("table")
46+
found = False
47+
for tag in autosummaries:
48+
if tag.find_all("span", string=what):
49+
found = True
50+
break
51+
return found
52+
53+
4254
class TestAutosummaryDocumenter:
55+
4356
def test_module(self, app):
4457
app.build()
4558
html = get_html(app, 'test_module.html')
46-
assert '<span class="pre">TestClass</span>' in html
47-
assert '<span class="pre">test_func</span>' in html
48-
assert '<span class="pre">test_method</span>' in html
49-
assert '<span class="pre">test_attr</span>' in html
59+
assert in_autosummary("TestClass", html)
60+
assert in_autosummary("test_func", html)
61+
assert in_autosummary("test_method", html)
62+
assert in_autosummary("test_attr", html)
5063

5164
# test whether the right objects are included
52-
assert '<span class="pre">class_caller</span>' in html
65+
assert in_autosummary("class_caller", html)
5366
assert 'Caller docstring for class attribute' in html
5467

5568
# test whether the data is shown correctly
56-
assert '<span class="pre">large_data</span>' in html
57-
assert '<span class="pre">small_data</span>' in html
69+
assert in_autosummary("large_data", html)
70+
assert in_autosummary("small_data", html)
5871

5972
try:
6073
assert 'Should be included' in html
@@ -94,26 +107,17 @@ def test_module_no_nesting(self, app):
94107
app.build()
95108
html = get_html(app, 'test_module_no_nesting.html')
96109

97-
assert '<span class="pre">TestClass</span>' in html
98-
assert '<span class="pre">test_func</span>' in html
110+
assert in_autosummary("TestClass", html)
111+
assert in_autosummary("test_func", html)
99112

100113
# test whether the data is shown correctly
101-
assert '<span class="pre">large_data</span>' in html
102-
assert '<span class="pre">small_data</span>' in html
114+
assert in_autosummary("large_data", html)
115+
assert in_autosummary("small_data", html)
103116

104-
# test that elements of TestClass are not autosummarized, since nesting is disabled.
105-
try:
106-
assert '<span class="pre">test_method</span>' not in html
107-
assert '<span class="pre">test_attr</span>' not in html
108-
except AssertionError: # sphinx>=3.5
109-
found_methods = re.findall(
110-
'<span class="pre">test_method</span>', html
111-
)
112-
assert len(found_methods) == 1
113-
found_attrs = re.findall(
114-
'<span class="pre">test_attr</span>', html
115-
)
116-
assert len(found_attrs) == 1
117+
# test that elements of TestClass are not autosummarized,
118+
# since nesting is disabled.
119+
assert not in_autosummary("test_method", html)
120+
assert not in_autosummary("test_attr", html)
117121

118122
# test the members are still displayed
119123
assert re.search(
@@ -124,12 +128,12 @@ def test_module_no_nesting(self, app):
124128
def test_module_summary_only(self, app):
125129
app.build()
126130
html = get_html(app, 'test_module_summary_only.html')
127-
assert '<span class="pre">TestClass</span>' in html
128-
assert '<span class="pre">test_func</span>' in html
131+
assert in_autosummary("TestClass", html)
132+
assert in_autosummary("test_func", html)
129133

130134
# test whether the data is shown correctly
131-
assert '<span class="pre">large_data</span>' in html
132-
assert '<span class="pre">small_data</span>' in html
135+
assert in_autosummary("large_data", html)
136+
assert in_autosummary("small_data", html)
133137

134138
assert not re.search(
135139
r'<dt( class=".*")? id="dummy.Class_CallTest"( class=".*")*>',
@@ -139,18 +143,18 @@ def test_module_summary_only(self, app):
139143
def test_module_with_title(self, app):
140144
app.build()
141145
html = get_html(app, 'test_module_title.html')
142-
assert '<span class="pre">TestClass</span>' in html
143-
assert '<span class="pre">test_func</span>' in html
144-
assert '<span class="pre">test_method</span>' in html
145-
assert '<span class="pre">test_attr</span>' in html
146+
assert in_autosummary("TestClass", html)
147+
assert in_autosummary("test_func", html)
148+
assert in_autosummary("test_method", html)
149+
assert in_autosummary("test_attr", html)
146150

147151
# test whether the right objects are included
148-
assert '<span class="pre">class_caller</span>' in html
152+
assert in_autosummary("class_caller", html)
149153
assert 'Caller docstring for class attribute' in html
150154

151155
# test whether the data is shown correctly
152-
assert '<span class="pre">large_data</span>' in html
153-
assert '<span class="pre">small_data</span>' in html
156+
assert in_autosummary("large_data", html)
157+
assert in_autosummary("small_data", html)
154158
try:
155159
assert 'Should be included' in html
156160
except AssertionError: # sphinx>=3.5
@@ -188,12 +192,12 @@ def test_module_nosignatures(self, app):
188192
app.build()
189193

190194
html = get_html(app, 'test_module_nosignatures.html')
191-
assert '<span class="pre">TestClass</span>' in html
192-
assert '<span class="pre">test_func</span>' in html
195+
assert in_autosummary("TestClass", html)
196+
assert in_autosummary("test_func", html)
193197

194198
# test whether the data is shown correctly
195-
assert '<span class="pre">large_data</span>' in html
196-
assert '<span class="pre">small_data</span>' in html
199+
assert in_autosummary("large_data", html)
200+
assert in_autosummary("small_data", html)
197201

198202
assert not re.search(
199203
r'<dt( class=".*")? id="dummy.Class_CallTest"( class=".*")*>',
@@ -206,15 +210,15 @@ def test_class(self, app):
206210
html = get_html(app, '/test_class.html')
207211

208212
if sphinx_version[:2] > [3, 1]:
209-
assert '<span class="pre">instance_attribute</span>' in html
213+
assert in_autosummary("instance_attribute", html)
210214
elif sphinx_version[:2] < [3, 1]:
211215
assert (
212216
'<span class="pre">dummy.TestClass.instance_attribute</span>'
213217
in html
214218
)
215219

216-
assert '<span class="pre">test_method</span>' in html
217-
assert '<span class="pre">test_attr</span>' in html
220+
assert in_autosummary("test_method", html)
221+
assert in_autosummary("test_attr", html)
218222

219223
# test escaping of *
220224
assert r'\*args' not in html
@@ -223,12 +227,12 @@ def test_class(self, app):
223227
assert '**kwargs' in html
224228

225229
# test whether the right objects are included
226-
assert '<span class="pre">class_caller</span>' in html
230+
assert in_autosummary("class_caller", html)
227231
assert 'Caller docstring for class attribute' in html
228232

229233
# test whether the data is shown correctly
230-
assert '<span class="pre">large_data</span>' in html
231-
assert '<span class="pre">small_data</span>' in html
234+
assert in_autosummary("large_data", html)
235+
assert in_autosummary("small_data", html)
232236

233237
assert 'Should be skipped' not in html
234238
try:
@@ -261,15 +265,15 @@ def test_class_order(self, app):
261265
html = get_html(app, '/test_class_order.html')
262266

263267
if sphinx_version[:2] > [3, 1]:
264-
assert '<span class="pre">instance_attribute</span>' in html
268+
assert in_autosummary("instance_attribute", html)
265269
elif sphinx_version[:2] < [3, 1]:
266270
assert (
267271
'<span class="pre">dummy.TestClass.instance_attribute</span>'
268272
in html
269273
)
270274

271-
assert '<span class="pre">test_attr</span>' in html
272-
assert '<span class="pre">large_data</span>' in html
275+
assert in_autosummary("test_attr", html)
276+
assert in_autosummary("large_data", html)
273277

274278
assert (
275279
html.index('<span class="pre">test_attr</span>')
@@ -281,22 +285,22 @@ def test_class_summary_only(self, app):
281285
html = get_html(app, '/test_class_summary_only.html')
282286

283287
if sphinx_version[:2] > [3, 1]:
284-
assert '<span class="pre">instance_attribute</span>' in html
288+
assert in_autosummary("instance_attribute", html)
285289
elif sphinx_version[:2] < [3, 1]:
286290
assert (
287291
'<span class="pre">dummy.TestClass.instance_attribute</span>'
288292
in html
289293
)
290294

291-
assert '<span class="pre">test_method</span>' in html
292-
assert '<span class="pre">test_attr</span>' in html
295+
assert in_autosummary("test_method", html)
296+
assert in_autosummary("test_attr", html)
293297

294298
# test whether the right objects are included
295-
assert '<span class="pre">class_caller</span>' in html
299+
assert in_autosummary("class_caller", html)
296300

297301
# test whether the data is shown correctly
298-
assert '<span class="pre">large_data</span>' in html
299-
assert '<span class="pre">small_data</span>' in html
302+
assert in_autosummary("large_data", html)
303+
assert in_autosummary("small_data", html)
300304

301305
assert not re.search(
302306
r'<dt( class=".*")? id="dummy.TestClass.small_data"( class=".*")*>',
@@ -308,22 +312,22 @@ def test_class_nosignatures(self, app):
308312
html = get_html(app, '/test_class_nosignatures.html')
309313

310314
if sphinx_version[:2] > [3, 1]:
311-
assert '<span class="pre">instance_attribute</span>' in html
315+
assert in_autosummary("instance_attribute", html)
312316
elif sphinx_version[:2] < [3, 1]:
313317
assert (
314318
'<span class="pre">dummy.TestClass.instance_attribute</span>'
315319
in html
316320
)
317321

318-
assert '<span class="pre">test_method</span>' in html
319-
assert '<span class="pre">test_attr</span>' in html
322+
assert in_autosummary("test_method", html)
323+
assert in_autosummary("test_attr", html)
320324

321325
# test whether the right objects are included
322-
assert '<span class="pre">class_caller</span>' in html
326+
assert in_autosummary("class_caller", html)
323327

324328
# test whether the data is shown correctly
325-
assert '<span class="pre">large_data</span>' in html
326-
assert '<span class="pre">small_data</span>' in html
329+
assert in_autosummary("large_data", html)
330+
assert in_autosummary("small_data", html)
327331

328332
assert not re.search(
329333
r'<dt( class=".*")? id="dummy.TestClass.small_data"( class=".*")*>',
@@ -335,7 +339,7 @@ def test_class_nosignatures(self, app):
335339
def test_inherited(self, app):
336340
app.build()
337341
html = get_html(app, '/test_inherited.html')
338-
assert '<span class="pre">test_method</span>' in html
342+
assert in_autosummary("test_method", html)
339343

340344
@pytest.mark.xfail
341345
def test_warnings_depreciation(self, app):
@@ -376,8 +380,8 @@ def test_autoclasssumm(self, app):
376380
assert "Class test for autosummary" not in html
377381

378382
# test if the methods and attributes are there in a table
379-
assert '<span class="pre">test_method</span>' in html
380-
assert '<span class="pre">test_attr</span>' in html
383+
assert in_autosummary("test_method", html)
384+
assert in_autosummary("test_attr", html)
381385

382386
def test_autoclasssumm_no_titles(self, app):
383387
"""Test building the autosummary of a class."""
@@ -389,8 +393,8 @@ def test_autoclasssumm_no_titles(self, app):
389393
assert "Class test for autosummary" not in html
390394

391395
# test if the methods and attributes are there in a table
392-
assert '<span class="pre">test_method</span>' in html
393-
assert '<span class="pre">test_attr</span>' in html
396+
assert in_autosummary("test_method", html)
397+
assert in_autosummary("test_attr", html)
394398

395399
assert "<strong>Methods</strong>" not in html
396400

@@ -404,9 +408,9 @@ def test_autoclasssumm_some_sections(self, app):
404408
assert "Class test for autosummary" not in html
405409

406410
# test if the methods and attributes are there in a table
407-
assert '<span class="pre">test_method</span>' not in html
408-
assert '<span class="pre">class_caller</span>' in html
409-
assert '<span class="pre">test_attr</span>' in html
411+
assert not in_autosummary("test_method", html)
412+
assert in_autosummary("class_caller", html)
413+
assert in_autosummary("test_attr", html)
410414

411415
def test_autoclasssumm_nosignatures(self, app):
412416
"""Test building the autosummary of a class without signatures."""
@@ -418,8 +422,8 @@ def test_autoclasssumm_nosignatures(self, app):
418422
assert "Class test for autosummary" not in html
419423

420424
# test if the methods and attributes are there in a table
421-
assert '<span class="pre">test_method</span>' in html
422-
assert '<span class="pre">test_attr</span>' in html
425+
assert in_autosummary("test_method", html)
426+
assert in_autosummary("test_attr", html)
423427

424428
assert '()' not in html
425429

@@ -433,9 +437,9 @@ def test_automodulesumm(self, app):
433437
assert "Module for testing the autodocsumm" not in html
434438

435439
# test if the classes, data and functions are there in a table
436-
assert '<span class="pre">Class_CallTest</span>' in html
437-
assert '<span class="pre">large_data</span>' in html
438-
assert '<span class="pre">test_func</span>' in html
440+
assert in_autosummary("Class_CallTest", html)
441+
assert in_autosummary("large_data", html)
442+
assert in_autosummary("test_func", html)
439443

440444
def test_automodulesumm_some_sections(self, app):
441445
"""Test building the autosummary of a module with some sections only."""
@@ -447,9 +451,9 @@ def test_automodulesumm_some_sections(self, app):
447451
assert "Module for testing the autodocsumm" not in html
448452

449453
# test if the classes, data and functions are there in a table
450-
assert '<span class="pre">Class_CallTest</span>' not in html
451-
assert '<span class="pre">large_data</span>' in html
452-
assert '<span class="pre">test_func</span>' in html
454+
assert not in_autosummary("Class_CallTest", html)
455+
assert in_autosummary("large_data", html)
456+
assert in_autosummary("test_func", html)
453457

454458
def test_automodulesumm_nosignatures(self, app):
455459
"""Test building the autosummary of a module without signatures."""
@@ -461,9 +465,9 @@ def test_automodulesumm_nosignatures(self, app):
461465
assert "Module for testing the autodocsumm" not in html
462466

463467
# test if the classes, data and functions are there in a table
464-
assert '<span class="pre">Class_CallTest</span>' in html
465-
assert '<span class="pre">large_data</span>' in html
466-
assert '<span class="pre">test_func</span>' in html
468+
assert in_autosummary("Class_CallTest", html)
469+
assert in_autosummary("large_data", html)
470+
assert in_autosummary("test_func", html)
467471

468472
assert '()' not in html
469473

@@ -472,4 +476,4 @@ def test_empty(self, app):
472476

473477
html = get_html(app, '/test_empty.html')
474478

475-
assert '<span class="pre">product</span>' not in html
479+
assert not in_autosummary("product", html)

0 commit comments

Comments
 (0)