Django test parallel AppRegistryNotReady

2024/9/21 5:44:21

I am trying to understand how to run django tests in parallel with in memory sqlite3.

I have django app with that structure:

gbookorder...tests__init__.pytest_a1.pytest_b1.pyutils.py

test_a1.py and test_b1.py contains same code:

import time
from order import models
from .utils import BackendTestCaseclass ATestCase(BackendTestCase):def test_a(self):time.sleep(1)a = models.City.objects.count()self.assertEqual(a, a)class BTestCase(BackendTestCase):def test_b(self):time.sleep(1)a = models.City.objects.count()self.assertEqual(a, a)

utils.py is:

from django.test import TestCase, Client
from order import models
from django.conf import settingsfrom order.utils import to_hashclass BackendTestCase(TestCase):fixtures = ['City.json', 'Agency.json']def setUp(self):self.client = Client()self.lang_codes = (i[0] for i in settings.LANGUAGES)...

settings_test.py:

from .settings import *DEBUG = FalseTEMPLATE_DEBUG = FalseSTATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'PASSWORD_HASHERS = ['django.contrib.auth.hashers.MD5PasswordHasher',]  # fasterDATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3',
}

When I run test in single process, all goes well (about 4 sec):

python.exe manage.py test order --settings=gbook.settings_test

Then I trying to run tests in parallel:

python.exe manage.py test order --settings=gbook.settings_test --parallel=2

I get this trace (console):

Creating test database for alias 'default'...Cloning test database for alias 'default'...
Cloning test database for alias 'default'...
System check identified no issues (0 silenced).
Process SpawnPoolWorker-2:
Process SpawnPoolWorker-1:
Traceback (most recent call last):
Traceback (most recent call last):File "C:\python\Python36-32\lib\multiprocessing\process.py", line 258, in _bootstrapself.run()File "C:\python\Python36-32\lib\multiprocessing\process.py", line 258, in _bootstrapself.run()File "C:\python\Python36-32\lib\multiprocessing\process.py", line 93, in runself._target(*self._args, **self._kwargs)File "C:\python\Python36-32\lib\multiprocessing\process.py", line 93, in runself._target(*self._args, **self._kwargs)File "C:\python\Python36-32\lib\multiprocessing\pool.py", line 108, in workertask = get()File "C:\python\Python36-32\lib\multiprocessing\pool.py", line 108, in workertask = get()File "C:\python\Python36-32\lib\multiprocessing\queues.py", line 337, in getreturn _ForkingPickler.loads(res)File "C:\python\Python36-32\lib\multiprocessing\queues.py", line 337, in getreturn _ForkingPickler.loads(res)File "C:\kvk\develop\Python\gbook\order\tests\test_a1.py", line 2, in <module>from order import modelsFile "C:\kvk\develop\Python\gbook\order\tests\test_a1.py", line 2, in <module>from order import modelsFile "C:\kvk\develop\Python\gbook\order\models.py", line 79, in <module>class Agency(models.Model):File "C:\kvk\develop\Python\gbook\order\models.py", line 79, in <module>class Agency(models.Model):File "C:\python\venv\gbook\lib\site-packages\django\db\models\base.py", line 110, in __new__app_config = apps.get_containing_app_config(module)File "C:\python\venv\gbook\lib\site-packages\django\db\models\base.py", line 110, in __new__app_config = apps.get_containing_app_config(module)File "C:\python\venv\gbook\lib\site-packages\django\apps\registry.py", line 247, in get_containing_app_configself.check_apps_ready()File "C:\python\venv\gbook\lib\site-packages\django\apps\registry.py", line 247, in get_containing_app_configself.check_apps_ready()File "C:\python\venv\gbook\lib\site-packages\django\apps\registry.py", line 125, in check_apps_readyraise AppRegistryNotReady("Apps aren't loaded yet.")File "C:\python\venv\gbook\lib\site-packages\django\apps\registry.py", line 125, in check_apps_readyraise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

From Pycharm trace is different:

