Python 断言一个操作引发一个Stopiteration
我有一个生成器,我想确认它已经结束(在程序中的某一点上)。我正在使用python 2.7中的unittestPython 断言一个操作引发一个Stopiteration,python,unit-testing,python-2.7,python-unittest,Python,Unit Testing,Python 2.7,Python Unittest,我有一个生成器,我想确认它已经结束(在程序中的某一点上)。我正在使用python 2.7中的unittest # it is a generator whould have only one item item = it.next() # any further next() calls should raise StopIteration self.assertRaises(StopIteration, it.next()) 但它没有传达出这样的信息 ====================
# it is a generator whould have only one item
item = it.next()
# any further next() calls should raise StopIteration
self.assertRaises(StopIteration, it.next())
但它没有传达出这样的信息
======================================================================
ERROR: test_productspider_parse_method (__main__.TestMyMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/myName/tests/__main__.py", line 94, in test_my_method
self.assertRaises(StopIteration, it.next())
StopIteration
----------------------------------------------------------------------
你需要传递方法本身,而不是调用方法。换句话说,去掉括号
self.assertRaises(StopIteration, it.next)
也可以将其用作上下文管理器:
with self.assertRaises(StopIteration):
it.next()
@iCodez,你说得对。我正在编辑它。谢谢你的评论。