鼻子测试,蟒蛇 [英] nosetests, python

查看:81
本文介绍了鼻子测试,蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习python,我遵循的指南要求我使用元组,列表和类编写一个简单的游戏".

I am trying to learn python, the guide I am following is asking me to write a simple 'game', utilizing tuples, lists, and classes.

运行"nosetests"命令时,出现以下错误:

When running the 'nosetests' command, I get the following error:

E.
====================================================================== ERROR:
tests.LEXICON_tests.test_directions
---------------------------------------------------------------------- 
Traceback (most recent call last):  
File "/Library/Python/2.6/site-packages/nose/case.py",
line 187, in runTest
   self.test(*self.arg)   
File "/Users/VB/Documents/svn/Programming/python/projects/lexicon/tests/LEXICON_tests.py",
line 6, in test_directions
   assert_equal(lexicon.scan("north"),
[('directions', 'north')]) TypeError:
unbound method scan() must be called
with lexicon instance as first
argument (got str instance instead)

---------------------------------------------------------------------- Ran 2 tests in 
 0.011s

 FAILED (errors=1) VB MP > ./lexicon.py

 > north [(), ('directions', 'north')] VB MP > ./lexicon.py 
 > north south east [[[(), ('directions', 'north')],
 ('directions', 'south')],
 ('directions', 'east')] VB MP 

主文件:

#!/usr/bin/env python
# encoding: utf-8

import sys
import os
from LEXICON.game import lexicon


def main():
    stuff = raw_input('> ') 

    lex = lexicon (stuff)

    split_array = lex.scan(stuff)
    print split_array

    #me = lex.tockens(split_array)
    #print me

if __name__ == '__main__':
    main()

class 
#!/usr/bin/env python
# encoding: utf-8
"""

import sys
import os

class lexicon (object):
    def __init__(self,data):
        self.direction = data
        #self.words = data.split()

    def scan(self,data):
        split_data = data.split()
        lex = lexicon (data)

        tocken = lex.tockens(split_data)


        return tocken

    def tockens(self,splitdata):

        sentence = splitdata
        verbs = ['go','swim','open','punch','fly','stop','kill','eat']
        objekts = ['door','bear','face','princess','cabinet']
        directions = ['north','south','east','west','down','up','left','right','back']
        stopwords = ['the','in','of','from','at','it']


        # setep 1, open the array

        bit = ()
        byte = ()
        for x in sentence:
            # step 2, match against verbs
            for v in verbs:
                try:
                    if (x == v):
                        bit = ('verb',x)
                        byte = [byte, bit]              
                except ValueError:
                    print "Ooops!"
            for o in objekts:
                try:
                    if (x == o):
                        bit = ('objekt',x)
                        byte = [byte, bit]
                except ValueError:
                    print "Ooops!"
            for d in directions:
                try:
                    if (x == d):
                        bit = ('directions',x)
                        byte = [byte, bit]
                except ValueError:
                    print "Ooops!"
            for s in stopwords:
                try:
                    if (x == s):
                        bit = ('stopwords',x)
                        byte = [byte, bit]
                except ValueError:
                    print "Ooops!"

        return byte

测试

from nose.tools import *
#import LEXICON
from LEXICON.game import lexicon

def test_directions(): 
    assert_equal(lexicon.scan("north"), [('directions', 'north')]) 
    result = lexicon.scan("north south east") 
    assert_equal(result, [('directions', 'north'),
                          ('directions', 'south'), 
                          ('directions', 'east')])

谢谢!

推荐答案

在python中调用方法的方式是,将调用它的对象作为第一个参数传递,并将所提供的参数分别下推1.您会像调用静态方法(lexicon.scan)而不是实例方法(lex.scan)一样调用此第一个参数.

The way methods are called in python is that the object it is called on is passed as the first argument, and the arguments supplied are each pushed down 1. When you call like its a static method (lexicon.scan) instead of an instance method (lex.scan) this first argument is not supplied.

方法lexicon.scan要求第一个参数是词典"类型的对象,因此您可能需要在测试中创建一个词典对象(lex = lexicon(stuff))并从该对象调用扫描().照原样,它正在呼叫scan("north"),而您希望呼叫是scan(lex, "north").

The method lexicon.scan requires the first argument be a "lexicon" type object, so what you probably want to do in your test is to create a lexicon object (lex = lexicon(stuff)) and call scan from that object (lex.scan("north")). As is, it is calling scan("north"), while you want the call to be scan(lex, "north").

这篇关于鼻子测试,蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