【问题标题】:TransactionManagementError while running tests, but can't find atomic block运行测试时出现 TransactionManagementError,但找不到原子块
【发布时间】:2015-10-13 11:10:14
【问题描述】:

我在运行测试时遇到了一个问题,我一开始运行测试就会收到 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 参数进行此测试,以确保干净地重新创建数据库。

【问题讨论】:

  • Django 的TestCase 将每个测试包装在一个事务中。如果您不希望这样,请使用TransactionTestCase
  • 你能告诉我们你的测试和相关的源代码吗?
  • @knbk,-感谢您的关注,我最终解决了问题,并在答案中添加了详细信息。

标签: django unit-testing django-cms


【解决方案1】:

我通过回滚提交来跟踪更改,直到测试通过并比较更改的内容。事实证明,它正在添加一个保存后信号处理程序来自动创建一个链接模型,该模型是阻止测试通过的更改。我有一个 User 模型和一个带有 OneToOneField 指向 User 模型的 UserProfile 模型。

问题在于测试通常是手动创建用户配置文件。一旦添加了 post_save 处理程序,这将导致手动创建的 UserProfile 出现重复 id 的错误,因为自动创建的 UserProfile 已经使用了该 id。我猜这导致围绕单元测试的事务严重失败,导致所有其他错误。

但最初的错误隐藏在许多关于交易的错误的噪音中。

【讨论】:

  • 哦,伙计,非常感谢您的提示。我花了几天时间尝试修复失败的测试,但与最近创建的多对多字段没有任何可见的联系。
猜你喜欢
  • 2017-10-14
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多