pytest:classmethods和boundmethods

时间:2015-12-17 12:58:31

标签: python oop pytest

所以我正在使用pytest,一切正常,但有些事困扰我。当我在我的TestClass定义中添加一些方法时,它默认是一个绑定方法,但我实际上从来没有使用过“self”。

现在,“setup_class”(或teardown_class)方法必然是classmethods,因此我无法从setup_class调用绑定方法。

那么我将每个方法定义为classmethods并与之相处,或者我错过了什么?我应该简单地从不使用绑定方法吗?

class TestSomething(object):

    @classmethod
    def setup_class(cls):
        "do something"
        cls.bound_method(?) #how do I call it ?


    def bound_method(self):
        "do something I'd like to do in setup_class"

1 个答案:

答案 0 :(得分:0)

您可以使用方法setup and teardown methods in a class

def setup_method(self, method):
    """ setup any state tied to the execution of the given method in a
    class.  setup_method is invoked for every test method of a class.
    """

def teardown_method(self, method):
    """ teardown any state that was previously setup with a setup_method
    call.
    """

在类

中定义的每个测试方法(绑定测试函数)上运行