gpt4 book ai didi

python - 使用django.test.client测试app http请求,报错

转载 作者:行者123 更新时间:2023-11-28 18:48:46 26 4
gpt4 key购买 nike

我们开始为我们的 API(使用 Django Rest Framework 创建)编写单元测试。我们决定从简单开始,使用内置的 unittest 和 django.test.client 类。我已经编写了一个测试的 stub ,它运行得很好:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import unittest

# from django.test.client import Client

class TestGrowth(unittest.TestCase):
def setUp(self):
# self.client = Client()
pass

def test_numbers_3_4(self):
self.assertEqual(4, 4, True)

def test_strings_a_3(self):
self.assertEqual('andy', 'andy', True)


def suite():
suite = unittest.TestSuite()

# Add test cases to suite
suite.addTests(unittest.makeSuite(TestGrowth))

return suite

if __name__ == '__main__':
# Run test suite
unittest.TextTestRunner(verbosity=2).run(suite())

但是,一旦我取消注释 from django.test.client import Client 行我得到一个错误:

Traceback (most recent call last):
File "./AccountGrowth.py", line 8, in <module>
from django.test.client import Client
File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/__init__.py", line 5, in <module>
from django.test.client import Client, RequestFactory
File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/client.py", line 21, in <module>
from django.db import close_connection
File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/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.

我们在 settings.py 中定义 DATABASES 如下:

if "DATABASE_URL" in os.environ:
# Parse database configuration from $DATABASE_URL
DATABASES = {
'default': dj_database_url.config()
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'emmasocial', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}

我正在 <app>/api/tests 内的 vagrant shell 中运行测试使用以下命令 python ./AccountGrowth.py .

谁能解释一下为什么会这样?

最佳答案

您不是通过 Django 的测试运行器运行测试,它会为您设置好所有这些。您不需要 if __name__ == '__main__' block 或它对 run() 的显式调用,您应该通过 ./manage 运行测试.py 测试.

关于python - 使用django.test.client测试app http请求,报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16346430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com