Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 断言一个操作引发一个Stopiteration_Python_Unit Testing_Python 2.7_Python Unittest - Fatal编程技术网

Python 断言一个操作引发一个Stopiteration

Python 断言一个操作引发一个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()) 但它没有传达出这样的信息 ====================

我有一个生成器,我想确认它已经结束(在程序中的某一点上)。我正在使用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())
但它没有传达出这样的信息

======================================================================
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,你说得对。我正在编辑它。谢谢你的评论。