Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run Django tests in pycharm community edition?

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.

like image 824
jonalv Avatar asked Oct 29 '22 18:10

jonalv


2 Answers

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.

like image 197
neverwalkaloner Avatar answered Nov 12 '22 23:11

neverwalkaloner


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.

like image 35
MikeyE Avatar answered Nov 12 '22 22:11

MikeyE