#36083: LiveServerTestCase fails in parallel test runner if
django.contrib.auth.backends has not yet been imported
-------------------------------------+-------------------------------------
Reporter: Adam Zapletal | Owner: (none)
Type: Bug | Status: new
Component: Testing framework | Version: dev
Severity: Release blocker | Resolution:
Keywords: TransactionTestCase | Triage Stage: Accepted
setupclass available_apps |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Adam Zapletal:
Old description:
> The full test suite run with multiple threads passes for me, but when I
> run the `tests.file_storage.tests` file in isolation (`./runtests.py
> file_storage.tests`), I get a failure. It passes in isolation if I run
> the tests with only one thread (`./runtests.py --parallel 1
> file_storage.tests`). This happened to others when I asked about it in
> the Django Discord server, but not everyone. For what it's worth, I'm on
> the latest macOS running Python v3.12.
>
> When a failure happens, I get the following error:
>
> `django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to
> model 'auth.User' that has not been installed`
>
> I have narrowed the issue down to the fact that this test file contains a
> class that inherits from `LiveServerTestCase`. It seems like any test
> file that mixes `LiveServerTestCase` and something more normal like
> `unittest.TestCase` will fail for me when run in isolation with more than
> one thread.
>
> Here's a minimal example if anyone is interested:
>
> {{{#!python
> from unittest import TestCase
>
> from django.test import LiveServerTestCase
>
> class TestCaseTests(TestCase):
> def test(self):
> self.assertEqual(2, 2)
>
> class LiveServerTestCaseTests(LiveServerTestCase):
> available_apps = []
>
> def test(self):
> self.client.get(self.live_server_url + '/')
> }}}
>
> I have a fix, but I'm not sure if it's a good one. Setting
> `available_apps = ['django.contrib.auth']` on the
> `LiveServerTestCase`-based class fixes it as one would expect from the
> error message, but I wonder if this is hiding something like a race
> condition. I wonder why it passes the full test suite when run with
> multiple threads, but it can't pass in isolation when run with multiple
> threads.
>
> Should I open a pull request to fix
> [
https://github.com/django/django/blob/8bee7fa45cd7bfe70b68784314e994e2d193fd70/tests/file_storage/tests.py#L1241
> the test] in the way I mentioned above, or is something deeper going on
> here? It could be that I'm misunderstanding something about how
> `LiveServerTestCase` is isolated during test runs.
New description:
The full test suite run with multiple threads passes for me, but when I
run the `tests.file_storage.tests` file in isolation (`./runtests.py
file_storage.tests`), I get a failure. It passes in isolation if I run the
tests with only one thread (`./runtests.py --parallel 1
file_storage.tests`). This happened to others when I asked about it in the
Django Discord server, but not everyone. For what it's worth, I'm on the
latest macOS running Python v3.12.
When a failure happens, I get the following error:
`django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to
model 'auth.User' that has not been installed`
I have narrowed the issue down to the fact that this test file contains a
class that inherits from `LiveServerTestCase`. It seems like any test file
that mixes `LiveServerTestCase` and something more normal like
`unittest.TestCase` will fail for me when run in isolation with more than
one thread.
Here's a minimal example if anyone is interested:
{{{#!python
from unittest import TestCase
from django.test import LiveServerTestCase
class TestCaseTests(TestCase):
def test(self):
self.assertEqual(2, 2)
class LiveServerTestCaseTests(LiveServerTestCase):
available_apps = []
def test(self):
self.assertEqual(2, 2)
}}}
I have a fix, but I'm not sure if it's a good one. Setting `available_apps
= ['django.contrib.auth']` on the `LiveServerTestCase`-based class fixes
it as one would expect from the error message, but I wonder if this is
hiding something like a race condition. I wonder why it passes the full
test suite when run with multiple threads, but it can't pass in isolation
when run with multiple threads.
Should I open a pull request to fix
[
https://github.com/django/django/blob/8bee7fa45cd7bfe70b68784314e994e2d193fd70/tests/file_storage/tests.py#L1241
the test] in the way I mentioned above, or is something deeper going on
here? It could be that I'm misunderstanding something about how
`LiveServerTestCase` is isolated during test runs.
--
--
Ticket URL: <
https://code.djangoproject.com/ticket/36083#comment:7>