简体   繁体   English

我可以在 pycharm 社区版中运行 Django 测试吗?

[英]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.我有一个 Django 项目,里面有一堆我刚刚导入到 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:我试图创建一个基于路径的测试配置,并给出 manage.py 路径和测试命令以及我的设置文件作为参数,但我收到以下神秘的错误消息:

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.我发现在 PyCharm 中运行 Django 测试,但它似乎根本不相关(更旧的版本?),因为我收到不同的错误,事情看起来很不一样。

You can use standard python configuration, not python unittest for django tests.对于 Django 测试,您可以使用标准的 python 配置,而不是 python unittest。 Just add new python run/debug configuration, select manage.py as file and specify parameters test --settings=Modeling.settings.tests .只需添加新的 python 运行/调试配置,选择manage.py作为文件并指定参数test --settings=Modeling.settings.tests

You can also edit PyCharm's test runner helper script.您还可以编辑 PyCharm 的测试运行程序帮助程序脚本。 For me it is located /opt/JetBrains/PyCharm/plugins/python-ce/helpers/pycharm/_jb_nosetest_runner.py .对我来说,它位于/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() .然后执行import django并在主入口点之后立即执行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 .然后您可以右键单击Project工具窗口中的任何测试,然后单击Run test You can also right any directory where unit tests file are and click Run Unittests in tests .您还可以右键单击单元测试文件所在的任何目录,然后单击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.注意:在运行单元测试之前,您需要确保将所有必需的环境变量添加到运行配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM