Skip to content

Commit 0dbe355

Browse files
authored
Merge pull request #743 from blueyed/harden
tests: harden assertions (no warnings etc)
2 parents cdbd7f1 + 0a2a228 commit 0dbe355

11 files changed

+26
-54
lines changed

pytest_django_test/compat.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
try:
2-
from urllib2 import urlopen, HTTPError # noqa
2+
from urllib2 import urlopen, HTTPError
33
except ImportError:
4-
from urllib.request import urlopen, HTTPError # noqa
5-
6-
# Django 1.10 removes patterns, instead it is just a list
7-
try:
8-
from django.conf.urls import patterns
9-
except ImportError:
10-
11-
def patterns(prefix, *urls):
12-
assert prefix == ""
13-
return urls
4+
from urllib.request import urlopen, HTTPError # noqa: F401

pytest_django_test/urls.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from django.conf.urls import url
22

33
from .app import views
4-
from .compat import patterns
54

6-
urlpatterns = patterns(
7-
"",
5+
urlpatterns = [
86
url(r"^item_count/$", views.item_count),
97
url(r"^admin-required/$", views.admin_required_view),
10-
)
8+
]

pytest_django_test/urls_overridden.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from django.conf.urls import url
22
from django.http import HttpResponse
33

4-
from .compat import patterns
5-
6-
urlpatterns = patterns(
7-
"", url(r"^overridden_url/$", lambda r: HttpResponse("Overridden urlconf works!"))
8-
)
4+
urlpatterns = [
5+
url(r"^overridden_url/$", lambda r: HttpResponse("Overridden urlconf works!"))
6+
]

tests/test_django_configurations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_settings():
4040
"""
4141
)
4242
result = testdir.runpytest_subprocess()
43-
result.stdout.fnmatch_lines(["*1 passed*"])
43+
result.stdout.fnmatch_lines(["* 1 passed in*"])
4444
assert result.ret == 0
4545

4646

@@ -68,7 +68,7 @@ def test_ds():
6868
"""
6969
)
7070
result = testdir.runpytest_subprocess()
71-
result.stdout.fnmatch_lines(["*1 passed*"])
71+
result.stdout.fnmatch_lines(["* 1 passed in*"])
7272
assert result.ret == 0
7373

7474

@@ -96,5 +96,5 @@ def test_ds():
9696
"""
9797
)
9898
result = testdir.runpytest_subprocess("--ds=tpkg.settings_opt", "--dc=MySettings")
99-
result.stdout.fnmatch_lines(["*1 passed*"])
99+
result.stdout.fnmatch_lines(["* 1 passed in*"])
100100
assert result.ret == 0

tests/test_django_settings_module.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_ds_after_user_conftest(testdir, monkeypatch):
138138
testdir.makepyfile(settings_after_conftest="SECRET_KEY='secret'")
139139
# testdir.makeconftest("import sys; print(sys.path)")
140140
result = testdir.runpytest_subprocess("-v")
141-
result.stdout.fnmatch_lines(["*1 passed*"])
141+
result.stdout.fnmatch_lines(["* 1 passed in*"])
142142
assert result.ret == 0
143143

144144

@@ -226,7 +226,7 @@ def test_user_count():
226226
"""
227227
)
228228
result = testdir.runpython(p)
229-
result.stdout.fnmatch_lines(["*4 passed*"])
229+
result.stdout.fnmatch_lines(["* 4 passed in*"])
230230

231231

