代码之家  ›  专栏  ›  技术社区  ›  Dave Schweisguth

如何在PyCharm中运行behave django功能?

  •  2
  • Dave Schweisguth  · 技术社区  · 8 年前

    出身背景

    我有一个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/
    

    我过去常表现得像django。 python manage.py behave 作品

    我用PyCharm。它被配置为使用项目的virtualenv。其Django支持配置如下:

    enter image description here

    在PyCharm中运行Django和运行unittest测试是可行的。

    问题

    当我尝试在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-run配置,我得到

    [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 ,但在顶层)我走得更远,但是

    • 测试在我的常规数据库中运行,而不是在临时测试数据库中运行
    • context.base_url 不可用,因此某些测试失败。

    我怎样才能让它工作?

    1 回复  |  直到 4 年前
        1
  •  1
  •   Peterino Buhake Sindi    5 年前

    (披露:我是 behave-django ,behave和Django的集成。)

    Behave是一个独立的工具

    实际的问题是 behave 并不意味着了解Django并融入其中。它是作为一个外部工具运行的。这就是PyCharm(2016.3.1)似乎认为理所当然的,以及PyCharm运行测试时发生的情况。测试的运行方式与您的运行方式相同 表现 ( python manage.py behave !) 从终端。

    在里面 code/features/steps/common.py ,第5行,您有一个针对Django(用户模型)的导入,这需要Djang0已经在运行,例如通过管理命令( runserver ). 在里面 表现得像django 我们需要运行一个管理命令,以便 LiveServerTestCase 可以在到达代码之前启动。Django已经准备好了,因为我们让它在碰到代码之前启动一个runserver。

    如果出现以下情况,JetBrains必须考虑到这一点: 表现得像django 应该是开箱即用的。

    可以 与Django集成 表现 只是,不需要 表现得像django ,在 environment.py 失去了我们集成的一些特性,您将不得不在所有项目中复制这种集成方法。

    Manual Integration 部分 表现 文件。你可以尝试复制 表现得像django code to access the live_server_url 到您的测试设置。

    其余的可能是喷气式飞机。对不起,坏消息!