标签:python-2-7 nose python
我正在尝试使用鼻子实现集成测试框架.作为核心,我希望所有测试类都继承一个基类.我想要一个被称为的类安装函数以及每个测试安装函数.当我使用鼻子测试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(self)-> 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-2-7,nose,python 来源: https://codeday.me/bug/20191122/2057783.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。