【问题标题】:PyCharm 1.1 - using specific settings for django test runnerPyCharm 1.1 - 使用 django 测试运行器的特定设置
【发布时间】:2011-06-01 10:39:39
【问题描述】:

有没有人有在 PyCharm 中为 Django 测试运行程序使用不同设置文件的经验?

我的项目组成如下:

+penguinproject
+apps
+settings
--__init__.py
--common.py # this one is imported by the others below
--development.py
--production.py
--staging.py
+static
...

使用 manage.py 命令(并添加 --settings=settings.development)运行测试工作正常,但我想使用内置的测试运行程序(因为它与 IDE 更好地集成)。

认为问题是我的设置没有正确加载,因为我收到了这个错误:

E:\Development\django_projects\penguinproject\Scripts\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py" --client 127.0.0.1 --port 49898 --file "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py" test items
Testing started at 20:36 ...
pydev debugger: warning: psyco not available for speedups (the debugger will still work correctly, but a bit slower)
pydev debugger: starting
E:\Development\django_projects\penguinproject\lib\site-packages\path.py:32: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
settings file:  development
pycharm django settings imported
Manager file:  manage
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 1165, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 929, in run
execfile(file, globals, locals) #execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py", line 15, in <module>
run_module(manage_file, None, '__main__')
File "c:\python26\Lib\runpy.py", line 140, in run_module
fname, loader, pkg_name)
File "c:\python26\Lib\runpy.py", line 34, in _run_code
exec code in run_globals
File "E:\Development\django_projects\penguinproject\yabe\manage.py", line 11, in <module>
execute_manager(settings)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager
utility.execute()
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\base.py", line 220, in execute
output = self.handle(*args, **options)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\core\management\commands\test.py", line 37, in handle
failures = test_runner.run_tests(test_labels)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\test\simple.py", line 396, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\test\simple.py", line 285, in build_suite
app = get_app(label)
File "E:\Development\django_projects\penguinproject\lib\site-packages\django\db\models\loading.py", line 140, in get_app
raise ImproperlyConfigured("App with label %s could not be found" % app_label)
django.core.exceptions.ImproperlyConfigured: App with label items could not be found

我在命令行中使用 manage.py 时没有出现此错误(在 IDE 中调用时也是如此),这让我认为 development.py 设置未正确加载。调试显示在 get_app() (在 loading.py 模块中)settings.INSTALLED_APPS 是一个空列表。

有没有人用这个设置文件成功设置了测试运行器?

非常感谢!

【问题讨论】:

  • 我想我在 Eclipse 中遇到了类似的问题。它需要 django 被引入并且 djanjo.setup() 在某处运行。

标签: django unit-testing pycharm


【解决方案1】:

我建议对您的项目结构稍作修改。看看这个更详细的例子:

http://www.revsys.com/blog/2014/nov/21/recommended-django-project-layout/

您会看到有一个顶级目录和一个同名的子目录,所有后续设置文件都在其中。

对于另一个参考,请查看可用于创建初始项目结构的“startproject”命令:

https://docs.djangoproject.com/en/1.10/ref/django-admin/#django-admin-startproject

HTH

【讨论】:

    【解决方案2】:

    您必须在该应用程序上创建一个models.py

    http://code.djangoproject.com/ticket/3310

    【讨论】:

    • 它有:apps 目录包含所有必要的文件(tests.py、models.py、views.py 等),每个应用程序。如果我没有 models.py 文件,那么从命令行运行的测试也将不起作用。
    猜你喜欢
    • 2017-04-06
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2013-12-22
    • 2017-12-05
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多