From e36e5aec211e1ed9d551070efc9268cb82e6201a Mon Sep 17 00:00:00 2001 From: Philipp Sommer Date: Tue, 19 Apr 2022 10:56:14 +0200 Subject: [PATCH 1/2] implement in_autosummary test function --- tests/test_autodocsumm.py | 164 +++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 80 deletions(-) diff --git a/tests/test_autodocsumm.py b/tests/test_autodocsumm.py index dfe4e5b..e7b2af5 100644 --- a/tests/test_autodocsumm.py +++ b/tests/test_autodocsumm.py @@ -18,6 +18,7 @@ limitations under the License. """ import re +import bs4 import pytest import sphinx @@ -39,22 +40,34 @@ def get_html(app, fname): return f.read() +def in_autosummary(what, html) -> bool: + soup = bs4.BeautifulSoup(html) + autosummaries = soup("table") + found = False + for tag in autosummaries: + if tag.find_all("span", string=what): + found = True + break + return found + + class TestAutosummaryDocumenter: + def test_module(self, app): app.build() html = get_html(app, 'test_module.html') - assert 'TestClass' in html - assert 'test_func' in html - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("TestClass", html) + assert in_autosummary("test_func", html) + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) # test whether the right objects are included - assert 'class_caller' in html + assert in_autosummary("class_caller", html) assert 'Caller docstring for class attribute' in html # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) try: assert 'Should be included' in html @@ -94,26 +107,17 @@ def test_module_no_nesting(self, app): app.build() html = get_html(app, 'test_module_no_nesting.html') - assert 'TestClass' in html - assert 'test_func' in html + assert in_autosummary("TestClass", html) + assert in_autosummary("test_func", html) # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) - # test that elements of TestClass are not autosummarized, since nesting is disabled. - try: - assert 'test_method' not in html - assert 'test_attr' not in html - except AssertionError: # sphinx>=3.5 - found_methods = re.findall( - 'test_method', html - ) - assert len(found_methods) == 1 - found_attrs = re.findall( - 'test_attr', html - ) - assert len(found_attrs) == 1 + # test that elements of TestClass are not autosummarized, + # since nesting is disabled. + assert not in_autosummary("test_method", html) + assert not in_autosummary("test_attr", html) # test the members are still displayed assert re.search( @@ -124,12 +128,12 @@ def test_module_no_nesting(self, app): def test_module_summary_only(self, app): app.build() html = get_html(app, 'test_module_summary_only.html') - assert 'TestClass' in html - assert 'test_func' in html + assert in_autosummary("TestClass", html) + assert in_autosummary("test_func", html) # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) assert not re.search( r'', @@ -139,18 +143,18 @@ def test_module_summary_only(self, app): def test_module_with_title(self, app): app.build() html = get_html(app, 'test_module_title.html') - assert 'TestClass' in html - assert 'test_func' in html - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("TestClass", html) + assert in_autosummary("test_func", html) + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) # test whether the right objects are included - assert 'class_caller' in html + assert in_autosummary("class_caller", html) assert 'Caller docstring for class attribute' in html # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) try: assert 'Should be included' in html except AssertionError: # sphinx>=3.5 @@ -188,12 +192,12 @@ def test_module_nosignatures(self, app): app.build() html = get_html(app, 'test_module_nosignatures.html') - assert 'TestClass' in html - assert 'test_func' in html + assert in_autosummary("TestClass", html) + assert in_autosummary("test_func", html) # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) assert not re.search( r'', @@ -206,15 +210,15 @@ def test_class(self, app): html = get_html(app, '/test_class.html') if sphinx_version[:2] > [3, 1]: - assert 'instance_attribute' in html + assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: assert ( 'dummy.TestClass.instance_attribute' in html ) - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) # test escaping of * assert r'\*args' not in html @@ -223,12 +227,12 @@ def test_class(self, app): assert '**kwargs' in html # test whether the right objects are included - assert 'class_caller' in html + assert in_autosummary("class_caller", html) assert 'Caller docstring for class attribute' in html # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) assert 'Should be skipped' not in html try: @@ -261,15 +265,15 @@ def test_class_order(self, app): html = get_html(app, '/test_class_order.html') if sphinx_version[:2] > [3, 1]: - assert 'instance_attribute' in html + assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: assert ( 'dummy.TestClass.instance_attribute' in html ) - assert 'test_attr' in html - assert 'large_data' in html + assert in_autosummary("test_attr", html) + assert in_autosummary("large_data", html) assert ( html.index('test_attr') @@ -281,22 +285,22 @@ def test_class_summary_only(self, app): html = get_html(app, '/test_class_summary_only.html') if sphinx_version[:2] > [3, 1]: - assert 'instance_attribute' in html + assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: assert ( 'dummy.TestClass.instance_attribute' in html ) - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) # test whether the right objects are included - assert 'class_caller' in html + assert in_autosummary("class_caller", html) # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) assert not re.search( r'', @@ -308,22 +312,22 @@ def test_class_nosignatures(self, app): html = get_html(app, '/test_class_nosignatures.html') if sphinx_version[:2] > [3, 1]: - assert 'instance_attribute' in html + assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: assert ( 'dummy.TestClass.instance_attribute' in html ) - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) # test whether the right objects are included - assert 'class_caller' in html + assert in_autosummary("class_caller", html) # test whether the data is shown correctly - assert 'large_data' in html - assert 'small_data' in html + assert in_autosummary("large_data", html) + assert in_autosummary("small_data", html) assert not re.search( r'', @@ -335,7 +339,7 @@ def test_class_nosignatures(self, app): def test_inherited(self, app): app.build() html = get_html(app, '/test_inherited.html') - assert 'test_method' in html + assert in_autosummary("test_method", html) @pytest.mark.xfail def test_warnings_depreciation(self, app): @@ -376,8 +380,8 @@ def test_autoclasssumm(self, app): assert "Class test for autosummary" not in html # test if the methods and attributes are there in a table - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) def test_autoclasssumm_no_titles(self, app): """Test building the autosummary of a class.""" @@ -389,8 +393,8 @@ def test_autoclasssumm_no_titles(self, app): assert "Class test for autosummary" not in html # test if the methods and attributes are there in a table - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) assert "Methods" not in html @@ -404,9 +408,9 @@ def test_autoclasssumm_some_sections(self, app): assert "Class test for autosummary" not in html # test if the methods and attributes are there in a table - assert 'test_method' not in html - assert 'class_caller' in html - assert 'test_attr' in html + assert not in_autosummary("test_method", html) + assert in_autosummary("class_caller", html) + assert in_autosummary("test_attr", html) def test_autoclasssumm_nosignatures(self, app): """Test building the autosummary of a class without signatures.""" @@ -418,8 +422,8 @@ def test_autoclasssumm_nosignatures(self, app): assert "Class test for autosummary" not in html # test if the methods and attributes are there in a table - assert 'test_method' in html - assert 'test_attr' in html + assert in_autosummary("test_method", html) + assert in_autosummary("test_attr", html) assert '()' not in html @@ -433,9 +437,9 @@ def test_automodulesumm(self, app): assert "Module for testing the autodocsumm" not in html # test if the classes, data and functions are there in a table - assert 'Class_CallTest' in html - assert 'large_data' in html - assert 'test_func' in html + assert in_autosummary("Class_CallTest", html) + assert in_autosummary("large_data", html) + assert in_autosummary("test_func", html) def test_automodulesumm_some_sections(self, app): """Test building the autosummary of a module with some sections only.""" @@ -447,9 +451,9 @@ def test_automodulesumm_some_sections(self, app): assert "Module for testing the autodocsumm" not in html # test if the classes, data and functions are there in a table - assert 'Class_CallTest' not in html - assert 'large_data' in html - assert 'test_func' in html + assert not in_autosummary("Class_CallTest", html) + assert in_autosummary("large_data", html) + assert in_autosummary("test_func", html) def test_automodulesumm_nosignatures(self, app): """Test building the autosummary of a module without signatures.""" @@ -461,9 +465,9 @@ def test_automodulesumm_nosignatures(self, app): assert "Module for testing the autodocsumm" not in html # test if the classes, data and functions are there in a table - assert 'Class_CallTest' in html - assert 'large_data' in html - assert 'test_func' in html + assert in_autosummary("Class_CallTest", html) + assert in_autosummary("large_data", html) + assert in_autosummary("test_func", html) assert '()' not in html @@ -472,4 +476,4 @@ def test_empty(self, app): html = get_html(app, '/test_empty.html') - assert 'product' not in html + assert not in_autosummary("product", html) From e7af083c3859659bd8b60e0a8e2a9276236f0fdd Mon Sep 17 00:00:00 2001 From: Philipp Sommer Date: Tue, 19 Apr 2022 11:33:22 +0200 Subject: [PATCH 2/2] install beautifulsoup4 in ci --- .github/workflows/python-app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 1066d80..9cce0df 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -36,7 +36,7 @@ jobs: SPHINX="${SPHINX}==${SPHINX_VERSION}"; JINJA2="${JINJA2}<3.1"; fi - pip install pytest pytest-cov codecov "${SPHINX}" "${JINJA2}" -e . + pip install pytest pytest-cov codecov "${SPHINX}" "${JINJA2}" beautifulsoup4 -e . - name: Test with pytest run: | pytest --cov=autodocsumm --cov-report=xml tests