Pycharm - no django tests found but run in console

I followed this tutorial https://docs.djangoproject.com/en/3.0/intro/tutorial05/

The tests can be run by the command (polls is the application name)

python manage.py test polls

However under PyCharm IDEA, when I click on the green arrow (line 21-22).

The message is "No tests were found"

enter image description here

The stack trace is gibberish to me

Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py", line 35, in sys.exit(main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 100, in init self.parseArgs(argv) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 147, in parseArgs self.createTests() File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 159, in createTests self.module) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 220, in loadTestsFromNames suites = [self.loadTestsFromName(name, module) for name in names] File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 220, in suites = [self.loadTestsFromName(name, module) for name in names] File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName module = import(module_name) File "/Users/raychenon/Projects/python/django/mysite/polls/test_views.py", line 8, in from .models import Question File "/Users/raychenon/Projects/python/django/mysite/polls/models.py", line 8, in class Question(models.Model): File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 107, in new app_config = apps.get_containing_app_config(module) File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config self.check_apps_ready() File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 134, in check_apps_ready settings.INSTALLED_APPS File "/usr/local/lib/python3.7/site-packages/django/conf/init.py", line 76, in getattr self._setup(name) File "/usr/local/lib/python3.7/site-packages/django/conf/init.py", line 61, in _setup % (desc, ENVIRONMENT_VARIABLE)) 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.

Process finished with exit code 1

Empty suite

PyCharm IDE setup

My python interpreter is set correctly enter image description here

Research

I already looked at Pycharm - no tests were found? . Unrelated error, my test functions names start by test_*


Based on your comment saying your django dependencies cannot be found, I suspect you didn't setup the python interpreter properly for your project.

Eg. if you use virtualenv, you need to setup your python interpreter to point to the python bin inside your virtualenv, so Pycharm can find your packages.

Eg. photo below shows the python interpreter for my "pmas" virtualenv.

enter image description here

enter image description here


The reason we use manage.py when working with Django is that it manages environment for us.

But working with pycharm, unless you specify things manually, a python file that has test footprints in it (importing unittest and having method names startng with "test_") are just modules we can test without any external dependencies.

in cases you use a framework such as Django, then you need to play by their rules. you will either use their tools or set whatever is needed manually.

I don't know if pycharm had this support at the time but this pycharm/django-support may help.

if that does not cover your needs, then you will need to work on a few things mentioned in Django Settings (check for other versions if you use an older one, or newer if you are from the future). your error message already mentions about this.

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.
  • you can either open project settings in pycharm and add this key-value in environment variables for it: DJANGO_SETTINGS_MODULE=mysite.settings

  • or use this in your code wherever it is needed. (check the settings page for details)

    from django.conf import settings
    settings.configure(DEBUG=True)