我正在尝试在我的django应用程序上运行单元测试但是我遇到了以下错误。运行应用程序很好。我正在使用pycharm 3.0。
C:\Users\user\my-project-env\Scripts\python.exe "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py" C:/Users/user/PycharmProjects/my-project/my_app/ true
Testing started at 20:16 ...
Traceback (most recent call last):
File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 113, in <module>
modules = loadModulesFromFolderRec(a[0])
File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 63, in loadModulesFromFolderRec
os.path.walk(folder, walkModules, (modules, pattern))
File "C:\Users\user\my-project-env\lib\ntpath.py", line 259, in walk
func(arg, top, names)
File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 51, in walkModules
modules.append(loadSource(os.path.join(dirname, name)))
File "K:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\utrunner.py", line 40, in loadSource
module = imp.load_source(moduleName, fileName)
File "C:/Users/user/PycharmProjects/my-project/my_app/tests.py", line 8, in <module>
from django.test import TestCase
File "C:\Users\user\my-project-env\lib\site-packages\django\test\__init__.py", line 5, in <module>
from django.test.client import Client, RequestFactory
File "C:\Users\user\my-project-env\lib\site-packages\django\test\client.py", line 21, in <module>
from django.db import close_connection
File "C:\Users\user\my-project-env\lib\site-packages\django\db\__init__.py", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "C:\Users\user\my-project-env\lib\site-packages\django\conf\__init__.py", line 53, in __getattr__
self._setup(name)
File "C:\Users\user\my-project-env\lib\site-packages\django\conf\__init__.py", line 46, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, 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
答案 0 :(得分:5)
您使用的是Djangos单元测试库,还是使用蟒蛇? Django需要设置一些东西才能加载,例如模型,即它需要设置,如堆栈跟踪所示。
如果您按照文档https://docs.djangoproject.com/en/1.5/topics/testing/overview/进行操作,您会发现Django已经为正常的单元测试功能做了一些插件。使用它并调用Django单元测试将为您运行它。将其作为Django单元测试运行意味着使用
运行它们 $ ./manage.py test
或者因为你使用pycharm,你可以选择将单元测试作为Django单元测试来运行,PyCharm会在你调用测试时问你。
答案 1 :(得分:4)
看看Traceback:
django.core.exceptions.ImproperlyConfigured:
请求设置DATABASES,但未配置设置。您必须先定义环境变量DJANGO_SETTINGS_MODULE或在访问设置前调用settings.configure()。
答案 2 :(得分:1)
更多是由于Pycharm中的设置问题,请看这个问题。 Pycharm error: Improperly configured