Running the test suite with tox with MySQL as backend

I am trying to tun the test suite with tox following this guide.
The command that I ran was tox -e py38-mysql -- --settings=test_mysql. I have a test_mysql.py inside the /tests folder which looks like:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_default',
'USER': 'root',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306'
},
'other': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_other',
'USER': 'root',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306'
}
}

SECRET_KEY = "django_tests_secret_key"

PASSWORD_HASHERS = [
'django.contrib.auth.hashers.MD5PasswordHasher',
]

I got the following error:

py38-mysql develop-inst-noop: /Users/sanskarjaiswal/Projects/django
py38-mysql installed: argon2-cffi==19.2.0,asgiref==3.2.7,bcrypt==3.1.7,certifi==2020.4.5.1,cffi==1.14.0,chardet==3.0.4,-e git+https://github.com/aryan9600/django.git@a49c2b6bf098eb48c07641f60dba9be78c6cc92f#egg=Django,docutils==0.16,geoip2==3.0.0,idna==2.9,Jinja2==2.11.1,MarkupSafe==1.1.1,maxminddb==1.5.2,mysqlclient==1.4.6,numpy==1.18.2,Pillow==7.1.1,pycparser==2.20,pylibmc==1.6.1,python-memcached==1.59,pytz==2019.3,pywatchman==1.4.1,PyYAML==5.3.1,requests==2.23.0,selenium==3.141.0,six==1.14.0,sqlparse==0.3.1,tblib==1.6.0,urllib3==1.25.8
py38-mysql run-test-pre: PYTHONHASHSEED='3727337229'
py38-mysql run-test: commands[0] | /Users/sanskarjaiswal/Projects/django/.tox/py38-mysql/bin/python runtests.py --settings=test_mysql
Testing against Django installed in '/Users/sanskarjaiswal/Projects/django/django'
Traceback (most recent call last):
  File "/Users/sanskarjaiswal/Projects/django/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Users/sanskarjaiswal/Projects/django/.tox/py38-mysql/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/sanskarjaiswal/Projects/django/.tox/py38-mysql/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/sanskarjaiswal/Projects/django/.tox/py38-mysql/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
  Reason: image not found

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "runtests.py", line 567, in <module>
    failures = django_tests(
  File "runtests.py", line 287, in django_tests
    state = setup(verbosity, test_labels, parallel, start_at, start_after)
  File "runtests.py", line 187, in setup
    django.setup()
  File "/Users/sanskarjaiswal/Projects/django/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/sanskarjaiswal/Projects/django/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/sanskarjaiswal/Projects/django/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/sanskarjaiswal/Projects/django/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/sanskarjaiswal/Projects/django/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/sanskarjaiswal/Projects/django/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/sanskarjaiswal/Projects/django/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/sanskarjaiswal/Projects/django/django/db/models/options.py", line 206, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/sanskarjaiswal/Projects/django/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/sanskarjaiswal/Projects/django/django/db/utils.py", line 214, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/sanskarjaiswal/Projects/django/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/sanskarjaiswal/Projects/django/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
ERROR: InvocationError for command /Users/sanskarjaiswal/Projects/django/.tox/py38-mysql/bin/python runtests.py --settings=test_mysql (exited with code 1)
___________________________________ summary ____________________________________
ERROR:   py38-mysql: commands failed

Running ./runtests.py --settings=test_mysql also results in the same error.

When I create a Django project with a MySQL backend, it works perfectly. Any possible diagnosis? :thinking:

There’s a bug with mysqlclient where cached wheels compiled against different mysql versions persist.

Try changing the tox file to add install_command under the [testenv] header:

[testenv]
install_command = python -m pip install --no-cache-dir {opts} {packages}

This will force the wheel to be compiled from scratch.

1 Like

Thanks @adamchainz! :smiley: