Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f223c77

Browse files
adjusted the test fixtures to be more pytest-y
1 parent acac44c commit f223c77

File tree

5 files changed

+44
-64
lines changed

5 files changed

+44
-64
lines changed

‎tests/test_generic_views/test_base.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,17 @@ async def test_direct_instantiation(self):
305305
assert response.status_code == 405
306306

307307

308+
@pytest.fixture(autouse=True)
309+
def urlconf_setting_set(settings):
310+
old_urlconf = settings.ROOT_URLCONF
311+
settings.ROOT_URLCONF = "test_generic_views.urls"
312+
yield settings
313+
settings.ROOT_URLCONF = old_urlconf
314+
315+
308316
class TestAsyncTemplateView:
309317
rf = RequestFactory()
310318

311-
@pytest.fixture(autouse=True)
312-
def urlconf_for_tests(self, settings):
313-
settings.ROOT_URLCONF = "test_generic_views.urls"
314-
315319
def _assert_about(self, response):
316320
response.render()
317321
assert b"<h1>About</h1>" in response.content
@@ -455,10 +459,6 @@ def test_extra_context(self):
455459
class TestAsyncRedirectView:
456460
rf = RequestFactory()
457461

458-
@pytest.fixture(autouse=True)
459-
def urlconf_for_tests(self, settings):
460-
settings.ROOT_URLCONF = "test_generic_views.urls"
461-
462462
async def test_no_url(self):
463463
"Without any configuration, returns HTTP 410 GONE"
464464
response = await AsyncRedirectView.as_view()(self.rf.get("/foo/"))

‎tests/test_generic_views/test_dates.py‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from django.db import connection
55
from pytest_django.asserts import assertNumQueries
66

7-
from django.conf import settings
87
from django.core.exceptions import ImproperlyConfigured
98
from django.test import Client
109
from django.test.utils import TZ_SUPPORT
@@ -50,12 +49,16 @@ def setup(self):
5049
)
5150

5251

52+
@pytest.fixture(autouse=True)
53+
def url_setting_set(settings):
54+
old_root_urlconf = settings.ROOT_URLCONF
55+
settings.ROOT_URLCONF = "test_generic_views.urls"
56+
yield settings
57+
settings.ROOT_URLCONF = old_root_urlconf
58+
59+
5360
@pytest.mark.django_db
5461
class TestArchiveIndexView(TestDataMixin):
55-
@pytest.fixture(autouse=True)
56-
def url_setting_set(self):
57-
settings.ROOT_URLCONF = "test_generic_views.urls"
58-
5962
def test_archive_view(self):
6063
res = client.get("/dates/books/")
6164
assert res.status_code == 200
@@ -216,10 +219,6 @@ def test_archive_view_without_date_field(self):
216219

217220
@pytest.mark.django_db
218221
class TestYearArchiveView(TestDataMixin):
219-
@pytest.fixture(autouse=True)
220-
def url_setting_set(self):
221-
settings.ROOT_URLCONF = "test_generic_views.urls"
222-
223222
def test_year_view(self):
224223
res = client.get("/dates/books/2008/")
225224
assert res.status_code == 200
@@ -389,10 +388,6 @@ def test_get_dated_items_not_implemented(self):
389388

390389
@pytest.mark.django_db
391390
class TestMonthArchiveView(TestDataMixin):
392-
@pytest.fixture(autouse=True)
393-
def url_setting_set(self):
394-
settings.ROOT_URLCONF = "test_generic_views.urls"
395-
396391
def test_month_view(self):
397392
res = client.get("/dates/books/2008/oct/")
398393
assert res.status_code == 200
@@ -552,10 +547,6 @@ def test_date_list_order(self):
552547

553548
@pytest.mark.django_db
554549
class TestWeekArchiveView(TestDataMixin):
555-
@pytest.fixture(autouse=True)
556-
def url_setting_set(self):
557-
settings.ROOT_URLCONF = "test_generic_views.urls"
558-
559550
def test_week_view(self):
560551
res = client.get("/dates/books/2008/week/39/")
561552
assert res.status_code == 200
@@ -692,10 +683,6 @@ def test_aware_datetime_week_view(self, settings):
692683

693684
@pytest.mark.django_db
694685
class TestDayArchiveView(TestDataMixin):
695-
@pytest.fixture(autouse=True)
696-
def url_setting_set(self):
697-
settings.ROOT_URLCONF = "test_generic_views.urls"
698-
699686
def test_day_view(self):
700687
res = client.get("/dates/books/2008/oct/01/")
701688
assert res.status_code == 200
@@ -837,10 +824,6 @@ def test_aware_datetime_day_view(self, settings):
837824

838825
@pytest.mark.django_db
839826
class TestDateDetailView(TestDataMixin):
840-
@pytest.fixture(autouse=True)
841-
def url_setting_set(self):
842-
settings.ROOT_URLCONF = "test_generic_views.urls"
843-
844827
def test_date_detail_by_pk(self):
845828
res = client.get("/dates/books/2008/oct/01/%s/" % self.book1.pk)
846829
assert res.status_code == 200

