运行 测试时出现 TransactionManagementError,但找不到原子块
TransactionManagementError while running tests, but can't find atomic block
我在 运行 测试中遇到了一个问题,我一开始 运行 测试就收到 TransactionManagementError。我尝试了各种不同的测试,它们都遇到了这个错误:
.ve/lib/python2.7/site-packages/django/test/testcases.py:189: in __call__
self._post_teardown()
.ve/lib/python2.7/site-packages/cms/test_utils/testcases.py:97: in _post_teardown
menu_pool.clear()
.ve/lib/python2.7/site-packages/menus/menu_pool.py:156: in clear
if to_be_deleted:
.ve/lib/python2.7/site-packages/django/db/models/query.py:145: in __nonzero__
self._fetch_all()
.ve/lib/python2.7/site-packages/django/db/models/query.py:966: in _fetch_all
self._result_cache = list(self.iterator())
.ve/lib/python2.7/site-packages/django/db/models/query.py:1202: in iterator
for row in self.query.get_compiler(self.db).results_iter():
.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py:701: in results_iter
for rows in self.execute_sql(MULTI):
.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py:787: in execute_sql
cursor.execute(sql, params)
.ve/lib/python2.7/site-packages/django/db/backends/utils.py:59: in execute
self.db.validate_no_broken_transaction()
.ve/lib/python2.7/site-packages/django/db/backends/__init__.py:386: in validate_no_broken_transaction
"An error occurred in the current transaction. You can't "
E TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
你可以看到堆栈跟踪在 _post_teardown
方法中,所以我猜问题出在这个测试之前的某个地方。我查看了堆栈跟踪每一行的代码,但看不到代码的那些位正在启动事务,所以它一定发生在此处之前,但我如何找出位置?
如果我单独测试应用程序 A 或单独测试应用程序 B,我不确定如何追踪有问题的代码。欢迎任何建议。
我正在使用 Django 1.7、Django CMS 3.1(在上面的堆栈跟踪中包含菜单应用程序)和 pytest 作为测试运行程序。我正在使用 --create-db
参数进行此测试,以确保干净地重新创建数据库。
我通过回滚提交直到测试通过并比较更改的内容来跟踪更改。事实证明,它正在添加一个 post 保存信号处理程序来自动创建一个链接模型,该模型是停止测试通过的更改。我有一个 User 模型和一个 UserProfile 模型,其中 OneToOneField 指向 User 模型。
问题在于测试通常是手动创建 UserProfile。添加 post_save 处理程序后,将导致手动创建的 UserProfile 出现重复 ID 错误,因为自动创建的 UserProfile 已经使用了该 ID。我猜这导致围绕单元测试的事务严重失败,导致所有其他错误。
但最初的错误隐藏在有关交易的许多错误的噪音中。
我在 运行 测试中遇到了一个问题,我一开始 运行 测试就收到 TransactionManagementError。我尝试了各种不同的测试,它们都遇到了这个错误:
.ve/lib/python2.7/site-packages/django/test/testcases.py:189: in __call__
self._post_teardown()
.ve/lib/python2.7/site-packages/cms/test_utils/testcases.py:97: in _post_teardown
menu_pool.clear()
.ve/lib/python2.7/site-packages/menus/menu_pool.py:156: in clear
if to_be_deleted:
.ve/lib/python2.7/site-packages/django/db/models/query.py:145: in __nonzero__
self._fetch_all()
.ve/lib/python2.7/site-packages/django/db/models/query.py:966: in _fetch_all
self._result_cache = list(self.iterator())
.ve/lib/python2.7/site-packages/django/db/models/query.py:1202: in iterator
for row in self.query.get_compiler(self.db).results_iter():
.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py:701: in results_iter
for rows in self.execute_sql(MULTI):
.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py:787: in execute_sql
cursor.execute(sql, params)
.ve/lib/python2.7/site-packages/django/db/backends/utils.py:59: in execute
self.db.validate_no_broken_transaction()
.ve/lib/python2.7/site-packages/django/db/backends/__init__.py:386: in validate_no_broken_transaction
"An error occurred in the current transaction. You can't "
E TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
你可以看到堆栈跟踪在 _post_teardown
方法中,所以我猜问题出在这个测试之前的某个地方。我查看了堆栈跟踪每一行的代码,但看不到代码的那些位正在启动事务,所以它一定发生在此处之前,但我如何找出位置?
如果我单独测试应用程序 A 或单独测试应用程序 B,我不确定如何追踪有问题的代码。欢迎任何建议。
我正在使用 Django 1.7、Django CMS 3.1(在上面的堆栈跟踪中包含菜单应用程序)和 pytest 作为测试运行程序。我正在使用 --create-db
参数进行此测试,以确保干净地重新创建数据库。
我通过回滚提交直到测试通过并比较更改的内容来跟踪更改。事实证明,它正在添加一个 post 保存信号处理程序来自动创建一个链接模型,该模型是停止测试通过的更改。我有一个 User 模型和一个 UserProfile 模型,其中 OneToOneField 指向 User 模型。
问题在于测试通常是手动创建 UserProfile。添加 post_save 处理程序后,将导致手动创建的 UserProfile 出现重复 ID 错误,因为自动创建的 UserProfile 已经使用了该 ID。我猜这导致围绕单元测试的事务严重失败,导致所有其他错误。
但最初的错误隐藏在有关交易的许多错误的噪音中。