...Traceback (most recent call last):File "C:\python\Python36-32\lib\unittest\suite.py", line 163, in _handleClassSetUpsetUpClass()File "C:\python\venv\gbook\lib\site-packages\django\test\testcases.py", line 1036, in setUpClass'database': db_name,File "C:\python\venv\gbook\lib\site-packages\django\core\management\__init__.py", line 131, in call_commandreturn command.execute(*args, **defaults)File "C:\python\venv\gbook\lib\site-packages\django\core\management\base.py", line 330, in executeoutput = self.handle(*args, **options)File "C:\python\venv\gbook\lib\site-packages\modeltranslation\management\commands\loaddata.py", line 61, in handlereturn super(Command, self).handle(*fixture_labels, **options)File "C:\python\venv\gbook\lib\site-packages\django\core\management\commands\loaddata.py", line 69, in handleself.loaddata(fixture_labels)File "C:\python\venv\gbook\lib\site-packages\django\core\management\commands\loaddata.py", line 109, in loaddataself.load_label(fixture_label)File "C:\python\venv\gbook\lib\site-packages\django\core\management\commands\loaddata.py", line 175, in load_labelobj.save(using=self.using)File "C:\python\venv\gbook\lib\site-packages\django\core\serializers\base.py", line 205, in savemodels.Model.save_base(self.object, using=using, raw=True, **kwargs)File "C:\python\venv\gbook\lib\site-packages\django\db\models\base.py", line 838, in save_baseupdated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)File "C:\python\venv\gbook\lib\site-packages\django\db\models\base.py", line 905, in _save_tableforced_update)File "C:\python\venv\gbook\lib\site-packages\django\db\models\base.py", line 955, in _do_updatereturn filtered._update(values) > 0File "C:\python\venv\gbook\lib\site-packages\django\db\models\query.py", line 664, in _updatereturn query.get_compiler(self.db).execute_sql(CURSOR)File "C:\python\venv\gbook\lib\site-packages\django\db\models\sql\compiler.py", line 1204, in execute_sqlcursor = super(SQLUpdateCompiler, self).execute_sql(result_type)File "C:\python\venv\gbook\lib\site-packages\django\db\models\sql\compiler.py", line 899, in execute_sqlraise original_exceptionFile "C:\python\venv\gbook\lib\site-packages\django\db\models\sql\compiler.py", line 889, in execute_sqlcursor.execute(sql, params)File "C:\python\venv\gbook\lib\site-packages\django\db\backends\utils.py", line 64, in executereturn self.cursor.execute(sql, params)File "C:\python\venv\gbook\lib\site-packages\django\db\utils.py", line 94, in __exit__six.reraise(dj_exc_type, dj_exc_value, traceback)File "C:\python\venv\gbook\lib\site-packages\django\utils\six.py", line 685, in reraiseraise value.with_traceback(tb)File "C:\python\venv\gbook\lib\site-packages\django\db\backends\utils.py", line 64, in executereturn self.cursor.execute(sql, params)File "C:\python\venv\gbook\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in executereturn Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: Problem installing fixture 'C:\kvk\develop\Python\gbook\order\fixtures\AirportInfo.json': Could not load order.AirportInfo(pk=2411): no such table: GB_AIRPORT_INFO

It seem like migrations not works for parallel, but why?

Docs says: "--parallel" Runs tests in separate parallel processes. Each process gets its own database. And I do not need to change my code for use it.

Please, help me to understand, what am i doing wrong.

multiprocessing.cpu_count() = 4

Django version 1.11.10

Python 3.6.5

Answer

Same issue as above with MacOS and Python 3.8+. You have to explicitly set import multiprocessing; multiprocessing.set_start_method('fork') at the top of your settings.py file. But be sure to understand the side effects before you do!

https://en.xdnf.cn/q/72262.html

Related Q&A

ImportError: PyCapsule_Import could not import module pyexpat

I am using Jenkins to build a python (Flask) solution to deploy to Google App Engine. As part of the build process I run a few integration tests. One of them is failing with the following error. ERROR:…

Python - Get max value in a list of dict

I have a dataset with this structure :In[17]: allIndices Out[17]: [{0: 0, 1: 1.4589, 4: 2.4879}, {0: 1.4589, 1: 0, 2: 2.1547}, {1: 2.1547, 2: 0, 3: 4.2114}, {2: 4.2114, 3: 0}, {0: 2.4879, 4: 0}]Id lik…

Rescaling axis in Matplotlib imshow under unique function call

I have written a function module that takes the argument of two variables. To plot, I hadx, y = pylab.ogrid[0.3:0.9:0.1, 0.:3.5:.5] z = np.zeros(shape=(np.shape(x)[0], np.shape(y)[1]))for i in range(le…

f2py array valued functions

Do recent versions of f2py support wrapping array-valued fortran functions? In some ancient documentation this wasnt supported. How about it now?Lets for example save the following function as func.f…

Unique strings in a pandas dataframe

I have following sample DataFrame d consisting of two columns col1 and col2. I would like to find the list of unique names for the whole DataFrame d. d = {col1:[Pat, Joseph, Tony, Hoffman, Miriam, Good…

finding index of multiple items in a list

I have a list myList = ["what is your name", "Hi, how are you","What about you", "How about a coffee", "How are you"]Now I want to search index of all …

Debugging asyncio code in PyCharm causes absolutely crazy unrepeatable errors

In my project that based on asyncio and asyncio tcp connections that debugs with PyCharm debugger I got very and very very absurd errors.If I put breakpoint on code after running, the breakpoint never …

how to generate pie chart using dict_values in Python 3.4?

I wanted the frequency of numbers in a list which I got using Counter library. Also, I got the keys and values using keys = Counter(list).keys() and values = Counter(list).values() respectively, where …

How can I make start_url in scrapy to consume from a message queue?

I am building a scrapy project in which I have multiple spiders( A spider for each domain). Now, the urls to be scraped come dynamically from a user given query. so basically I do not need to do broad…

Pip install results in this error cl.exe failed with exit code 2

Ive read all of the other questions on this error and frustratingly enough, none give a solution that works. If I run pip install sentencepiece in the cmd line, it gives me the following output.src/sen…