【问题标题】:Nose test superclass calls and inherited classes鼻子测试超类调用和继承类
【发布时间】:2015-04-01 09:20:50
【问题描述】:

问题
如何通过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 和我要找的很相似,但它是用来模拟的!

编辑
所以我的鼻子测试会有这样的东西:

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)

【问题讨论】:

    标签: python inheritance nose


    【解决方案1】:

    这样做的一种方法是:

    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)
    

    希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多