python测试我的方法

时间:2012-11-09 04:00:20

标签: python testing

嘿伙计们,我有一个关于测试我的方法的快速问题。我是上周新的python,但我正在尝试。我的任务是制作一种采用卡片列表并以某种方式对其进行排序的方法。我知道所有的检查和if语句和这样的工作,因为我在python shell中测试它们,但我想如果我将所有代码放入shell中它将超出索引(正确的缩进)。所以,如果你们可以帮助我,我只是不确定如何实现我的方法。

这是我的方法:

def play(list):

    for k in list:
        a,x,z = 0
        #insert into the pile
        pile = []
        pile = pile + [[list[k]]]

        #check if the new card in the pile is compatible to the card 3 to the left

        #check in terms if the first char of each card is compatible 
        if( (((pile[a])[(len(pile[a]))-1])[0] ) == ((((pile[a-3])[(len(pile[a-3]))-1])[0])) ):
            #add card to the top of the card 3 to left
            pile[a-3] = pile[a-3] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]

        #check in terms if the last char of each card is compatible 
        elif((((pile[a])[(len(pile[a]))-1])[(len((((pile[a])[(len(pile[a]))-1]))))-1]) == (((pile[a-3])[(len(pile[a-3]))-1])[(len((((pile[a-3])[(len(pile[a-3]))-1]))))-1])):
            #add card to the top of the card 3 to left
            pile[a-3] = pile[a-3] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]

        #check if the new card in the pile is compatible to the card to the left 

        #check in terms if the first char of each card is compatible 
        elif((((pile[a])[(len(pile[a]))-1])[0] ) == ((((pile[a-1])[(len(pile[a-1]))-1])[0]))):
            pile[a-1] = pile[a-1] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]

        #check in terms if the last char of each card is compatible 
        elif( (((pile[a])[(len(pile[a]))-1])[(len((((pile[a])[(len(pile[a]))-1]))))-1]) == (((pile[a-1])[(len(pile[a-1]))-1])[(len((((pile[a-1])[(len(pile[a-1]))-1]))))-1])):
            pile[a-1] = pile[a-1] + [((pile[a])[(len(pile[a]))-1])]
            if (len(pile[a])) == 1:
                del pile[a]
            else:
                del (pile[a])[-1]

        else:
            a = a + 1  

        #now go through and look for additional moves 
        #(if any match the 1 to the left or 3rd)

        while x < (len(pile)):
            if((((pile[x])[(len(pile[x]))-1])[0] ) == ((((pile[x-3])[(len(pile[x-3]))-1])[0]))):
                pile[x-3] = pile[x-3] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[x])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                    del pile[(len(pile))-1]
                else:
                    del (pile[x])[-1]
                x = x + 1

            elif((((pile[x])[(len(pile[x]))-1])[(len((((pile[x])[(len(pile[x]))-1]))))-1]) == (((pile[x-3])[(len(pile[x-3]))-1])[(len((((pile[x-3])[(len(pile[x-3]))-1]))))-1])):
                pile[x-3] = pile[x-3] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[a])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                else:
                    del (pile[a])[-1]        
                x = x + 1

            elif((((pile[x])[(len(pile[x]))-1])[0] ) == ((((pile[x-1])[(len(pile[x-1]))-1])[0]))):
                pile[x-1] = pile[x-1] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[x])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                    del pile[(len(pile))-1]
                else:
                    del (pile[x])[-1]
                x = x + 1

            elif( (((pile[x])[(len(pile[x]))-1])[(len((((pile[x])[(len(pile[x]))-1]))))-1]) == (((pile[x-1])[(len(pile[x-1]))-1])[(len((((pile[x-1])[(len(pile[x-1]))-1]))))-1])):
                pile[x-1] = pile[x-1] + [((pile[x])[(len(pile[x]))-1])]
                if (len(pile[a])) == 1:
                    del (pile[x])[-1]
                    z = x + 1
                    while z < (len(pile)):
                        pile[z-1] = pile[z]
                        z = z + 1
                else:
                    del (pile[x])[-1]
                x = x + 1

            else:
                x = x + 1

        #end while loop
    #end of for loop
    return pile

这就像老师给我的那样,就像测试案例一样:

import unittest
import solitaire

class Test(unittest.TestCase):

def test1(self):
    actual   = solitaire.play(['AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC',
                               'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
                               'AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
                               'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS'] )
    expected = [['AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS','KH','QH','JH','TH','9H','8H','7H','6H','5H','4H','3H','2H','AH','AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD','KC','QC','JC','TC','9C','8C','7C','6C','5C','4C','3C','2C','AC']]
    self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
    self.assertListEqual(     expected,      actual,  'Incorrect result' )
    pass

def test2(self):
    actual   = solitaire.play(['QS','5D','4S','8D','8H','3C','3H','5C','9H','6S','QD','2S','2C',
                               'KS','QC','7C','JC','4H','3D','5S','6C','KD','JS','9C','TS','2D',
                               '4D','AS','AC','7H','TC','AH','KH','6D','4C','8C','TD','AD','8S',
                               '3S','JH','KC','QH','TH','6H','7S','5H','2H','9S','7D','JD','9D'] )
    expected = [['8D','8H','3H','3C','JC','7C','QC','QD','4D','4S','6S','KS','KD','5D','5C','2C','2S','QS'],
                ['9C','4C','4H','9H'],
                ['6D','2D','3D'],
                ['JS','TS','TC','6C','AC','AS','5S'],
                ['KH','AH','7H'],
                ['8C'],
                ['AD','TD'],
                ['3S','8S'],
                ['6H','TH','JH'],
                ['KC'],
                ['2H','QH'],
                ['9D','JD','7D','7S'],
                ['5H'],
                ['9S']]
    self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
    self.assertListEqual(     expected,      actual,  'Incorrect result' )
    pass

def test3(self):
    actual   = solitaire.play(['5S','9H','QH','2H','8H','6H','3S','3H','AS','5H','JH','3C','KH',
                               '4H','6S','8S','8D','2D','JD','AH','KC','TD','TC','3D','6D','2C',
                               'AD','QS','9C','7C','6C','QD','JS','7H','7S','7D','KD','TS','5D',
                               'KS','9S','9D','4C','5C','8C','QC','JC','TH','4S','4D','2S','AC'] )
    expected = [['4D','5D','5C','8C','QC','JC','4C','4S','TS','AS','AH','4H','5H','7H','7S','JS','QS','QD','AD','6D','3D','TD','JD','2D','8D','8S','6S','KS','KD','7D','9D','9C','7C','6C','2C','TC','3C','KC','KH','JH','9H','QH','2H','8H','6H','3H','3S','5S'],
                ['9S'],
                ['TH'],
                ['2S'],
                ['AC']]
    self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
    self.assertListEqual(     expected,      actual,  'Incorrect result' )
    pass

def test4(self):
    actual   = solitaire.play(['7C','9S','QC','4H','9D','3S','AD','9C','8H','AC','2D','9H','AH',
                               '5D','6C','QD','3H','TC','QS','2S','8D','7D','QH','6H','4C','3C',
                               'JS','JD','7H','TS','4S','TD','5H','KD','8C','KS','JC','6D','2H',
                               '5C','3D','KH','8S','JH','TH','KC','2C','5S','AS','4D','7S','6S'] )
    expected = [['3S','9S','9D','AD','AC','7C'],
                ['QD','8D','7D','2D','5D','5H','8H','4H','9H','9C','QC'],
                ['AH'],
                ['TC','6C'],
                ['6H','3H'],
                ['JD','JS','2S','QS'],
                ['QH'],
                ['5C','JC','8C','3C','4C'],
                ['7H'],
                ['6D','TD','KD','KS','4S','TS'],
                ['2H'],
                ['3D'],
                ['TH','KH'],
                ['AS','5S','8S'],
                ['JH'],
                ['2C','KC'],
                ['4D'],
                ['6S','7S']]
    self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
    self.assertListEqual(     expected,      actual,  'Incorrect result' )
    pass

def test5(self):
    actual   = solitaire.play(['6C','AC','7C','7D','KC','6S','9H','5C','JS','3S','TS','9D','JH',
                               'QD','2D','8H','QS','8D','8C','2S','TC','9C','9S','5H','4D','3C',
                               'JD','QC','AS','3H','TD','7H','KS','KH','AD','TH','KD','4C','2C',
                               'QH','3D','5D','7S','4S','6D','2H','AH','JC','8S','4H','5S','6H'] )
    expected = [['JC','QC','2C','2H','QH','AH','4H','6H','6D','3D','5D','JD','TD','TH','7H','7S','4S','4C','TC','3C','3H','5H','KH','KS','KD','8D','4D','AD','AS','9S','9C','KC','5C','8C','8H','9H','JH','JS','TS','3S','6S','QS','2S','2D','QD','9D','7D','7C','AC','6C'],
                ['5S','8S']]
    self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
    self.assertListEqual(     expected,      actual,  'Incorrect result' )
    pass

def test6(self):
    actual   = solitaire.play(['8S','7C','AS','5H','7H','9D','8C','8H','3H','TC','AH','KH','JH',
                               'JC','AD','9C','AC','QS','6H','KC','KS','5C','TS','3C','9H','7D',
                               '3S','4D','TD','QD','5D','9S','3D','6S','6D','8D','JS','2S','4C',
                               '4S','5S','6C','2D','4H','7S','2H','KD','TH','JD','QH','QC','2C'] )
    expected = [['5S','7S','JS','2S','4S','4C','4H','TH','QH','QC','6C','9C','AC','KC','KS','QS','3S','3C','5C','5D','QD','7D','4D','TD','TS','9S','6S','6D','3D','8D','2D','2H','KH','6H','9H','9D','AD','AS','AH','3H','5H','JH','JC','8C','TC','7C','7H','8H','8S'],
                ['JD','KD'],
                ['2C']]
    self.assertEqual    ( len(expected), len(actual), 'Incorrect result' )
    self.assertListEqual(     expected,      actual,  'Incorrect result' )
    pass

if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()

因为我刚刚学习了很多python idk,但我不确定如何使用那个测试'class',我想这就是所谓的。

有什么想法吗?

非常感谢!

1 个答案:

答案 0 :(得分:2)

似乎play函数应该在导入的solitaire模块中。这意味着将值分配给变量actual

 actual = solitaire.play()

是存储在pile函数中的play变量中的结果。然后,测试函数将预期结果存储在变量expected中。因此,您应该将solitaire模块和测试脚本放在同一目录中,然后运行:

 python test.py

它应该给你一些反馈。运行你所拥有的产生一些错误。通过解释器运行play函数可以为您提供有关错误位置的一些反馈。

修改

OP发表评论后编辑。

您在pile函数中调用play的内容并不重要。它是由函数返回的,所以当你执行像actual = solitaire.play()那样返回堆并将其分配给实际的东西时。

要退后一步,您需要两段代码:play函数和类Test。设置代码的方式应该在两个单独的文件中,让我们称之为solitaire.py和test.py. play函数应该在solitaire.py文件中,而Test类应该在test.py中。 test.py脚本使用行

从solitaire.py文件导入函数

import solitaire

这允许你做像。

这样的事情

actual = solitaire.play()

该行基本上等于说“从纸牌模块使用播放功能并将其返回的数据分配给变量实际”。要运行脚本,请确保纸牌和测试文件位于同一目录中,该目录是终端中的工作目录。然后,您只需在终端中输入python test.py即可运行。

如果您需要进一步了解Python,如何运行脚本等,请查看Learn Python the Hard Way之类的内容。