【问题标题】:error in setting up lettuce with django test使用 django 测试设置生菜时出错
【发布时间】:2012-03-14 03:07:18
【问题描述】:

我正在尝试将生菜与标准 django 测试联系起来。生菜单独工作时效果很好。但是,当我从 django.test.client 导入 Client 时,出现错误: "无法导入设置,因为环境变量 DJANGO_SETTINGS_MODULE 未定义。"

我的 steps.py 看起来像:

from lettuce import *
from django.test.client import Client

@before.all
    def set_browser():
    world.browser = Client()

@step(r'I access the url "(.*)"')
def have_the_number(step,url):
    world.response = world.browser.get(url)

我收到以下错误

            File "/usr/local/bin/lettuce", line 9, in <module>
    load_entry_point('lettuce==0.1.34', 'console_scripts', 'lettuce')()
File "/Library/Python/2.7/site-packages/lettuce-0.1.34-py2.7.egg/lettuce/lettuce_cli.py", line 71, in main
    result = runner.run()
File "/Library/Python/2.7/site-packages/lettuce-0.1.34-py2.7.egg/lettuce/__init__.py", line 114, in run
    self.loader.find_and_load_step_definitions()
  File "/Library/Python/2.7/site-packages/lettuce-0.1.34-py2.7.egg/lettuce/fs.py", line 42, in find_and_load_step_definitions
    module = __import__(to_load)
  File "/Users/aliahsan/djcode/drftest/features/steps.py", line 2, in <module>
    from django.test.client import Client
  File "/Library/Python/2.7/site-packages/django/test/__init__.py", line 5, in <module>
    from django.test.client import Client, RequestFactory
  File "/Library/Python/2.7/site-packages/django/test/client.py", line 27, in <module>
    from django.db import transaction, close_connection
  File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 14, in <module>
    if not settings.DATABASES:
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

我尝试在 init.py 文件中设置 DJANGO_SETTINGS_MODULE 变量,但它也不起作用。 有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: django testing automated-tests django-settings lettuce


    【解决方案1】:

    我实际上一直在关注入门教程,并在 app 文件夹中使用 Lettuce 命令,但它没有找到 django 的任何设置文件。在意识到之后得到了修复, 运行

    python manage.py harvest
    

    项目文件夹中的命令,它可以工作。如果您只想测试项目中的特定应用程序,您可以在收获旁边提及该应用程序。默认情况下,它会测试您的设置文件中安装的所有应用的所有功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2019-07-27
      • 2015-05-02
      • 2016-06-20
      • 2019-08-04
      • 2017-04-06
      相关资源
      最近更新 更多