__init__ for unittest.TestCase
·
回答问题
我想为unittest.TestCase类在初始化时所做的事情添加几件事,但我不知道该怎么做。
现在我正在这样做:
#filename test.py
class TestingClass(unittest.TestCase):
def __init__(self):
self.gen_stubs()
def gen_stubs(self):
# Create a couple of tempfiles/dirs etc etc.
self.tempdir = tempfile.mkdtemp()
# more stuff here
我希望为整组测试只生成一次所有存根。我不能使用setUpClass()因为我正在使用 Python 2.4(我也无法在 python 2.7 上使用它)。
我在这里做错了什么?
我收到此错误:
`TypeError: __init__() takes 1 argument (2 given)`
...当我使用命令python -m unittest -v test运行它时将所有存根代码移动到__init__时出现其他错误。
Answers
尝试这个:
class TestingClass(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestingClass, self).__init__(*args, **kwargs)
self.gen_stubs()
您正在覆盖TestCase的__init__,因此您可能希望让基类为您处理参数。
更多推荐

所有评论(0)