232232
def test_settings_in_hook(testdir, monkeypatch):
@@ -275,7 +275,7 @@ def test_settings():
275275
"""
276276
)
277277
result = testdir.runpytest_subprocess()
278-
result.stdout.fnmatch_lines(["*1 passed*"])
278+
result.stdout.fnmatch_lines(["* 1 passed in*"])
279279
assert result.ret == 0
280280

281281

tests/test_environment.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,10 @@ def test_invalid_template_variable(django_testdir):
5858
django_testdir.create_app_file(
5959
"""
6060
from django.conf.urls import url
61-
from pytest_django_test.compat import patterns
6261
6362
from tpkg.app import views
6463
65-
urlpatterns = patterns(
66-
'',
67-
url(r'invalid_template/', views.invalid_template),
68-
)
64+
urlpatterns = [url(r'invalid_template/', views.invalid_template)]
6965
""",
7066
"urls.py",
7167
)
@@ -168,14 +164,10 @@ def test_invalid_template_variable_opt_in(django_testdir):
168164
django_testdir.create_app_file(
169165
"""
170166
from django.conf.urls import url
171-
from pytest_django_test.compat import patterns
172167
173168
from tpkg.app import views
174169
175-
urlpatterns = patterns(
176-
'',
177-
url(r'invalid_template/', views.invalid_template),
178-
)
170+
urlpatterns = [url(r'invalid_template/', views.invalid_template)]
179171
""",
180172
"urls.py",
181173
)

tests/test_fixtures.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,9 @@ class MyCustomUser(AbstractUser):
518518
django_testdir.create_app_file(
519519
"""
520520
from django.conf.urls import url
521-
from pytest_django_test.compat import patterns
522521
from tpkg.app import views
523522
524-
urlpatterns = patterns(
525-
'',
526-
url(r'admin-required/', views.admin_required_view),
527-
)
523+
urlpatterns = [url(r'admin-required/', views.admin_required_view)]
528524
""",
529525
"urls.py",
530526
)
@@ -543,12 +539,12 @@ def admin_required_view(request):
543539
)
544540
django_testdir.makepyfile(
545541
"""
546-
from django.utils.encoding import force_text
542+
from django.utils.encoding import force_str
547543
from tpkg.app.models import MyCustomUser
548544
549545
def test_custom_user_model(admin_client):
550546
resp = admin_client.get('/admin-required/')
551-
assert force_text(resp.content) == 'You are an admin'
547+
assert force_str(resp.content) == 'You are an admin'
552548
"""
553549
)
554550

@@ -578,7 +574,7 @@ class Migration(migrations.Migration):
578574
('password', models.CharField(max_length=128, verbose_name='password')),
579575
('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)),
580576
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
581-
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, max_length=30, validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.', 'invalid')], help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True, verbose_name='username')),
577+
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, max_length=30, validators=[django.core.validators.RegexValidator(r'^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.', 'invalid')], help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True, verbose_name='username')),
582578
('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)),
583579
('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)),
584580
('email', models.EmailField(max_length=254, verbose_name='email address', blank=True)),
@@ -602,7 +598,7 @@ class Migration(migrations.Migration):
602598
)
603599

604600
result = django_testdir.runpytest_subprocess("-s")
605-
result.stdout.fnmatch_lines(["*1 passed*"])
601+
result.stdout.fnmatch_lines(["* 1 passed in*"])
606602
assert result.ret == 0
607603

608604

tests/test_initialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_ds():
5454
"conftest",
5555
"pytest_configure: conftest",
5656
"pytest_configure: plugin",
57-
"*1 passed*",
57+
"* 1 passed in*",
5858
]
5959
)
6060
assert result.ret == 0

tests/test_unittest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def test_noop(self):
392392

393393
result = django_testdir.runpytest_subprocess("-q", "-s")
394394
result.stdout.fnmatch_lines(
395-
["*FooBarTestCase.setUpClass*", "*test_noop*", "1 passed*"]
395+
["*FooBarTestCase.setUpClass*", "*test_noop*", "1 passed in*"]
396396
)
397397
assert result.ret == 0
398398

tests/test_urls.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ def test_urls_cache_is_cleared(testdir):
2323
testdir.makepyfile(
2424
myurls="""
2525
from django.conf.urls import url
26-
from pytest_django_test.compat import patterns
2726
2827
def fake_view(request):
2928
pass
3029
31-
urlpatterns = patterns('', url(r'first/$', fake_view, name='first'))
30+
urlpatterns = [url(r'first/$', fake_view, name='first')]
3231
"""
3332
)
3433

@@ -60,24 +59,22 @@ def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(testdir):
6059
testdir.makepyfile(
6160
myurls="""
6261
from django.conf.urls import url
63-
from pytest_django_test.compat import patterns
6462
6563
def fake_view(request):
6664
pass
6765
68-
urlpatterns = patterns('', url(r'first/$', fake_view, name='first'))
66+
urlpatterns = [url(r'first/$', fake_view, name='first')]
6967
"""
7068
)
7169

7270
testdir.makepyfile(
7371
myurls2="""
7472
from django.conf.urls import url
75-
from pytest_django_test.compat import patterns
7673
7774
def fake_view(request):
7875
pass
7976
80-
urlpatterns = patterns('', url(r'second/$', fake_view, name='second'))
77+
urlpatterns = [url(r'second/$', fake_view, name='second')]
8178
"""
8279
)
8380

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ setenv =
4545
coverage: COVERAGE_FILE={toxinidir}/.coverage
4646
coverage: PYTESTDJANGO_COVERAGE_SRC={toxinidir}/
4747

48-
passenv = PYTEST_ADDOPTS
48+
passenv = PYTEST_ADDOPTS TERM
4949
usedevelop = True
5050
commands =
5151
coverage: coverage erase

0 commit comments

Comments
 (0)