Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests for scalability #32

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/roots/test-figclass/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

extensions = ['sphinxcontrib.screenshot']
2 changes: 2 additions & 0 deletions tests/roots/test-figclass/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.. screenshot:: http://www.example.com
:figclass: round
15 changes: 15 additions & 0 deletions tests/roots/test-pdf/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

extensions = ['sphinxcontrib.screenshot']
2 changes: 2 additions & 0 deletions tests/roots/test-pdf/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.. screenshot:: http://www.example.com
:pdf:
10 changes: 0 additions & 10 deletions tests/roots/test-root/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,3 @@
:caption: Changing the background.

document.body.style.background = 'red';

.. screenshot:: http://www.example.com
:width: 480
:height: 320
:figclass: round

.. screenshot:: http://www.example.com
:width: 640
:height: 640
:pdf:
28 changes: 28 additions & 0 deletions tests/test_figclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from bs4 import BeautifulSoup
from sphinx.testing.util import SphinxTestApp


@pytest.mark.sphinx('html', testroot='figclass')
def test_default(app: SphinxTestApp) -> None:
app.build()
out_html = app.outdir / "index.html"
soup = BeautifulSoup(out_html.read_text(), "html.parser")

# The figure node should have the class name specified.
figure = soup.select_one('figure.round')
assert figure is not None
34 changes: 34 additions & 0 deletions tests/test_pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import pytest
from bs4 import BeautifulSoup
from sphinx.testing.util import SphinxTestApp


@pytest.mark.sphinx('html', testroot='pdf')
def test_default(app: SphinxTestApp) -> None:
app.build()
out_html = app.outdir / "index.html"
soup = BeautifulSoup(out_html.read_text(), "html.parser")
img = soup.select_one('img')
assert img

imgsrc = str(img['src'])
root, ext = os.path.splitext(os.path.basename(imgsrc))
pdf_filepath = app.outdir / '_static' / 'screenshots' / f'{root}.pdf'
# Should generate a screenshot PDF.
assert os.path.exists(pdf_filepath)
47 changes: 17 additions & 30 deletions tests/test_it.py → tests/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from io import StringIO

import pytest
from bs4 import BeautifulSoup
from PIL import Image
from sphinx.testing.util import SphinxTestApp


@pytest.mark.sphinx('html')
def test_default(app: SphinxTestApp, status: StringIO,
warning: StringIO) -> None:
@pytest.mark.sphinx('html', testroot='root')
def test_default(app: SphinxTestApp) -> None:
app.build()
out_html = app.outdir / "index.html"
soup = BeautifulSoup(out_html.read_text(), "html.parser")

# Every screenshot directive should become an image.
imgs = soup.find_all('img')
assert len(list(imgs)) == 5
assert len(list(imgs)) == 3

# The image size should be set as specified.
img_obj = Image.open(app.outdir / imgs[0]['src'])
Expand All @@ -39,32 +35,23 @@ def test_default(app: SphinxTestApp, status: StringIO,
assert height == 320

# The caption should be rendered.
figcaption = soup.select_one('figcaption span')
assert figcaption and (figcaption.get_text().strip()
== 'This is a test screenshot')

# The images should be different after the specified user interaction.
img_before_interaction = Image.open(app.outdir / imgs[0]['src'])
img_after_interaction = Image.open(app.outdir / imgs[2]['src'])
assert list(img_before_interaction.getdata()) != list(
img_after_interaction.getdata())
figcaptions = [
figcaption.get_text().strip()
for figcaption in soup.select('figcaption span')
]
assert figcaptions == [
'This is a test screenshot', 'This is another screenshot',
'Changing the background.'
]

# The images should be the same if the difference is only the caption.
img_with_caption_a = imgs[0]
img_with_caption_b = imgs[1]
assert img_with_caption_a['src'] == img_with_caption_b['src']

# The figure node should have the class name specified.
assert 'round' in soup.find_all('figure')[3]['class']

# Should generate a PDF file if specified.
img_with_pdf = imgs[4]['src']
root, ext = os.path.splitext(os.path.basename(img_with_pdf))
pdf_filepath = app.outdir / '_static' / 'screenshots' / f'{root}.pdf'
assert os.path.exists(pdf_filepath)

# Should not generate a PDF file if not specified.
img_without_pdf = imgs[2]['src']
root, ext = os.path.splitext(os.path.basename(img_without_pdf))
pdf_filepath = app.outdir / '_static' / 'screenshots' / f'{root}.pdf'
assert not os.path.exists(pdf_filepath)
# The images should be different after the specified user interaction.
imgsrc_before_interaction = app.outdir / imgs[1]['src']
imgsrc_after_interaction = app.outdir / imgs[2]['src']
assert imgsrc_before_interaction != imgsrc_after_interaction
assert list(Image.open(imgsrc_before_interaction).getdata()) != list(
Image.open(imgsrc_after_interaction).getdata())
Loading