Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: settings for tests of a reusable app?

I created a small app in Django and runserver and admin works fine.

I wrote some tests which can call with python manage.py test and the tests pass.

Now I would like to call one particular test via PyCharm.

This fails like this:

/home/guettli/x/venv/bin/python 
   /snap/pycharm-community/179/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py 
   --path /home/guettli/x/xyz/tests.py
Launching pytest with arguments /home/guettli/x/xyz/tests.py in /home/guettli/x

============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 --
cachedir: .pytest_cache
rootdir: /home/guettli/x
collecting ... 
xyz/tests.py:None (xyz/tests.py)
xyz/tests.py:6: in <module>
    from . import views
xyz/views.py:5: in <module>
    from xyz.models import Term, SearchLog, GlobalConfig
xyz/models.py:1: in <module>
    from django.contrib.auth.models import User
venv/lib/python3.6/site-packages/django/contrib/auth/models.py:2: in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
venv/lib/python3.6/site-packages/django/contrib/auth/base_user.py:47: in <module>
    class AbstractBaseUser(models.Model):
venv/lib/python3.6/site-packages/django/db/models/base.py:107: in __new__
    app_config = apps.get_containing_app_config(module)
venv/lib/python3.6/site-packages/django/apps/registry.py:252: in get_containing_app_config
    self.check_apps_ready()
venv/lib/python3.6/site-packages/django/apps/registry.py:134: in check_apps_ready
    settings.INSTALLED_APPS
venv/lib/python3.6/site-packages/django/conf/__init__.py:76: in __getattr__
    self._setup(name)
venv/lib/python3.6/site-packages/django/conf/__init__.py:61: in _setup
    % (desc, ENVIRONMENT_VARIABLE))
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.

Assertion failed
collected 0 items / 1 error

I understand the background: My app xyz is reusable. It does not contain any settings. The app does not know (and should not know) my project. But the settings are in my project.

How to solve this?

I read the great django docs, but could not find a solution.

How to set DJANGO_SETTINGS_MODULE if you execute one particular test directly from PyCharm with "Run" (ctrl-shift-F10)?

like image 395
guettli Avatar asked Mar 15 '20 13:03

guettli


3 Answers

You can add DJANGO_SETTINGS_MODULE as an environmental variable:

In the menu: Run -> Edit Configurations -> Templates -> Python Tests -> Unittests

Pycharm configs

And delete old "Unittests for tests...." entries.

like image 82
jTiKey Avatar answered Oct 18 '22 00:10

jTiKey


You can specify the settings in your test command

Assuming your in the xyz directory, and the structure is

/xyz
   - manage.py
   - xyz/
       - settings.py

The following command should work

python manage.py test --settings=xyz.settings
like image 40
ibaguio Avatar answered Oct 18 '22 00:10

ibaguio


Edited: For this method to work django support should me enabled in pycharm. I guess it should be possible to setup the equivalent template in the community edition version of pycharm.

Method with django support enabled:

I find that the most convenient way that also allow you to directly click on a particular test case and run it directly within pycharm without having to set the settings every time is to do the following:

->Edit configuration (Run/Debug configurations)

->Templates and select "Django Tests"

->Tick "Custom settings" and then browse to the settings you want use.

Then when you launch tests directly within pycharm it will use it as a template.

If you test with any other supported method by pycharm, you can pick testing framework in pycharm: Choose testing framework and then create a template for it.

like image 1
Daniel Backman Avatar answered Oct 18 '22 00:10

Daniel Backman