Python unittest - setUpClass() 给我带来了麻烦 - 为什么我不能像这样继承?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14044474/
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-18 10:15:56  来源:igfitidea点击:

Python unittest - setUpClass() is giving me trouble - why can't I inherit like this?

pythonunit-testing

提问by jononomo

I have unittest code like the following:

我有如下的单元测试代码:

import unittest

class MyUnitTest(unittest.TestCase):
    def setUpClass(self):
        do_something_expensive_for_all_sets_of_tests()

class MyFirstSetOfTests(MyUnitTest):
    def setUpClass(self):
        super(MyFirstSetOfTests, self).setUpClass()
        do_something_expensive_for_just_these_first_tests()

    def test_one(self):
        ...

    def test_two(self):
        ...

class MySecondSetOfTests(MyUnitTest):
    def setUpClass(self):
        super(MySecondSetOfTests, self).setUpClass()
        do_something_expensive_for_just_these_second_tests()

    def test_one(self):
        ...

    def test_two(self):
        ...

if __name__ == '__main__':
    unittest.main()

When I try to run this code, I get an error like this:

当我尝试运行此代码时,出现如下错误:

======================================================================
ERROR: setUpClass (__main__.MyFirstSetOfTests)
----------------------------------------------------------------------
TypeError: unbound method setUpClass() must be called with MyFirstSetOfTests instance as first argument (got nothing instead)

----------------------------------------------------------------------

采纳答案by Martijn Pieters

setUpClassmustbe a class method. From the documentation:

setUpClass必须是类方法。从文档

A class method called before tests in an individual class run. setUpClassis called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def setUpClass(cls):
    ...

See Class and Module Fixturesfor more details.

在单个类中运行测试之前调用的类方法。setUpClass使用类作为唯一参数调用,并且必须装饰为classmethod()

@classmethod
def setUpClass(cls):
    ...

有关更多详细信息,请参阅类和模块装置

Your version is missing the @classmethoddecorator:

您的版本缺少@classmethod装饰器

class MyUnitTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        do_something_expensive_for_all_sets_of_tests()

class MyFirstSetOfTests(MyUnitTest):
    @classmethod
    def setUpClass(cls):
        super(MyFirstSetOfTests, cls).setUpClass()
        do_something_expensive_for_just_these_first_tests()

The error is thrown because MyFirstSetOfTests.setUpClass()is called on the class, not on an instance, but you didn't mark your method as a classmethodand thus it was not passed in the automatic selfargument. In the above updated code I used clsinstead, to reflect that the name references the class object.

抛出错误是因为MyFirstSetOfTests.setUpClass()class上调用,而不是在实例上调用,但是您没有将方法标记为 a classmethod,因此它没有在自动self参数中传递。在上面我使用的更新代码中cls,以反映名称引用了类对象。