Skip to content

tests: harden assertions (no warnings etc) #743

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 5 commits into from
Oct 16, 2019
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
13 changes: 2 additions & 11 deletions pytest_django_test/compat.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
try:
from urllib2 import urlopen, HTTPError # noqa
from urllib2 import urlopen, HTTPError
except ImportError:
from urllib.request import urlopen, HTTPError # noqa

# Django 1.10 removes patterns, instead it is just a list
try:
from django.conf.urls import patterns
except ImportError:

def patterns(prefix, *urls):
assert prefix == ""
return urls
from urllib.request import urlopen, HTTPError # noqa: F401
6 changes: 2 additions & 4 deletions pytest_django_test/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from django.conf.urls import url

from .app import views
from .compat import patterns

urlpatterns = patterns(
"",
urlpatterns = [
url(r"^item_count/$", views.item_count),
url(r"^admin-required/$", views.admin_required_view),
)
]
8 changes: 3 additions & 5 deletions pytest_django_test/urls_overridden.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.conf.urls import url
from django.http import HttpResponse

from .compat import patterns

urlpatterns = patterns(
"", url(r"^overridden_url/$", lambda r: HttpResponse("Overridden urlconf works!"))
)
urlpatterns = [
url(r"^overridden_url/$", lambda r: HttpResponse("Overridden urlconf works!"))
]
6 changes: 3 additions & 3 deletions tests/test_django_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_settings():
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["*1 passed*"])
result.stdout.fnmatch_lines(["* 1 passed in*"])
assert result.ret == 0


Expand Down Expand Up @@ -68,7 +68,7 @@ def test_ds():
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["*1 passed*"])
result.stdout.fnmatch_lines(["* 1 passed in*"])
assert result.ret == 0


Expand Down Expand Up @@ -96,5 +96,5 @@ def test_ds():
"""
)
result = testdir.runpytest_subprocess("--ds=tpkg.settings_opt", "--dc=MySettings")
result.stdout.fnmatch_lines(["*1 passed*"])
result.stdout.fnmatch_lines(["* 1 passed in*"])
assert result.ret == 0
6 changes: 3 additions & 3 deletions tests/test_django_settings_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_ds_after_user_conftest(testdir, monkeypatch):
testdir.makepyfile(settings_after_conftest="SECRET_KEY='secret'")
# testdir.makeconftest("import sys; print(sys.path)")
result = testdir.runpytest_subprocess("-v")
result.stdout.fnmatch_lines(["*1 passed*"])
result.stdout.fnmatch_lines(["* 1 passed in*"])
assert result.ret == 0


Expand Down Expand Up @@ -226,7 +226,7 @@ def test_user_count():
"""
)
result = testdir.runpython(p)
result.stdout.fnmatch_lines(["*4 passed*"])
result.stdout.fnmatch_lines(["* 4 passed in*"])


def test_settings_in_hook(testdir, monkeypatch):
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_settings():
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["*1 passed*"])
result.stdout.fnmatch_lines(["* 1 passed in*"])
assert result.ret == 0


Expand Down
12 changes: 2 additions & 10 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ def test_invalid_template_variable(django_testdir):
django_testdir.create_app_file(
"""
from django.conf.urls import url
from pytest_django_test.compat import patterns

from tpkg.app import views

urlpatterns = patterns(
'',
url(r'invalid_template/', views.invalid_template),
)
urlpatterns = [url(r'invalid_template/', views.invalid_template)]
""",
"urls.py",
)
Expand Down Expand Up @@ -168,14 +164,10 @@ def test_invalid_template_variable_opt_in(django_testdir):
django_testdir.create_app_file(
"""
from django.conf.urls import url
from pytest_django_test.compat import patterns

from tpkg.app import views

urlpatterns = patterns(
'',
url(r'invalid_template/', views.invalid_template),
)
urlpatterns = [url(r'invalid_template/', views.invalid_template)]
""",
"urls.py",
)
Expand Down
14 changes: 5 additions & 9 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,9 @@ class MyCustomUser(AbstractUser):
django_testdir.create_app_file(
"""
from django.conf.urls import url
from pytest_django_test.compat import patterns
from tpkg.app import views

urlpatterns = patterns(
'',
url(r'admin-required/', views.admin_required_view),
)
urlpatterns = [url(r'admin-required/', views.admin_required_view)]
""",
"urls.py",
)
Expand All @@ -543,12 +539,12 @@ def admin_required_view(request):
)
django_testdir.makepyfile(
"""
from django.utils.encoding import force_text
from django.utils.encoding import force_str
from tpkg.app.models import MyCustomUser

def test_custom_user_model(admin_client):
resp = admin_client.get('/admin-required/')
assert force_text(resp.content) == 'You are an admin'
assert force_str(resp.content) == 'You are an admin'
"""
)

Expand Down Expand Up @@ -578,7 +574,7 @@ class Migration(migrations.Migration):
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('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')),
('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')),
('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)),
('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)),
('email', models.EmailField(max_length=254, verbose_name='email address', blank=True)),
Expand All @@ -602,7 +598,7 @@ class Migration(migrations.Migration):
)

result = django_testdir.runpytest_subprocess("-s")
result.stdout.fnmatch_lines(["*1 passed*"])
result.stdout.fnmatch_lines(["* 1 passed in*"])
assert result.ret == 0


Expand Down
2 changes: 1 addition & 1 deletion tests/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_ds():
"conftest",
"pytest_configure: conftest",
"pytest_configure: plugin",
"*1 passed*",
"* 1 passed in*",
]
)
assert result.ret == 0
2 changes: 1 addition & 1 deletion tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def test_noop(self):

result = django_testdir.runpytest_subprocess("-q", "-s")
result.stdout.fnmatch_lines(
["*FooBarTestCase.setUpClass*", "*test_noop*", "1 passed*"]
["*FooBarTestCase.setUpClass*", "*test_noop*", "1 passed in*"]
)
assert result.ret == 0

Expand Down
9 changes: 3 additions & 6 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ def test_urls_cache_is_cleared(testdir):
testdir.makepyfile(
myurls="""
from django.conf.urls import url
from pytest_django_test.compat import patterns

def fake_view(request):
pass

urlpatterns = patterns('', url(r'first/$', fake_view, name='first'))
urlpatterns = [url(r'first/$', fake_view, name='first')]
"""
)

Expand Down Expand Up @@ -60,24 +59,22 @@ def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(testdir):
testdir.makepyfile(
myurls="""
from django.conf.urls import url
from pytest_django_test.compat import patterns

def fake_view(request):
pass

urlpatterns = patterns('', url(r'first/$', fake_view, name='first'))
urlpatterns = [url(r'first/$', fake_view, name='first')]
"""
)

testdir.makepyfile(
myurls2="""
from django.conf.urls import url
from pytest_django_test.compat import patterns

def fake_view(request):
pass

urlpatterns = patterns('', url(r'second/$', fake_view, name='second'))
urlpatterns = [url(r'second/$', fake_view, name='second')]
"""
)

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ setenv =
coverage: COVERAGE_FILE={toxinidir}/.coverage
coverage: PYTESTDJANGO_COVERAGE_SRC={toxinidir}/

passenv = PYTEST_ADDOPTS
passenv = PYTEST_ADDOPTS TERM
usedevelop = True
commands =
coverage: coverage erase
Expand Down