‎tests/test_generic_views/test_detail.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44

5-
from django.conf import settings
65
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
76
from django.test import Client
87
from django.test.client import RequestFactory
@@ -16,12 +15,16 @@
1615
client = Client()
1716

1817

18+
@pytest.fixture(autouse=True)
19+
def url_setting_set(settings):
20+
old_root_urlconf = settings.ROOT_URLCONF
21+
settings.ROOT_URLCONF = "test_generic_views.urls"
22+
yield settings
23+
settings.ROOT_URLCONF = old_root_urlconf
24+
25+
1926
@pytest.mark.django_db
2027
class TestAsyncDetailView:
21-
@pytest.fixture(autouse=True)
22-
def urlconf_for_tests(self):
23-
settings.ROOT_URLCONF = "test_generic_views.urls"
24-
2528
@pytest.fixture(autouse=True)
2629
def setup(self):
2730
self.artist1 = Artist.objects.create(name="Rene Magritte")

‎tests/test_generic_views/test_edit.py‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import re
22

33
from django import forms
4-
from django.conf import settings
54
from django.core.exceptions import ImproperlyConfigured
65
from django.test import Client, AsyncClient
76
from django.test.client import RequestFactory
@@ -76,15 +75,19 @@ class FormContext(AsyncFormMixin):
7675
assert isinstance(context_data["form"], forms.Form)
7776

7877

78+
@pytest.fixture(autouse=True)
79+
def url_setting_set(settings):
80+
old_root_urlconf = settings.ROOT_URLCONF
81+
settings.ROOT_URLCONF = "test_generic_views.urls"
82+
yield settings
83+
settings.ROOT_URLCONF = old_root_urlconf
84+
85+
7986
@pytest.mark.django_db
8087
class TestBasicForm:
81-
@pytest.fixture(autouse=True)
82-
def urlconf_for_tests(self):
83-
settings.ROOT_URLCONF = "test_generic_views.urls"
84-
85-
# def test_post_data(self):
86-
# res = client.post("/contact/", {"name": "Me", "message": "Hello"})
87-
# assertRedirects(res, "/list/authors/")
88+
def test_post_data(self):
89+
res = client.post("/contact/", {"name": "Me", "message": "Hello"})
90+
assertRedirects(res, "/list/authors/")
8891

8992
async def test_late_form_validation(self):
9093
"""
@@ -109,10 +112,6 @@ def test_get_form_checks_for_object(self):
109112

110113
@pytest.mark.django_db
111114
class TestCreateView:
112-
@pytest.fixture(autouse=True)
113-
def urlconf_for_tests(self):
114-
settings.ROOT_URLCONF = "test_generic_views.urls"
115-
116115
def test_create(self):
117116
res = client.get("/edit/authors/create/")
118117
assert res.status_code == 200
@@ -255,10 +254,6 @@ class MyCreateView(AsyncCreateView):
255254

256255
@pytest.mark.django_db
257256
class TestUpdateView:
258-
@pytest.fixture(autouse=True)
259-
def urlconf_for_tests(self):
260-
settings.ROOT_URLCONF = "test_generic_views.urls"
261-
262257
@pytest.fixture(autouse=True)
263258
def setup(cls):
264259
cls.author = Author.objects.create(
@@ -392,10 +387,6 @@ def test_update_get_object(self):
392387

393388
@pytest.mark.django_db
394389
class TestDeleteView:
395-
@pytest.fixture(autouse=True)
396-
def urlconf_for_tests(self):
397-
settings.ROOT_URLCONF = "test_generic_views.urls"
398-
399390
@pytest.fixture(autouse=True)
400391
def setup(cls):
401392
cls.author = Author.objects.create(

‎tests/test_generic_views/test_list.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import pytest
55

6-
from django.conf import settings
76
from django.core.exceptions import ImproperlyConfigured
87
from django.test import Client, TestCase
98

@@ -14,12 +13,16 @@
1413
client = Client()
1514

1615

16+
@pytest.fixture(autouse=True)
17+
def url_setting_set(settings):
18+
old_root_urlconf = settings.ROOT_URLCONF
19+
settings.ROOT_URLCONF = "test_generic_views.urls"
20+
yield settings
21+
settings.ROOT_URLCONF = old_root_urlconf
22+
23+
1724
@pytest.mark.django_db(transaction=True)
1825
class ListViewTests(TestCase):
19-
@pytest.fixture(autouse=True)
20-
def urlconf_for_tests(self):
21-
settings.ROOT_URLCONF = "test_generic_views.urls"
22-
2326
@classmethod
2427
def setUpTestData(self):
2528
self.artist1 = Artist.objects.create(name="Rene Magritte")
@@ -259,7 +262,7 @@ def test_explicitly_ordered_list_view(self):
259262
assert res.context["object_list"][2].name == "2066"
260263

261264
@pytest.fixture(autouse=True)
262-
def toggle_debug_tests(self):
265+
def toggle_debug_tests(self, settings):
263266
settings.DEBUG = True
264267

265268
def test_paginated_list_view_returns_useful_message_on_invalid_page(self):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /