Skip to content

Use pytest's parametrize instead of subtests #248

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

Merged
merged 1 commit into from
Apr 24, 2025
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
1 change: 0 additions & 1 deletion requirements.dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ mypy-json-report>=0.1.3
pre-commit
pytest
pytest-django
pytest-subtests
types-requests
3 changes: 0 additions & 3 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ pytest==7.1.2
# via
# -r requirements.dev.in
# pytest-django
# pytest-subtests
pytest-django==4.5.2
# via -r requirements.dev.in
pytest-subtests==0.8.0
# via -r requirements.dev.in
python-dateutil==2.8.2
# via faker
pyyaml==6.0.1
Expand Down
51 changes: 33 additions & 18 deletions tests/test_page_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from django.urls import reverse
from pytest_django.asserts import assertHTMLEqual, assertNumQueries
from pytest_django.fixtures import SettingsWrapper
from pytest_subtests import SubTests


RENDERED_VIEWS = [
parameters = [
(
"homepage.html",
5,
Expand Down Expand Up @@ -94,9 +93,27 @@
]


@pytest.mark.parametrize(
["filename", "num_queries", "url"],
parameters,
ids=[
"homepage",
"version-detail.html",
"module-detail.html",
"klass-detail.html",
"klass-detail-old.html",
"fuzzy-module-detail.html",
"fuzzy-klass-detail.html",
"fuzzy-klass-detail-old.html",
],
)
@pytest.mark.django_db
def test_page_html(
client: Client, settings: SettingsWrapper, subtests: SubTests
client: Client,
settings: SettingsWrapper,
filename: str,
num_queries: int,
url: str,
) -> None:
"""
Checks that the pages in the array above match the reference files in tests/_page_snapshots/.
Expand All @@ -118,22 +135,20 @@ def test_page_html(
# ValueError: Missing staticfiles manifest entry for 'bootstrap.css'
settings.STATICFILES_STORAGE = None

for filename, num_queries, url in RENDERED_VIEWS:
with subtests.test(url=url):
with assertNumQueries(num_queries):
response = client.get(url)
with assertNumQueries(num_queries):
response = client.get(url)

html = response.rendered_content
path = Path("tests/_page_snapshots", filename)
html = response.rendered_content
path = Path("tests/_page_snapshots", filename)

# Uncomment the below to re-generate the reference files
# when they need to change for a legitimate reason.
# DO NOT commit this uncommented!
# path.write_text(html)
# Uncomment the below to re-generate the reference files when they need to
# change for a legitimate reason.
# DO NOT commit this uncommented!
# path.write_text(html)

expected = path.read_text()
expected = path.read_text()

# This forces a useful error in the case of a mismatch.
# We have to ignore the type because accessing __wrapped__ is pretty odd.
assertHTMLEqual.__wrapped__.__self__.maxDiff = None # type: ignore
assertHTMLEqual(html, expected)
# This forces a useful error in the case of a mismatch.
# We have to ignore the type because accessing __wrapped__ is pretty odd.
assertHTMLEqual.__wrapped__.__self__.maxDiff = None # type: ignore
assertHTMLEqual(html, expected)