Python LPTHW:Ex48单元测试(使用nosetests)、类、方法参数TypeErrors
我是LPTHW的新手。在ex48中,我得到了很多作为单元测试的代码,我应该为它们编写相应的my_代码,这些代码将被测试(使用NoTests,抱歉,这句话中有很多测试) 这是我的密码:Python LPTHW:Ex48单元测试(使用nosetests)、类、方法参数TypeErrors,python,Python,我是LPTHW的新手。在ex48中,我得到了很多作为单元测试的代码,我应该为它们编写相应的my_代码,这些代码将被测试(使用NoTests,抱歉,这句话中有很多测试) 这是我的密码: class lexicon(object): def __init__(self): #some initial stuff. def scan(self,stuff): #some cool code. 这是test.py文件 from nose.tools import*
class lexicon(object):
def __init__(self):
#some initial stuff.
def scan(self,stuff):
#some cool code.
这是test.py文件
from nose.tools import*
from Ex48.code import lexicon
def test_directions():
assert_equal(lexicon.scan("north"), [('direction', 'north')])
...#Unimportant details the above is enough.
但是,当我运行nosetests时,会出现以下错误:
Traceback (most recent call last):
File "c:\python31\lib\site-packages\nose'case.py", line 197, i runTest
... line x, in test_directions
assert_equal(lexicon.scan("north"), [('direction', 'north')])
TypeError: scan() takes exactly 2 positional arguments (1 given)
这很奇怪,因为这两个论点中有一个是自我论点,我认为不应该给出
为了解决这个问题,我尝试了:
@staticmethod
def scan(self,stuff):
但这给了我同样的打字错误
接下来我试着:
assert_equal(lexicon.__init__(self).scan(...)
但这也不起作用(我认为。init(self)应该只在另一个类中使用,而不是在main中使用)
我最终决定:
lexicon1=lexicon()
def test_directions():
assert_equal(lexicon1.scan("north"), [('direction', 'north')])
但这不是该怎么做的
附言:我没有包括整个回溯,因为它可能没有必要(而且已经很晚了)。我没有包括我的代码bc的一部分,我确信它工作正常,不会导致错误 我认为你做这件事的方式(创建一个实例,然后使用它的方法——你的lexicon1示例)很好,对吗 如果您想用第一种方法,您是否尝试过:
@classmethod
def scan(self, stuff):
我也是一个新手(刚刚完成练习49),所以也许其他人可以回答得更好。不过,很好奇@classmethod decorator是否符合您的要求。是的!这实际上已经奏效了,我不需要创建实例!但是显然@classmethod忽略了def\uuu init\uuuuu(self)中的所有内容(如所有属性、dicts列表等):所以我不得不将它们复制粘贴到我的扫描函数定义中。我不知道!我想我更喜欢创建一个实例。但是@Steve Koch你知道我是否应该用cls替换扫描中的自变量,然后在该方法的其余部分这样做吗?对不起,这是我迄今为止的知识范围:)谢谢你尝试并发布你的结果!