环境:
Intellij PyCharm,Python Django,Sqlite3
我使用标准的Django安装项目。
我尝试编写一些单元测试,但是遇到以下错误。
经过研究,我最终来到了这里。
数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
tests.py代码
from django.test import TestCase as djangoTestCase
import os
import unittest
# Create your tests here.
class TestExample(djangoTestCase):
def setUp(self):
print('setup')
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def test_abc(self):
self.assertEqual(1,1)
def tearDown(self):
print('tearDown')
if __name__ == '__main__':
unittest.main()
这是输出
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
我试图通过阅读文档来查找错误,但是没有成功。
可能是什么问题呢?
最佳答案
使用Django测试,您不需要这部分:
if __name__ == '__main__':
unittest.main()
您只需使用
python manage.py test
命令运行测试。
关于django - Django Unittest遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020116/