Commit c9ae09ad authored by Tim Graham's avatar Tim Graham Committed by GitHub
Browse files

Replaced use of TestCase.fail() with assertRaises().

Also removed try/except/fail antipattern that hides exceptions.
parent c1b6f554
Loading
Loading
Loading
Loading
+2 −10
Original line number Diff line number Diff line
@@ -1243,10 +1243,7 @@ class CustomModelAdminTest(AdminViewBasicTestCase):
        self.assertContains(response, 'Hello from a custom logout template')

    def test_custom_admin_site_index_view_and_template(self):
        try:
        response = self.client.get(reverse('admin2:index'))
        except TypeError:
            self.fail('AdminSite.index_template should accept a list of template paths')
        self.assertIsInstance(response, TemplateResponse)
        self.assertTemplateUsed(response, 'custom_admin/index.html')
        self.assertContains(response, 'Hello from a custom index template *bar*')
@@ -1283,12 +1280,7 @@ class CustomModelAdminTest(AdminViewBasicTestCase):
    def test_pwd_change_custom_template(self):
        self.client.force_login(self.superuser)
        su = User.objects.get(username='super')
        try:
            response = self.client.get(
                reverse('admin4:auth_user_password_change', args=(su.pk,))
            )
        except TypeError:
            self.fail('ModelAdmin.change_user_password_template should accept a list of template paths')
        response = self.client.get(reverse('admin4:auth_user_password_change', args=(su.pk,)))
        self.assertEqual(response.status_code, 200)


+2 −9
Original line number Diff line number Diff line
@@ -224,10 +224,7 @@ class SilencingCheckTests(SimpleTestCase):
    def test_silenced_error(self):
        out = StringIO()
        err = StringIO()
        try:
        call_command('check', stdout=out, stderr=err)
        except CommandError:
            self.fail("The mycheck.E001 check should be silenced.")
        self.assertEqual(out.getvalue(), 'System check identified no issues (1 silenced).\n')
        self.assertEqual(err.getvalue(), '')

@@ -236,11 +233,7 @@ class SilencingCheckTests(SimpleTestCase):
    def test_silenced_warning(self):
        out = StringIO()
        err = StringIO()
        try:
        call_command('check', stdout=out, stderr=err)
        except CommandError:
            self.fail("The mycheck.E001 check should be silenced.")

        self.assertEqual(out.getvalue(), 'System check identified no issues (1 silenced).\n')
        self.assertEqual(err.getvalue(), '')

+4 −5
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import hashlib
import json
import os
import shutil
import sys
import tempfile as sys_tempfile
import unittest
from io import BytesIO
@@ -564,16 +565,14 @@ class DirectoryCreationTests(SimpleTestCase):
    def setUp(self):
        self.obj = FileModel()

    @unittest.skipIf(sys.platform == 'win32', "Python on Windows doesn't have working os.chmod().")
    def test_readonly_root(self):
        """Permission errors are not swallowed"""
        os.chmod(MEDIA_ROOT, 0o500)
        self.addCleanup(os.chmod, MEDIA_ROOT, 0o700)
        try:
        with self.assertRaises(OSError) as cm:
            self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'), save=False)
        except OSError as err:
            self.assertEqual(err.errno, errno.EACCES)
        except Exception:
            self.fail("OSError [Errno %s] not raised." % errno.EACCES)
        self.assertEqual(cm.exception.errno, errno.EACCES)

    def test_not_a_directory(self):
        """The correct IOError is raised when the upload directory name exists but isn't a directory"""
+6 −21
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@ import warnings
import django
from django.core import management, serializers
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import CommandError
from django.core.serializers.base import DeserializationError
from django.db import IntegrityError, transaction
from django.db.models import signals
@@ -838,15 +837,9 @@ class M2MNaturalKeyFixtureTests(TestCase):
        A, B, C, AtoB, BtoC, CtoA = (M2MComplexCircular1A, M2MComplexCircular1B,
                                     M2MComplexCircular1C, M2MCircular1ThroughAB,
                                     M2MCircular1ThroughBC, M2MCircular1ThroughCA)
        try:
        sorted_deps = serializers.sort_dependencies(
            [('fixtures_regress', [A, B, C, AtoB, BtoC, CtoA])]
        )
        except CommandError:
            self.fail("Serialization dependency solving algorithm isn't "
                      "capable of handling circular M2M setups with "
                      "intermediate models.")

        # The dependency sorting should not result in an error, and the
        # through model should have dependencies to the other models and as
        # such come last in the list.
@@ -858,17 +851,9 @@ class M2MNaturalKeyFixtureTests(TestCase):
        Circular M2M relations with explicit through models should be serializable
        This test tests the circularity with explicit natural_key.dependencies
        """
        try:
        sorted_deps = serializers.sort_dependencies([
                ('fixtures_regress', [
                    M2MComplexCircular2A,
                    M2MComplexCircular2B,
                    M2MCircular2ThroughAB])
            ('fixtures_regress', [M2MComplexCircular2A, M2MComplexCircular2B, M2MCircular2ThroughAB])
        ])
        except CommandError:
            self.fail("Serialization dependency solving algorithm isn't "
                      "capable of handling circular M2M setups with "
                      "intermediate models plus natural key dependency hints.")
        self.assertEqual(sorted_deps[:2], [M2MComplexCircular2A, M2MComplexCircular2B])
        self.assertEqual(sorted_deps[2:], [M2MCircular2ThroughAB])

+2 −4
Original line number Diff line number Diff line
@@ -16,11 +16,9 @@ from django.utils.safestring import mark_safe

class AssertFormErrorsMixin(object):
    def assertFormErrors(self, expected, the_callable, *args, **kwargs):
        try:
        with self.assertRaises(ValidationError) as cm:
            the_callable(*args, **kwargs)
            self.fail("Testing the 'clean' method on %s failed to raise a ValidationError.")
        except ValidationError as e:
            self.assertEqual(e.messages, expected)
        self.assertEqual(cm.exception.messages, expected)


class FormsErrorMessagesTestCase(SimpleTestCase, AssertFormErrorsMixin):
Loading