如何 运行 表现出 PyCharm 中的 django 特性?
How to run behave-django features in PyCharm?
背景
我有一个 Django 项目 ("api") 具有单元测试和行为功能。其结构的相关部分是
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/
我使用 behave-django。 python manage.py behave
有效。
我用PyCharm。它被配置为使用项目的 virtualenv。它的 Django 支持是这样配置的:
运行 Django 和 运行 在 PyCharm 内进行单元测试测试有效。
问题
当我尝试 运行 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
我怎样才能 运行 我的行为特征符合 PyCharm 中的预期方式?
我尝试过但没有用的东西
将 运行 配置的工作目录设置为 .../api/code
没有任何区别。
上面堆栈跟踪中的建议似乎是假的,因为设置文件是默认的,我不需要设置 DJANGO_SETTINGS_MODULE
来使其他任何东西在 PyCharm 中或之外工作,但是如果我将 DJANGO_SETTINGS_MODULE=api.settings
添加到 PyCharm 中的 behave 运行 配置中,我会得到
[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.
当我运行 django.setup()
在features/environment.py
(不是在before_all
,而是在顶层)我走得更远,但是
- 测试 运行 在我的常规数据库中,而不是在临时测试数据库中,并且
- behave-django 功能如
context.base_url
不可用,因此一些测试失败。
我怎样才能完成这项工作?
(披露:我是 behave-django
的维护者之一,behave 和 Django 的集成。)
Behave 是一个独立的工具
实际问题是behave
并不是要了解Django并融入其中。它旨在 运行 作为外部工具。这就是 PyCharm (2016.3.1) 似乎对 g运行ted 所做的,以及当 PyCharm 运行s 测试时会发生什么。测试是 运行 就像你从终端 运行 behave
( 不是 python manage.py behave
!)。
在 code/features/steps/common.py
的第 5 行中,您对 Django(用户模型)进行了导入,这要求 Django 已经 运行ning,例如通过管理命令 (runserver
)。在 behave-django
中,我们要求管理命令改为 运行,以便 LiveServerTestCase
可以在到达您的代码之前启动。 Django 将准备就绪,因为我们让它在访问您的代码之前启动 运行 服务器。
如果 behave-django
开箱即用,JetBrains 必须考虑到这一点。
替代整合(部分解决方案)
您 可以 仅使用 behave
集成到 Django 中,不需要 behave-django
,在 environment.py
中添加几行额外的代码.您 将 失去我们集成的一些功能,但是您必须在所有项目中复制这种集成方法。
查看 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
测试设置。
剩下的可能取决于 JetBrains。对不起,坏消息!
背景
我有一个 Django 项目 ("api") 具有单元测试和行为功能。其结构的相关部分是
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/
我使用 behave-django。 python manage.py behave
有效。
我用PyCharm。它被配置为使用项目的 virtualenv。它的 Django 支持是这样配置的:
运行 Django 和 运行 在 PyCharm 内进行单元测试测试有效。
问题
当我尝试 运行 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
我怎样才能 运行 我的行为特征符合 PyCharm 中的预期方式?
我尝试过但没有用的东西
将 运行 配置的工作目录设置为 .../api/code
没有任何区别。
上面堆栈跟踪中的建议似乎是假的,因为设置文件是默认的,我不需要设置 DJANGO_SETTINGS_MODULE
来使其他任何东西在 PyCharm 中或之外工作,但是如果我将 DJANGO_SETTINGS_MODULE=api.settings
添加到 PyCharm 中的 behave 运行 配置中,我会得到
[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.
当我运行 django.setup()
在features/environment.py
(不是在before_all
,而是在顶层)我走得更远,但是
- 测试 运行 在我的常规数据库中,而不是在临时测试数据库中,并且
- behave-django 功能如
context.base_url
不可用,因此一些测试失败。
我怎样才能完成这项工作?
(披露:我是 behave-django
的维护者之一,behave 和 Django 的集成。)
Behave 是一个独立的工具
实际问题是behave
并不是要了解Django并融入其中。它旨在 运行 作为外部工具。这就是 PyCharm (2016.3.1) 似乎对 g运行ted 所做的,以及当 PyCharm 运行s 测试时会发生什么。测试是 运行 就像你从终端 运行 behave
( 不是 python manage.py behave
!)。
在 code/features/steps/common.py
的第 5 行中,您对 Django(用户模型)进行了导入,这要求 Django 已经 运行ning,例如通过管理命令 (runserver
)。在 behave-django
中,我们要求管理命令改为 运行,以便 LiveServerTestCase
可以在到达您的代码之前启动。 Django 将准备就绪,因为我们让它在访问您的代码之前启动 运行 服务器。
如果 behave-django
开箱即用,JetBrains 必须考虑到这一点。
替代整合(部分解决方案)
您 可以 仅使用 behave
集成到 Django 中,不需要 behave-django
,在 environment.py
中添加几行额外的代码.您 将 失去我们集成的一些功能,但是您必须在所有项目中复制这种集成方法。
查看 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
测试设置。
剩下的可能取决于 JetBrains。对不起,坏消息!