Python 解决通道/异步测试用例上的django测试运行时错误
我正试图为我的django(3.2)通道消费者编写一个测试用例,以实现websocket通信。我已经设法让testcase通过,但现在看来,在尝试清理测试数据库时,testcase拆卸失败了Python 解决通道/异步测试用例上的django测试运行时错误,python,testing,async-await,django-3.2,Python,Testing,Async Await,Django 3.2,我正试图为我的django(3.2)通道消费者编写一个测试用例,以实现websocket通信。我已经设法让testcase通过,但现在看来,在尝试清理测试数据库时,testcase拆卸失败了 System check identified no issues (0 silenced). . ---------------------------------------------------------------------- Ran 1 test in 0.130s OK Destroyi
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.130s
OK
Destroying test database for alias 'default'...
...
psycopg2.errors.ObjectInUse: database "test_myproject" is being accessed by other users
DETAIL: There is 1 other session using the database.
...
File ".../python3.8/site-packages/django/core/management/commands/test.py", line 55, in handle
failures = test_runner.run_tests(test_labels)
File ".../python3.8/site-packages/django/test/runner.py", line 736, in run_tests
self.teardown_databases(old_config)
File ".../python3.8/site-packages/django/test/runner.py", line 674, in teardown_databases
_teardown_databases(
File ".../python3.8/site-packages/django/test/utils.py", line 313, in teardown_databases
connection.creation.destroy_test_db(old_name, verbosity, keepdb)
File ".../python3.8/site-packages/django/db/backends/base/creation.py", line 282, in destroy_test_db
self._destroy_test_db(test_database_name, verbosity)
File ".../python3.8/site-packages/django/db/backends/base/creation.py", line 298, in _destroy_test_db
cursor.execute("DROP DATABASE %s"
File ".../python3.8/contextlib.py", line 162, in __exit__
raise RuntimeError("generator didn't stop after throw()")
RuntimeError: generator didn't stop after throw()
消费者是使用async编写的,似乎我在async如何与测试用例集成方面遇到了问题。为了让测试用例正常运行,我还需要做什么
我创建并配置了一个fixture将数据加载到数据库中,并怀疑这与那里的testcase db交互有关
我尝试用标准的django“TestCase”代替“TransactionTestCase”,但显然配置的fixture没有按预期加载到db中
类ConsumerTestCase(TransactionTestCase):
固定装置=(“基本数据”)
异步def测试\警报消费者\接收\有效(自):
#在夹具中定义
username=“测试用户名”
警报\u id=1
url=“/alerts/ws/”
communicator=WebsocketCommunicator(AlertConsumer.as_asgi(),url)
已连接,subtocol=wait communicator.connect()
断言连接
message={“username”:“me”,“alert_id”:1}
等待communicator.send_to(text_data=json.dumps(message))
响应=等待通讯器。从()接收
预期={
“消息”:“已接受”
}
self.assertDictEqual(实际、预期)