Python __init__ 用于 unittest.TestCase

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17353213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:59:28  来源:igfitidea点击:

__init__ for unittest.TestCase

pythonunit-testing

提问by ffledgling

I'd like to add a couple of things to what the unittest.TestCaseclass does upon being initialized but I can't figure out how to do it.

我想为unittest.TestCase类在初始化时所做的事情添加一些东西,但我不知道如何去做。

Right now I'm doing this:

现在我正在这样做:

#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

I'd like all the stubs to be generated only once for this entire set of tests. I can't use setUpClass()because I'm working on Python 2.4 (I haven't been able to get that working on python 2.7 either).

我希望为整个测试集只生成一次所有存根。我无法使用,setUpClass()因为我正在使用Python 2.4(我也无法在 Python 2.7 上使用它)。

What am I doing wrong here?

我在这里做错了什么?

I get this error:

我收到此错误:

 `TypeError: __init__() takes 1 argument (2 given)` 

...and other errors when I move all of the stub code into __init__when I run it with the command python -m unittest -v test.

...和其他错误,当我__init__使用命令运行它时将所有存根代码移入python -m unittest -v test

采纳答案by karthikr

Try this:

尝试这个:

class TestingClass(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        super(TestingClass, self).__init__(*args, **kwargs)
        self.gen_stubs()

You are overriding the TestCase's __init__, so you might want to let the base class handle the arguments for you.

您正在覆盖TestCase's __init__,因此您可能希望让基类为您处理参数。

回答by fabrizioM

Install unittest2 and use that package's unittest.

安装 unittest2 并使用该包的 unittest。

import unittest2 

and then use the setupModule / tearDownModule or setupClass / tearDown class for special initialization logic

然后使用 setupModule/tearDownModule 或 setupClass/tearDown 类进行特殊的初始化逻辑

More info: http://www.voidspace.org.uk/python/articles/unittest2.shtml

更多信息:http: //www.voidspace.org.uk/python/articles/unittest2.shtml

Also most likely your are creating an integration test more than an unittest. Choose a good name for the Tests to differentiate them or put in a different container module.

也很可能您正在创建一个集成测试而不是单元测试。为测试选择一个好名字以区分它们或放入不同的容器模块中。

回答by Thomas Vetterli

Just wanted to add some clarifications about overriding the init function of

只是想添加一些关于覆盖 init 函数的说明

unittest.TestCase

The function will be called before each method in your test class. Please note that if you want to add some expensive computations that should be performed oncebefore running all test methods please use the SetUpClassclassmethod

该函数将在测试类中的每个方法之前调用。请注意,如果您想添加一些在运行所有测试方法之前应该执行一次的昂贵计算,请使用SetUpClass类方法

@classmethod
def setUpClass(cls):
    cls.attribute1 = some_expensive_computation()

This function will be called oncebefore all test methods of the class. See setUpfor a method that is called before each test method.

该函数将在类的所有测试方法之前调用一次。请参阅setUp在每个测试方法之前调用的方法。