4

I have a Django project with a bunch of tests that I have just imported into PyCharm. I managed to get it configured so that I can run the server and that works fine but now I want to run the tests as well. I have tried to create a Path based Test Configuration and given the manage.py path and the test command as well as my settings file as parameters but I get the following cryptic error message:

Testing started at 10:12 ...
/Users/jonathan/anaconda/bin/python "/Applications/PyCharm CE.app/Contents/helpers/pycharm/_jb_unittest_runner.py" --path /Users/jonathan/Work/GenettaSoft/modeling-web/modeling/manage.py -- test --settings=Modeling.settings.tests
Launching unittests with arguments python -m unittest /Users/jonathan/Work/GenettaSoft/modeling-web/modeling/manage.py test --settings=Modeling.settings.tests in /Users/jonathan/Work/GenettaSoft/modeling-web/modeling

usage: python -m unittest [-h] [-v] [-q] [--locals] [-f] [-c]
                          [tests [tests ...]]
python -m unittest: error: unrecognized arguments: --settings=Modeling.settings.tests

Process finished with exit code 2
Empty test suite.

It must be running things in the wrong way somehow. Can this not be done at all?

Ps. I found Running Django tests in PyCharm but it does not seem related at all (much older version?), cause I get different errors and things look very different.

jonalv
  • 3,887
  • 6
  • 37
  • 58
  • Check [\[SO\]: Run//ebug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)](https://stackoverflow.com/questions/42989471/run-debug-a-django-applications-unittests-from-the-mouse-right-click-context-me) – CristiFati Mar 07 '19 at 01:00
  • 1
    Possible duplicate of [Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition?](https://stackoverflow.com/questions/42989471/run-debug-a-django-applications-unittests-from-the-mouse-right-click-context-me) – jonalv Mar 18 '19 at 12:50

2 Answers2

2

You can use standard python configuration, not python unittest for django tests. Just add new python run/debug configuration, select manage.py as file and specify parameters test --settings=Modeling.settings.tests.

neverwalkaloner
  • 38,255
  • 4
  • 53
  • 70
  • 1
    Okay so there is no way of getting integration with the testing UI of PyCharm then. :( – jonalv Feb 14 '18 at 12:36
  • 1
    Check this: [\[SO\]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)](https://stackoverflow.com/questions/42989471/run-debug-a-django-applications-unittests-from-the-mouse-right-click-context-me) – CristiFati Mar 07 '19 at 01:11
0

You can also edit PyCharm's test runner helper script. For me it is located /opt/JetBrains/PyCharm/plugins/python-ce/helpers/pycharm/_jb_nosetest_runner.py. Then do import django and immediately after the main entry point do django.setup(). I have included the entire file from my machine for completeness.

# coding=utf-8
import re

import nose
import sys 
import django

from _jb_runner_tools import jb_start_tests, jb_patch_separator, jb_doc_args, JB_DISABLE_BUFFERING
from teamcity.nose_report import TeamcityReport

if __name__ == '__main__':
    django.setup()
    path, targets, additional_args = jb_start_tests()
    sys.argv += [path] if path else jb_patch_separator(targets, fs_glue="/", python_glue=".", fs_to_python_glue=".py:")
    sys.argv += additional_args
    if JB_DISABLE_BUFFERING and "-s" not in sys.argv:
        sys.argv += ["-s"]
    jb_doc_args("Nosetest", sys.argv)
    sys.exit(nose.main(addplugins=[TeamcityReport()]))

Then you can right click any test in the Project tool window and click Run test. You can also right any directory where unit tests file are and click Run Unittests in tests.

NOTE: you will need to make sure all required environment variables on added to the Run Configuration before running unit tests.

MikeyE
  • 1,378
  • 1
  • 13
  • 30