python - 继承 Nose 测试的基类

标签 python python-2.7 nose

我正在尝试使用 nose 实现集成测试框架。在核心,我想要一个所有测试类都继承的基类。我想要一个被调用的类设置函数以及每个测试设置函数。当我使用 nosetests 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__setupteardown 函数类方法?当我尝试这样做时:

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)

从父类(super class)调用中删除 self (BaseClass.setup_class(self) -> BaseClass.setup_class()) 似乎可以解决它...我不明白:

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__ 函数没有帮助。我怎样才能使它成为一个类方法?为什么将 self 传递给父类(super class)会失败?

有人知道这方面的信息吗?

最佳答案

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

当你打电话时

BaseClass.setup_class(self)

真的更像

BaseClass.setup_class(BaseClass, self)

因此对两个参数发出警告。因此,当您放弃 self 时它已修复;提醒一下,更改定义:

@classmethod
def setup_class(cls):

哦,__init__ 作为@classmethod 毫无意义;它用于设置实例。

关于python - 继承 Nose 测试的基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21392720/

相关文章:

python - 使用 Opencv 评估 tensorflow 模型失败

python - 如何在南迁移期间调用 django 模型类的静态方法

python - 多 GPU/Tower 设置 Tensorflow 1.2 Estimator

python - 如何在 Python 2.7 中同时优化和查找两个方程的系数?

python - scikit-learn 模型持久性 : pickle vs pmml vs . ..?

python - 使用装饰器进行重构以减少代码量

python - 属性错误 : 'module' object has no attribute 'model'

python - python 中的振荡积分

python - 使用nosetest时cfg文件中的插件出错

python - 对 AppEngine 进行单元测试以发现故障