我正在尝试使用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__
,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)
从超类调用中删除self
(BaseClass.setup_class(self)
- &gt; 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
传递给超类会失败?
有没有人对此有一些信息?
答案 0 :(得分:2)
类方法采用单个隐式参数(按惯例称为cls
,尽管您也称它为self
),例如实例方法采用self
。
致电时
BaseClass.setup_class(self)
这更像是
BaseClass.setup_class(BaseClass, self)
因此警告两个论点。因此,当你放弃self
时它就被修复了;提醒一下,更改定义:
@classmethod
def setup_class(cls):
哦,__init__
没有任何意义@classmethod
;它用于设置实例。
答案 1 :(得分:1)
看起来@Jonrsharpe已经击败了我,但是,是的。无论如何,我会发布它。
This one可能会解释setup_class()错误。当你包含self时,它传递的是BaseClass的实例以及'self'(或TestSomeStuff)。鼻子必须硬编码,以便在该功能中不允许超过1个参数(或单元测试,不确定哪个具有该要求)。
对于 init 部分,在阅读了Unittest文档后,似乎代码将更好地打印为:
def __init__(self):
print 'Initialize Base Class Object'
和
def __init__(self):
BaseClass.__init__(self)
print 'Initialize Inherited Class Object'
因为它基本上为每个测试用例创建一个对象并运行 init 函数以确保测试用例准备就绪。