How do I run the pytest tests in Django?

Hi there,

Instead of using Django’s default TestCase module to create test, I am going more pythonic way to use pytest library to create Django unit tests. I have created all tests, but when I try to run those with the following command:

py.test

I get the following error

ERROR test_models.py - django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. Y...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Full error details are below:

========================================================== test session starts ===========================================================
platform win32 -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\Projects\meistery\projects\pricing_password_management_poc\inviteandresetpass\apps\accounts\tests
collected 0 items / 1 error

================================================================= ERRORS =================================================================
____________________________________________________ ERROR collecting test_models.py _____________________________________________________ 
test_models.py:1: in <module>
    from apps.accounts.models import User, Invitation, Workspace
..\models.py:4: in <module>
    from django.contrib.contenttypes.models import ContentType
..\..\..\..\..\..\venvs\inviteandresetpass\lib\site-packages\django\contrib\contenttypes\models.py:131: in <module>
    class ContentType(models.Model):
..\..\..\..\..\..\venvs\inviteandresetpass\lib\site-packages\django\db\models\base.py:127: in __new__
    app_config = apps.get_containing_app_config(module)
..\..\..\..\..\..\venvs\inviteandresetpass\lib\site-packages\django\apps\registry.py:260: in get_containing_app_config
    self.check_apps_ready()
..\..\..\..\..\..\venvs\inviteandresetpass\lib\site-packages\django\apps\registry.py:137: in check_apps_ready
    settings.INSTALLED_APPS
..\..\..\..\..\..\venvs\inviteandresetpass\lib\site-packages\django\conf\__init__.py:87: in __getattr__
    self._setup(name)
..\..\..\..\..\..\venvs\inviteandresetpass\lib\site-packages\django\conf\__init__.py:67: in _setup
    raise ImproperlyConfigured(
E   django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define 
the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
======================================================== short test summary info ========================================================= 
ERROR test_models.py - django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. Y...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
============================================================ 1 error in 1.18s ============================================================

Anyone knows that what should I do in order to run the tests?

I use pytest-django Documentation — pytest-django documentation

1 Like