Hey guys i have a quick question about testing my method. Im new to python as of last week but im trying. My assignment was to make a method that takes of list of cards and sorts em in a certain fashion. I know all my checks and and if statements and such work because i tested them in a python shell, but i figured if i put all my code into the shell it would be out of index(correct indentation). So if you guys could help the thing im just not sure about is how to implement my method.
here is my method:
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
and this is like what my teacher gave me, like the test cases:
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()
Because i just learned python idk that much but im just not sure how i would use that test ‘class’ i guess it’s called.
Any idea’s?
Thanks a bunch!
It seems like the
playfunction should be in thesolitairemodule that is being imported. This means that the values being assigned to the variableactual:are the results stored in the
pilevariable in theplayfunction. The test functions then have the expected results stored in the variableexpected. So you should have thesolitairemodule and the test script in the same directory and then just run:and it should give you some feedback. Running what you have produces a few errors. Running the
playfunction through the interpreter should give you some feedback on where you have errors.Edit:
Edit after the OP’s comment.
It doesn’t matter what you call
pilein theplayfunction. It’s being returned by the function, so when you do something likeactual = solitaire.play()that’s returning pile and assigning it to actual.To take a step back, you have two pieces of code: the
playfunction and the classTest. The way the code is setup these should be in two separate files, let’s call them solitaire.py and test.py. Theplayfunction should be in the solitaire.py file, while theTestclass should go in test.py. The test.py script is importing the functions from the solitaire.py file with the lineimport solitaireThis allows you to do things like.
actual = solitaire.play()This line basically amounts to saying “From the solitaire module use the play function and assign the data it returns to the variable actual.” To run the script, make sure the solitaire and test files are in the same directory, and that directory is the working directory in the terminal. You then just enter
python test.pyinto the terminal and let it run.If you need further help understanding Python, how to run scripts, etc., take a look at something like Learn Python the Hard Way.