Python 循环中的assertRaises:每个迭代1个测试
我试图在循环中使用Python 循环中的assertRaises:每个迭代1个测试,python,unit-testing,testing,nose,Python,Unit Testing,Testing,Nose,我试图在循环中使用assertRaises,这样我就可以测试多个错误的分隔符([',':','-']),而不必为每个案例编写新的测试。在循环中使用assertRaises时遇到问题。我制作了一个unittest.TestCase的最小工作示例,它在循环中调用assertRaises: import sys import unittest def throw_error(): sys.stderr.write('In throw error!') raise TypeError c
assertRaises
,这样我就可以测试多个错误的分隔符([',':','-']
),而不必为每个案例编写新的测试。在循环中使用assertRaises
时遇到问题。我制作了一个unittest.TestCase
的最小工作示例,它在循环中调用assertRaises
:
import sys
import unittest
def throw_error():
sys.stderr.write('In throw error!')
raise TypeError
class Test(unittest.TestCase):
def test_loop(self):
for i in range(5):
self.assertRaises(TypeError, throw_error)
这是可行的,但它只算作1个测试,而我更希望它被理解为5个测试。有没有一种规范的方法来获得这种行为
>>> nosetests ../footest.py
In throw error!
In throw error!
In throw error!
In throw error!
In throw error!
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
我想要这种行为的主要原因是因为这看起来太像一个黑匣子了,我最终会忘记这是1中的5个测试。也许像我用自定义消息那样给stderr写东西已经足够好了,或者你有更好的建议吗
Falsetru的回答很好,但我无法将其适应我的情况
Falsetru的答案可以作为一个独立的示例使用,但是我的test_loop()函数需要是TestCase的一个实例方法,因为它需要处理许多属性和方法。因此,当我将他的答案调整为仍然使用TestCase时,它不再有效:
import sys
import unittest
import nose.tools as ntools
def throw_error():
sys.stderr.write('In throw error!')
raise TypeError
class Test(unittest.TestCase):
def test_loop(self):
for i in range(5):
yield ntools.assert_raises, TypeError, throw_error
这将产生以下结果:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
意思是永远不会调用
throw\u error()
。您使用的nose支持:
您使用的nose支持:
这是使用unittest.TestCase的方法:
import sys
import unittest
from nose.tools import istest
def throw_error():
sys.stderr.write('In throw error!')
raise TypeError
class Test(unittest.TestCase):
pass
def _create():
""" Helper method to make functions on the fly """
@istest
def func_name(self):
self.assertRaises(TypeError, throw_error)
return func_name
def populate(cls, tests):
""" Helper method that injects tests to the TestCase class """
for index, problem in enumerate(tests):
test_method_name = '_'.join(['test', str(index)])
_method = _create()
_method.__name__ = test_method_name
_method.__doc__ = test_method_name
setattr(cls, _method.__name__, _method)
tests = range(5)
populate(Test, tests)
以下是输出:
$ nosetests -v
test_0 ... In throw error!ok
test_1 ... In throw error!ok
test_2 ... In throw error!ok
test_3 ... In throw error!ok
test_4 ... In throw error!ok
----------------------------------------------------------------------
Ran 5 tests in 0.031s
OK
这是使用unittest.TestCase的方法:
import sys
import unittest
from nose.tools import istest
def throw_error():
sys.stderr.write('In throw error!')
raise TypeError
class Test(unittest.TestCase):
pass
def _create():
""" Helper method to make functions on the fly """
@istest
def func_name(self):
self.assertRaises(TypeError, throw_error)
return func_name
def populate(cls, tests):
""" Helper method that injects tests to the TestCase class """
for index, problem in enumerate(tests):
test_method_name = '_'.join(['test', str(index)])
_method = _create()
_method.__name__ = test_method_name
_method.__doc__ = test_method_name
setattr(cls, _method.__name__, _method)
tests = range(5)
populate(Test, tests)
以下是输出:
$ nosetests -v
test_0 ... In throw error!ok
test_1 ... In throw error!ok
test_2 ... In throw error!ok
test_3 ... In throw error!ok
test_4 ... In throw error!ok
----------------------------------------------------------------------
Ran 5 tests in 0.031s
OK
听起来你想做的事情像:听起来你想做的事情像:谢谢。我试过这个,但语法不正确。我没有意识到我可以把所有这些放在屈服之后,当我将test_loop()作为TestCase的实例方法时,这是失败的。只是不返回任何函数。我更新了我的问题以反映这一点。有什么建议吗?@AdamHughes,测试生成器不支持基于
unittest.TestCase
的类。但您仍然可以使用非基于unittest.TestCase的类。试试这个:谢谢。我试过这个,但语法不正确。我没有意识到我可以把所有这些放在屈服之后,当我将test_loop()作为TestCase的实例方法时,这是失败的。只是不返回任何函数。我更新了我的问题以反映这一点。有什么建议吗?@AdamHughes,测试生成器不支持基于unittest.TestCase
的类。但您仍然可以使用非基于unittest.TestCase的类。试试这个: