鼻子测试超类调用和继承的类

时间:2015-04-01 09:20:50

标签: python inheritance nose

问题
我如何通过super()调用测试从另一个超类继承的类及其方法?

示例

class A():
    def __init__(self, x, y):
        # do something with x and y

    def aMethod(self, param1):
        # do stuff with param1

class B(A):
    def bMethod1(self, param2):
        # do stuff with self and param2
        super(B, self).aMethod(param2)

    def bMethod2(self, myDict):
        # do stuff with myDict
        return myDict

如果我有class A(Z)(继承自class Z())然后class B(A),那会不会一样?

answer与我正在寻找的非常相似,但它适用于Mocking!

修改
所以我的鼻子测试会有这样的东西:

class Test_B(Object):
    # All of the setup and teardown defs go here...
    def test_bMethod2(self):
        b = B()
        dict = { 'a' : '1', 'b' : '2' } # and so on...
        assert_equal(b.bMethod2(dict), dict)

1 个答案:

答案 0 :(得分:0)

这样做的方法是:

class Test_B(Object):
# All of the setup and teardown defs go here...
def test_bMethod2(self):
    a, b, param = "",
    dict = { 'a' : '1', 'b' : '2' }
    # Instantiating B with its super params
    klass = B(a, b)
    # Calling class B's bMethod1() with required params 
    # to pass onto its super class' method (here: aMethod())
    klass.bMethod1(param)
    assert_equal(klass.bMethod2(dict), dict)

希望这有助于其他人。