[英]How to run behave-django features in PyCharm?
I have a Django project ("api") with unittest tests and behave features.我有一个带有单元测试和行为功能的 Django 项目(“api”)。 Relevant parts of its structure are
其结构的相关部分是
code/ # i.e. the Django root is not the root of the project
manage.py
api/
settings.py
# and other Django stuff
app/
# Django app stuff
features/
environment.py
steps/
foo.feature
virtualenv/
I use behave-django.我使用行为Django。
python manage.py behave
works. python manage.py behave
有效。
I use PyCharm.我使用 PyCharm。 It's configured to use the project's virtualenv.
它被配置为使用项目的 virtualenv。 Its Django support is configured thus:
它的 Django 支持是这样配置的:
Running Django and running unittest tests inside PyCharm works.在 PyCharm 中运行 Django 并运行单元测试测试是有效的。
When I attempt to run a behave feature in PyCharm (by hitting control-shift-R when editing the feature file and choosing the behave run context configuration) I get当我尝试在 PyCharm 中运行行为功能时(通过在编辑功能文件时点击 control-shift-R 并选择行为运行上下文配置)我得到
/Users/dave/data/projects/api/code/virtualenv/bin/python2.7 "/Applications/PyCharm 2016.3.app/Contents/helpers/pycharm/behave_runner.py"
Testing started at 04:31 ...
Traceback (most recent call last):
File "/Applications/PyCharm 2016.3.app/Contents/helpers/pycharm/behave_runner.py", line 294, in <module>
_BehaveRunner(my_config, base_dir).run()
File "/Applications/PyCharm 2016.3.app/Contents/helpers/pycharm/_bdd_utils.py", line 91, in run
number_of_tests = self._get_number_of_tests()
File "/Applications/PyCharm 2016.3.app/Contents/helpers/pycharm/_bdd_utils.py", line 211, in _get_number_of_tests
for feature in self._get_features_to_run():
File "/Applications/PyCharm 2016.3.app/Contents/helpers/pycharm/behave_runner.py", line 231, in _get_features_to_run
self.__real_runner.run()
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/behave/runner.py", line 672, in run
return self.run_with_paths()
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/behave/runner.py", line 678, in run_with_paths
self.load_step_definitions()
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/behave/runner.py", line 658, in load_step_definitions
exec_file(os.path.join(path, name), step_module_globals)
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/behave/runner.py", line 304, in exec_file
exec(code, globals, locals)
File "code/features/steps/common.py", line 5, in <module>
from django.contrib.auth.models import User
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 53, in AbstractBaseUser
password = models.CharField(_('password'), max_length=128)
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1043, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, 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
How can I run my behave features in the intended way in PyCharm?如何在 PyCharm 中以预期方式运行我的行为功能?
Setting the run configuration's working directory to .../api/code
didn't make a difference.将运行配置的工作目录设置为
.../api/code
没有任何区别。
The suggestion in the stack trace above seems bogus, because the settings file is the default, and I didn't need to set DJANGO_SETTINGS_MODULE
to make anything else work in or out of PyCharm, but if I add DJANGO_SETTINGS_MODULE=api.settings
to the behave run configuration in PyCharm I get上面堆栈跟踪中的建议似乎是错误的,因为设置文件是默认文件,我不需要设置
DJANGO_SETTINGS_MODULE
来使任何其他工作在 PyCharm 中或外工作,但是如果我将DJANGO_SETTINGS_MODULE=api.settings
添加到行为在 PyCharm 中运行配置我得到
[some duplicate stack frames removed]
File "code/features/steps/common.py", line 5, in <module>
from django.contrib.auth.models import User
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "/Users/dave/data/projects/api/code/virtualenv/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
When I run django.setup()
in features/environment.py
(not in before_all
, but at the top level) I get farther, but当我在
features/environment.py
运行django.setup()
时(不是在before_all
,而是在顶层)我走得更远,但是
context.base_url
are not available, so some tests fail.context.base_url
类的行为 django 功能不可用,因此某些测试失败。 How can I make this work?我怎样才能使这项工作?
(Disclosure: I'm one of the maintainers of behave-django
, the integration of behave and Django.) (披露:我是
behave-django
的维护者之一,行为和 Django 的集成。)
The actual problem is that behave
is not meant to be aware of Django and integrate into it.实际问题是,
behave
并不意味着了解 Django 并集成到其中。 It's meant to run as an external tool.它旨在作为外部工具运行。 And that's what PyCharm (2016.3.1) seems to take for granted, and what happens when PyCharm runs the tests.
这就是 PyCharm (2016.3.1) 似乎认为理所当然的,以及 PyCharm 运行测试时会发生什么。 The tests are run just as if you ran
behave
( not python manage.py behave
!) from the terminal.测试的运行就像您从终端运行
behave
(不是python manage.py behave
!)一样。
In code/features/steps/common.py
, line 5, you have an import towards Django (User model), which requires Django being already running, eg via a management command ( runserver
).在
code/features/steps/common.py
第 5 行,您有一个对 Django(用户模型)的导入,这需要 Django 已经在运行,例如通过管理命令 ( runserver
)。 In behave-django
we require a management command to be run instead, so that a LiveServerTestCase
can launch before your code is reached.在
behave-django
我们需要运行管理命令,以便LiveServerTestCase
可以在到达您的代码之前启动。 Django will be ready, because we make it launch a runserver before it hits your code. Django 将准备就绪,因为我们让它在命中您的代码之前启动一个运行服务器。
JetBrains would have to take this into account if behave-django
should work out of the box.如果
behave-django
应该开箱即用,JetBrains 就必须考虑到这一点。
You can integrate into Django with behave
only, no need for behave-django
, with a few extra lines of code in environment.py
.您可以只使用
behave
集成到 Django 中,不需要behave-django
,在environment.py
几行额外的代码。 You will lose a few features of our integration, and you'll have to replicate this integration approach over all your projects, though.您将失去我们集成的一些功能,但是您必须在所有项目中复制这种集成方法。
See the Manual Integration section of the behave docs.请参阅行为文档的手动集成部分。 You could try to replicate
behave-django
's behavior by adding some of its code to access the live_server_url
to your test setup.您可以尝试通过添加一些代码来访问
live_server_url
到您的测试设置来复制behave-django
的行为。
The rest is probably up to JetBrains.剩下的可能取决于 JetBrains。 Sorry for the bad news!
关于这些坏消息我很遗憾!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.