继承鼻子测试的基类

我正在尝试使用鼻子实现集成测试框架.作为核心,我希望所有测试类都继承一个基类.我想要一个被称为的类安装函数以及每个测试安装函数.当我使用鼻子测试a_file.py -vs时,a_file.py看起来像这样:

    from nose import tools

    class BaseClass(object):
        def __init__(self):
            print 'Initialize Base Class'

        def setup(self):
            print "\nBase Setup"

        def teardown(self):
            print "Base Teardown"

        @tools.nottest
        def a_test(self):
            return 'This is a test.'

        @tools.nottest
        def another_test(self):
            return 'This is another test'

    class TestSomeStuff(BaseClass):
        def __init__(self):
            BaseClass.__init__(self)
            print 'Initialize Inherited Class'

        def setup(self):
            BaseClass.setup(self)
            print "Inherited Setup"

        def teardown(self):
            BaseClass.teardown(self)
            print 'Inherited Teardown'

        def test1(self):
            print self.a_test()

        def test2(self):
            print self.another_test()

输出此:

Initialize Base Class
Initialize Inherited Class
Initialize Base Class
Initialize Inherited Class
cases.nose.class_super.TestSomeStuff.test1 ... 
Base Setup
Inherited Setup
This is a test.
Base Teardown
Inherited Teardown
ok
cases.nose.class_super.TestSomeStuff.test2 ... 
Base Setup
Inherited Setup
This is another test
Base Teardown
Inherited Teardown
ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

如何使__init __,setup和teardown函数成为类方法?当我尝试这样做时:

from nose import tools

class BaseClass(object):
    def __init__(self):
        print 'Initialize Base Class'

    @classmethod
    def setup_class(self):
        print "\nBase Setup"

    @classmethod
    def teardown_class(self):
        print "Base Teardown"

    @tools.nottest
    def a_test(self):
        return 'This is a test.'

    @tools.nottest
    def another_test(self):
        return 'This is another test'

class TestSomeStuff(BaseClass):
    def __init__(self):
        BaseClass.__init__(self)
        print 'Initialize Inherited Class'

    @classmethod
    def setup_class(self):
        BaseClass.setup_class(self)
        print "Inherited Setup"

    @classmethod
    def teardown_class(self):
        BaseClass.teardown_class(self)
        print 'Inherited Teardown'

    def test1(self):
        print self.a_test()

    def test2(self):
        print self.another_test()

我得到这个:

Initialize Base Class
Initialize Inherited Class
Initialize Base Class
Initialize Inherited Class
ERROR

======================================================================
ERROR: test suite for <class 'cases.nose.class_super.TestSomeStuff'>
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/suite.py", line 208, in run
    self.setUp()
  File "/usr/lib/python2.7/dist-packages/nose/suite.py", line 291, in setUp
    self.setupContext(ancestor)
  File "/usr/lib/python2.7/dist-packages/nose/suite.py", line 314, in setupContext
    try_run(context, names)
  File "/usr/lib/python2.7/dist-packages/nose/util.py", line 478, in try_run
    return func()
  File "/home/ryan/project/python_testbed/cases/nose/class_super.py", line 30, in setup_class
    BaseClass.setup_class(self)
TypeError: setup_class() takes exactly 1 argument (2 given)

----------------------------------------------------------------------
Ran 0 tests in 0.001s

FAILED (errors=1)

从超类调用(BaseClass.setup_class(s​​elf)-> BaseClass.setup_class())中删除self似乎可以解决此问题…我不明白:

Initialize Base Class
Initialize Inherited Class
Initialize Base Class
Initialize Inherited Class

Base Setup
Inherited Setup
cases.nose.class_super.TestSomeStuff.test1 ... This is a test.
ok
cases.nose.class_super.TestSomeStuff.test2 ... This is another test
ok
Base Teardown
Inherited Teardown

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

但是,这对__init__函数没有帮助.如何使它成为类方法?为什么自我传递给超类失败?

有人对此有任何信息吗?

解决方法:

类方法采用一个隐式参数(按惯例称为cls,尽管您也将其称为self),就像实例方法采用self.

你打电话时

BaseClass.setup_class(self)

真的更像

BaseClass.setup_class(BaseClass, self)

因此,警告了两个论点.因此,当你放弃自我时,它是固定的.提醒一下,更改定义:

@classmethod
def setup_class(cls):

哦,__ init__作为@classmethod没有意义;这是用于设置实例.

上一篇:python-gae-sessions和鼻子错误


下一篇:python-ImportError:Travis-CI构建上没有名为config的模块