我正在编写一些涉及 ZODB 的测试,但由于我在某些单元测试中遇到的错误而被卡住了很长时间。让我们称之为Test_B
:
Failure/Error: 'NoneType' object has no attribute 'sortKey'
...
File <<< my code somewhere >>>
transaction.commit()
File "/usr/local/lib/python2.7/site-packages/transaction/_manager.py", line 111, in commit
return self.get().commit()
File "/usr/local/lib/python2.7/site-packages/transaction/_transaction.py", line 280, in commit
reraise(t, v, tb)
File "/usr/local/lib/python2.7/site-packages/transaction/_transaction.py", line 271, in commit
self._commitResources()
File "/usr/local/lib/python2.7/site-packages/transaction/_transaction.py", line 386, in _commitResources
L.sort(key=rm_key)
File "/usr/local/lib/python2.7/site-packages/transaction/_transaction.py", line 555, in rm_key
return func()
File "/usr/local/lib/python2.7/site-packages/ZODB/Connection.py", line 813, in sortKey
return "%s:%s" % (self._storage.sortKey(), id(self))
值得庆幸的是,我最终发现我忘记在调用transaction.commit()
之前运行的测试中调用 a Test_B
(不出所料)Test_A
。日志记录的事件序列如下所示:
<<< Test_A begins >>>
23:01:41 DEBUG txn.140735119446400: new transaction
...
<<< no further mentions of txn.140735119446400 being committed or aborted >>>
<<< Test_A ends >>>
<<< Test_B begins >>>
23:01:41 DEBUG txn.140735119446400: new transaction
23:01:41 DEBUG my_spec: *** MANUALLY altered DB in Test_B body
...
<<< Test_B bails out due to error >>>
注意Test A
成功:这是错误的!我想Test A
失败并告诉我我忘记提交的事务中有未完成的更改。
我怎样才能用 ZODB 做到这一点?我在文档中找不到任何可以用来确定我是否在进行更改的交易的内容。
显然,通过该检查,我可以将其粘贴到AfterEach
套件中所有单元测试的块中。