From fbc55d097b43276cd90b02290e9de3fb0f66d6e6 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Tue, 22 Apr 2025 14:48:06 +0100 Subject: [PATCH] Use pytest's parametrize instead of subtests --- requirements.dev.in | 1 - requirements.dev.txt | 3 --- tests/test_page_snapshots.py | 51 +++++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/requirements.dev.in b/requirements.dev.in index c7e549f3..f034c960 100644 --- a/requirements.dev.in +++ b/requirements.dev.in @@ -7,5 +7,4 @@ mypy-json-report>=0.1.3 pre-commit pytest pytest-django -pytest-subtests types-requests diff --git a/requirements.dev.txt b/requirements.dev.txt index 25676463..f17b1810 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -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 diff --git a/tests/test_page_snapshots.py b/tests/test_page_snapshots.py index c0d49f18..a1f13686 100644 --- a/tests/test_page_snapshots.py +++ b/tests/test_page_snapshots.py @@ -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, @@ -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/. @@ -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)