我已经为我的应用程序创建了相当多的TestCases,到目前为止,一切都像预期的那样工作,最近当我试图运行任何TestCase时,我得到了这个错误信息(SimpleTestCase工作得很好,因为它不会创建任何DB对象,至少这是我的理论)。
示例:
# # pages/tests.py
from django.test import SimpleTestCase, TestCase
from non_voice.models import Product
class HomePageTests(SimpleTestCase):
def test_home_page_status_code(self):
response = self.client.get('/')
self.assertEquals(response.status_code, 200)
class ProductTests(TestCase):
def setUp(self):
Product.objects.create(product='test', product_name='test', header_text='test', language_code='DE')
def test_text_content(self):
product = Product.objects.get(id=1)
expected_object_name = f'{product.product}'
self.assertEquals(expected_object_name, 'test')输出:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/core/management/commands/test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/test/runner.py", line 684, in run_tests
old_config = self.setup_databases(aliases=databases)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/test/runner.py", line 606, in setup_databases
self.parallel, **kwargs
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/test/utils.py", line 156, in setup_databases
test_databases, mirrored_aliases = get_unique_databases_and_mirrors(aliases)
File "/EDR_ICT/.virtualenvs/WSx_Dev/lib/python3.6/site-packages/django/test/utils.py", line 272, in get_unique_databases_and_mirrors
(connection.settings_dict['NAME'], set())
TypeError: unhashable type: 'set'用于设备的DataBase设置(在正常操作中工作):
DATABASES = {
'default': {
'NAME': 'WSX_NV_AUT',
'ENGINE': 'sql_server.pyodbc',
'HOST': f'{db_server}, {db_port}', # MS SQL Driver needs the port in the hostname
'USER': db_user,
'PASSWORD': db_password,
'PORT': {db_port},
'OPTIONS': {
'driver': "ODBC Driver 17 for SQL Server",
}
}
}我没有改变我能想到的任何东西,DB驱动程序是一样的,django版本和库没有改变,等等?有人有什么想法吗?
发布于 2021-01-15 18:42:41
'PORT': {db_port}导致了这个错误,因为您的目标是创建一个只有一个键而没有值的集合/字典。这可能是使用f-string时的遗留问题或疏忽。
这将解决此错误:
DATABASES = {
'default': {
'NAME': 'WSX_NV_AUT',
'ENGINE': 'sql_server.pyodbc',
'HOST': f'{db_server}, {db_port}', # MS SQL Driver needs the port in the hostname
'USER': db_user,
'PASSWORD': db_password,
'PORT': db_port, # note the removed {}
'OPTIONS': {
'driver': "ODBC Driver 17 for SQL Server",
}
}
}https://stackoverflow.com/questions/65732215
复制相似问题