summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/admin_scripts/tests.py9
-rw-r--r--tests/backends/tests.py5
-rw-r--r--tests/bash_completion/tests.py5
-rw-r--r--tests/handlers/views.py6
-rw-r--r--tests/mail/tests.py9
-rw-r--r--tests/postgres_tests/test_aggregates.py5
-rw-r--r--tests/postgres_tests/test_array.py5
-rw-r--r--tests/postgres_tests/test_hstore.py5
-rw-r--r--tests/postgres_tests/test_json.py5
-rw-r--r--tests/postgres_tests/test_ranges.py5
-rwxr-xr-xtests/runtests.py9
-rw-r--r--tests/staticfiles_tests/storage.py5
-rw-r--r--tests/transaction_hooks/tests.py34
13 files changed, 39 insertions, 68 deletions
diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
index e2d2a3bca1..d90456ac13 100644
--- a/tests/admin_scripts/tests.py
+++ b/tests/admin_scripts/tests.py
@@ -12,6 +12,7 @@ import subprocess
import sys
import tempfile
import unittest
+from contextlib import suppress
from io import StringIO
from unittest import mock
@@ -95,12 +96,10 @@ class AdminScriptTestCase(unittest.TestCase):
# Also try to remove the compiled file; if it exists, it could
# mess up later tests that depend upon the .py file not existing
- try:
+ with suppress(OSError):
if sys.platform.startswith('java'):
# Jython produces module$py.class files
os.remove(re.sub(r'\.py$', '$py.class', full_name))
- except OSError:
- pass
# Also remove a __pycache__ directory, if it exists
cache_name = os.path.join(self.test_dir, '__pycache__')
if os.path.isdir(cache_name):
@@ -166,10 +165,8 @@ class AdminScriptTestCase(unittest.TestCase):
def run_manage(self, args, settings_file=None):
def safe_remove(path):
- try:
+ with suppress(OSError):
os.remove(path)
- except OSError:
- pass
conf_dir = os.path.dirname(conf.__file__)
template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py-tpl')
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index 6d38625a98..30c1cbf86d 100644
--- a/tests/backends/tests.py
+++ b/tests/backends/tests.py
@@ -3,6 +3,7 @@ import datetime
import threading
import unittest
import warnings
+from contextlib import suppress
from django.core.management.color import no_style
from django.db import (
@@ -389,10 +390,8 @@ class BackendTestCase(TransactionTestCase):
finally:
# Clean up the mess created by connection._close(). Since the
# connection is already closed, this crashes on some backends.
- try:
+ with suppress(Exception):
connection.close()
- except Exception:
- pass
@override_settings(DEBUG=True)
def test_queries(self):
diff --git a/tests/bash_completion/tests.py b/tests/bash_completion/tests.py
index 1d35e1f28e..ba2a5ea773 100644
--- a/tests/bash_completion/tests.py
+++ b/tests/bash_completion/tests.py
@@ -4,6 +4,7 @@ A series of tests to establish that the command-line bash completion works.
import os
import sys
import unittest
+from contextlib import suppress
from django.apps import apps
from django.core.management import ManagementUtility
@@ -50,10 +51,8 @@ class BashCompletionTests(unittest.TestCase):
def _run_autocomplete(self):
util = ManagementUtility(argv=sys.argv)
with captured_stdout() as stdout:
- try:
+ with suppress(SystemExit):
util.autocomplete()
- except SystemExit:
- pass
return stdout.getvalue().strip().split('\n')
def test_django_admin_py(self):
diff --git a/tests/handlers/views.py b/tests/handlers/views.py
index 22b94de3b9..3c1fa5b802 100644
--- a/tests/handlers/views.py
+++ b/tests/handlers/views.py
@@ -1,12 +1,12 @@
+from contextlib import suppress
+
from django.core.exceptions import SuspiciousOperation
from django.db import connection, transaction
from django.http import HttpResponse, StreamingHttpResponse
from django.views.decorators.csrf import csrf_exempt
-try:
+with suppress(ImportError): # Python < 3.5
from http import HTTPStatus
-except ImportError: # Python < 3.5
- pass
def regular(request):
diff --git a/tests/mail/tests.py b/tests/mail/tests.py
index 50922e21d7..e1a7feb1f9 100644
--- a/tests/mail/tests.py
+++ b/tests/mail/tests.py
@@ -8,6 +8,7 @@ import socket
import sys
import tempfile
import threading
+from contextlib import suppress
from email import message_from_binary_file, message_from_bytes
from email.header import Header
from email.mime.text import MIMEText
@@ -1123,12 +1124,10 @@ class ConsoleBackendTests(BaseEmailBackendTests, SimpleTestCase):
class FakeSMTPChannel(smtpd.SMTPChannel):
def collect_incoming_data(self, data):
- try:
+ # Ignore decode error in SSL/TLS connection tests as the test only
+ # cares whether the connection attempt was made.
+ with suppress(UnicodeDecodeError):
smtpd.SMTPChannel.collect_incoming_data(self, data)
- except UnicodeDecodeError:
- # ignore decode error in SSL/TLS connection tests as we only care
- # whether the connection attempt was made
- pass
def smtp_AUTH(self, arg):
if arg == 'CRAM-MD5':
diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py
index 056d08441b..1fe8a1bf03 100644
--- a/tests/postgres_tests/test_aggregates.py
+++ b/tests/postgres_tests/test_aggregates.py
@@ -1,4 +1,5 @@
import json
+from contextlib import suppress
from django.db.models.expressions import F, Value
from django.test.testcases import skipUnlessDBFeature
@@ -7,14 +8,12 @@ from django.test.utils import Approximate
from . import PostgreSQLTestCase
from .models import AggregateTestModel, StatTestModel
-try:
+with suppress(ImportError): # psycopg2 is not installed
from django.contrib.postgres.aggregates import (
ArrayAgg, BitAnd, BitOr, BoolAnd, BoolOr, Corr, CovarPop, JSONBAgg,
RegrAvgX, RegrAvgY, RegrCount, RegrIntercept, RegrR2, RegrSlope,
RegrSXX, RegrSXY, RegrSYY, StatAggregate, StringAgg,
)
-except ImportError:
- pass # psycopg2 is not installed
class TestGeneralAggregate(PostgreSQLTestCase):
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py
index e2e4ccdeb2..7378b5c12d 100644
--- a/tests/postgres_tests/test_array.py
+++ b/tests/postgres_tests/test_array.py
@@ -2,6 +2,7 @@ import decimal
import json
import unittest
import uuid
+from contextlib import suppress
from django import forms
from django.core import exceptions, serializers, validators
@@ -19,13 +20,11 @@ from .models import (
PostgreSQLModel, Tag,
)
-try:
+with suppress(ImportError):
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.forms import (
SimpleArrayField, SplitArrayField, SplitArrayWidget,
)
-except ImportError:
- pass
class TestSaveLoad(PostgreSQLTestCase):
diff --git a/tests/postgres_tests/test_hstore.py b/tests/postgres_tests/test_hstore.py
index 069e570f51..55b179ba5e 100644
--- a/tests/postgres_tests/test_hstore.py
+++ b/tests/postgres_tests/test_hstore.py
@@ -1,4 +1,5 @@
import json
+from contextlib import suppress
from django.core import exceptions, serializers
from django.forms import Form
@@ -7,12 +8,10 @@ from django.test.utils import modify_settings
from . import PostgreSQLTestCase
from .models import HStoreModel
-try:
+with suppress(ImportError):
from django.contrib.postgres import forms
from django.contrib.postgres.fields import HStoreField
from django.contrib.postgres.validators import KeysValidator
-except ImportError:
- pass
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'})
diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py
index 2506fc36d6..20650ae95b 100644
--- a/tests/postgres_tests/test_json.py
+++ b/tests/postgres_tests/test_json.py
@@ -1,5 +1,6 @@
import datetime
import uuid
+from contextlib import suppress
from decimal import Decimal
from django.core import exceptions, serializers
@@ -11,11 +12,9 @@ from django.utils.html import escape
from . import PostgreSQLTestCase
from .models import JSONModel
-try:
+with suppress(ImportError):
from django.contrib.postgres import forms
from django.contrib.postgres.fields import JSONField
-except ImportError:
- pass
@skipUnlessDBFeature('has_jsonb_datatype')
diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py
index d87ad36438..da72240bf4 100644
--- a/tests/postgres_tests/test_ranges.py
+++ b/tests/postgres_tests/test_ranges.py
@@ -1,5 +1,6 @@
import datetime
import json
+from contextlib import suppress
from django import forms
from django.core import exceptions, serializers
@@ -10,14 +11,12 @@ from django.utils import timezone
from . import PostgreSQLTestCase
from .models import RangeLookupsModel, RangesModel
-try:
+with suppress(ImportError):
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
from django.contrib.postgres import fields as pg_fields, forms as pg_forms
from django.contrib.postgres.validators import (
RangeMaxValueValidator, RangeMinValueValidator,
)
-except ImportError:
- pass
class TestSaveLoad(PostgreSQLTestCase):
diff --git a/tests/runtests.py b/tests/runtests.py
index 7f4f1670c5..08e53f4cb1 100755
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -8,6 +8,7 @@ import subprocess
import sys
import tempfile
import warnings
+from contextlib import suppress
import django
from django.apps import apps
@@ -315,10 +316,8 @@ def bisect_tests(bisection_label, options, test_labels, parallel):
# Make sure the bisection point isn't in the test list
# Also remove tests that need to be run in specific combinations
for label in [bisection_label, 'model_inheritance_same_model_name']:
- try:
+ with suppress(ValueError):
test_labels.remove(label)
- except ValueError:
- pass
subprocess_args = get_subprocess_args(options)
@@ -366,10 +365,8 @@ def paired_tests(paired_test, options, test_labels, parallel):
# Make sure the constant member of the pair isn't in the test list
# Also remove tests that need to be run in specific combinations
for label in [paired_test, 'model_inheritance_same_model_name']:
- try:
+ with suppress(ValueError):
test_labels.remove(label)
- except ValueError:
- pass
subprocess_args = get_subprocess_args(options)
diff --git a/tests/staticfiles_tests/storage.py b/tests/staticfiles_tests/storage.py
index 7a1f72c130..0dcfd77a70 100644
--- a/tests/staticfiles_tests/storage.py
+++ b/tests/staticfiles_tests/storage.py
@@ -1,4 +1,5 @@
import os
+from contextlib import suppress
from datetime import datetime, timedelta
from django.conf import settings
@@ -48,10 +49,8 @@ class PathNotImplementedStorage(storage.Storage):
def delete(self, name):
name = self._path(name)
- try:
+ with suppress(FileNotFoundError):
os.remove(name)
- except FileNotFoundError:
- pass
def path(self, name):
raise NotImplementedError
diff --git a/tests/transaction_hooks/tests.py b/tests/transaction_hooks/tests.py
index 000de4104c..049211139d 100644
--- a/tests/transaction_hooks/tests.py
+++ b/tests/transaction_hooks/tests.py
@@ -1,3 +1,5 @@
+from contextlib import suppress
+
from django.db import connection, transaction
from django.test import TransactionTestCase, skipUnlessDBFeature
@@ -48,12 +50,10 @@ class TestConnectionOnCommit(TransactionTestCase):
self.assertDone([1])
def test_does_not_execute_if_transaction_rolled_back(self):
- try:
+ with suppress(ForcedError):
with transaction.atomic():
self.do(1)
raise ForcedError()
- except ForcedError:
- pass
self.assertDone([])
@@ -71,12 +71,10 @@ class TestConnectionOnCommit(TransactionTestCase):
with transaction.atomic():
self.do(1)
# one failed savepoint
- try:
+ with suppress(ForcedError):
with transaction.atomic():
self.do(2)
raise ForcedError()
- except ForcedError:
- pass
# another successful savepoint
with transaction.atomic():
self.do(3)
@@ -86,25 +84,21 @@ class TestConnectionOnCommit(TransactionTestCase):
def test_no_hooks_run_from_failed_transaction(self):
"""If outer transaction fails, no hooks from within it run."""
- try:
+ with suppress(ForcedError):
with transaction.atomic():
with transaction.atomic():
self.do(1)
raise ForcedError()
- except ForcedError:
- pass
self.assertDone([])
def test_inner_savepoint_rolled_back_with_outer(self):
with transaction.atomic():
- try:
+ with suppress(ForcedError):
with transaction.atomic():
with transaction.atomic():
self.do(1)
raise ForcedError()
- except ForcedError:
- pass
self.do(2)
self.assertDone([2])
@@ -113,11 +107,9 @@ class TestConnectionOnCommit(TransactionTestCase):
with transaction.atomic():
with transaction.atomic():
self.do(1)
- try:
+ with suppress(ForcedError):
with transaction.atomic(savepoint=False):
raise ForcedError()
- except ForcedError:
- pass
self.assertDone([])
@@ -125,11 +117,9 @@ class TestConnectionOnCommit(TransactionTestCase):
with transaction.atomic():
with transaction.atomic():
self.do(1)
- try:
+ with suppress(ForcedError):
with transaction.atomic():
raise ForcedError()
- except ForcedError:
- pass
self.assertDone([1])
@@ -151,12 +141,10 @@ class TestConnectionOnCommit(TransactionTestCase):
self.assertDone([1, 2]) # not [1, 1, 2]
def test_hooks_cleared_after_rollback(self):
- try:
+ with suppress(ForcedError):
with transaction.atomic():
self.do(1)
raise ForcedError()
- except ForcedError:
- pass
with transaction.atomic():
self.do(2)
@@ -177,11 +165,9 @@ class TestConnectionOnCommit(TransactionTestCase):
self.assertDone([2])
def test_error_in_hook_doesnt_prevent_clearing_hooks(self):
- try:
+ with suppress(ForcedError):
with transaction.atomic():
transaction.on_commit(lambda: self.notify('error'))
- except ForcedError:
- pass
with transaction.atomic():
self.do(1)