Repository: django/django
File: django/db/backends/base/creation.py (Python)

Code before:
        """
        # Remove the test database to clean up after
        # ourselves. Connect to the previous database (not the test database)
        # to do so, because it's not allowed to delete a database while being
        # connected to it.
        with self._nodb_cursor() as cursor:
            cursor.execute(
                "DROP DATABASE %s" % self.connection.ops.quote_name(test_database_name)
            )

    def mark_expected_failures_and_skips(self):
        """
        Mark tests in Django's test suite which are expected failures on this
        database and test which should be skipped on this database.
        """
        # Only load unittest if we're actually testing.
        from unittest import expectedFailure, skip

        def get_test(module_or_class, class_or_method, test_name):
            try:
                test_frame = import_string(module_or_class)
            except ImportError:
                # When given a submodule, import_string() can raise ImportError
                # if its parent module has not been already imported during
                # test discovery. Since the parallel test runner spawns workers
                # with a fresh import cache and then applies these skips, do
                # not expect import_string() to succeed for submodules.
                # test_name is likely a class, so import that instead.
                test_to_skip = import_string(test_name)
                test_frame = sys.modules.get(test_to_skip.__module__)
            else:
                test_to_skip = getattr(test_frame, class_or_method)
            return test_frame, test_to_skip

        for test_name in self.connection.features.django_test_expected_failures:
            module_or_class_name, _, class_or_method_name = test_name.rpartition(".")
            test_app = test_name.split(".")[0]
            # Importing a test app that isn't installed raises RuntimeError.
            if test_app in settings.INSTALLED_APPS:
                test_frame, test_to_skip = get_test(
                    module_or_class_name, class_or_method_name, test_name
                )
                setattr(test_frame, class_or_method_name, expectedFailure(test_to_skip))
        for reason, tests in self.connection.features.django_test_skips.items():
            for test_name in tests:
                module_or_class_name, _, class_or_method_name = test_name.rpartition(
                    "."
                )
                test_app = test_name.split(".")[0]
                # Importing a test app that isn't installed raises
                # RuntimeError.

Review comment (bug):
"do not expect" seems like a verbiage change from the previous sentence. Here's my suggestion:

import_string() can raise ImportError if a submodule's parent module hasn't already been imported during test discovery. This can happen in at least two cases:
1. When running a subset of tests in a module, tests in that module's other submodules won't be imported by the test runner.
2. When the parallel test runner spawns workers with an empty import cache.

Please correct any details that I got wrong.


Code after:
        """
        # Remove the test database to clean up after
        # ourselves. Connect to the previous database (not the test database)
        # to do so, because it's not allowed to delete a database while being
        # connected to it.
        with self._nodb_cursor() as cursor:
            cursor.execute(
                "DROP DATABASE %s" % self.connection.ops.quote_name(test_database_name)
            )

    def mark_expected_failures_and_skips(self):
        """
        Mark tests in Django's test suite which are expected failures on this
        database and test which should be skipped on this database.
        """
        for test_name in self.connection.features.django_test_expected_failures:
            self._mark_test(test_name)
        for reason, tests in self.connection.features.django_test_skips.items():
            for test_name in tests:
                self._mark_test(test_name, reason)

    def _mark_test(self, test_name, skip_reason=None):
        # Only load unittest during testing.
        from unittest import expectedFailure, skip

        module_or_class_name, _, name_to_mark = test_name.rpartition(".")
        test_app = test_name.split(".")[0]
        # Importing a test app that isn't installed raises RuntimeError.
        if test_app in settings.INSTALLED_APPS:
            try:
                test_frame = import_string(module_or_class_name)
            except ImportError:
                # import_string() can raise ImportError if a submodule's parent
                # module hasn't already been imported during test discovery.
                # This can happen in at least two cases:
                # 1. When running a subset of tests in a module, the test
                #    runner won't import tests in that module's other
                #    submodules.
                # 2. When the parallel test runner spawns workers with an empty
                #    import cache.
                test_to_mark = import_string(test_name)
                test_frame = sys.modules.get(test_to_mark.__module__)
            else:
                test_to_mark = getattr(test_frame, name_to_mark)
            if skip_reason:
                setattr(test_frame, name_to_mark, skip(skip_reason)(test_to_mark))
            else:
                setattr(test_frame, name_to_mark, expectedFailure(test_to_mark))

    def sql_table_creation_suffix(self):
        """