TransactionManagementError运行测试时,但无法找到原子块
问题描述:
我遇到了运行测试的问题,当我开始运行测试时,我收到TransactionManagementError。我已经尝试了各种不同的测试,他们都打这个错误: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
参数进行此测试,以确保数据库完全重新创建。
答
我通过回滚提交来追踪更改,直到测试通过并比较更改的内容。事实证明,它添加了一个后保存信号处理程序来自动创建一个链接模型,这是停止测试传递的变化。我有一个用户模型和一个UserProfile模型,其中OneToOneField指向用户模型。
问题是测试通常是手动创建UserProfile。一旦添加了post_save处理程序,这将导致手动创建的UserProfile的重复ID错误,因为自动创建的UserProfile已经使用了该ID。我想这会导致单元测试周围的事务失败,导致所有其他错误。
但是,原来的错误隐藏在关于交易的许多错误的噪音中。
Django的'TestCase'将每个测试包装在一个事务中。如果你不想这样,使用'TransactionTestCase'。 – knbk
你能告诉我们你的测试和相关的源代码吗? – Uri
@knbk, - 感谢您的期待,我最终解决了这个问题,并在答案中添加了详细信息。 –