Question: This is the code i am trying to write tests for: # Card.py class Card(object): '''A simple playing card. A Card is characterized by two
This is the code i am trying to write tests for:
# Card.py class Card(object): '''A simple playing card. A Card is characterized by two components: rank: an integer value in the range 1-13, inclusive (Ace-King) suit: a character in 'cdhs' for clubs, diamonds, hearts, and spades.'''
SUITS = 'cdhs' SUIT_NAMES = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
RANKS = list(range(1,14)) RANK_NAMES = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
def __init__(self, rank, suit): '''Constructor pre: rank in range(1,14) and suit in 'cdhs' post: self has the given rank and suit''' print(self.RANKS) self.rank_num = rank self.suit_char = suit def suit(self): '''Card suit post: Returns the suit of self as a single character'''
return self.suit_char
def rank(self): '''Card rank post: Returns the rank of self as an int'''
return self.rank_num def suitName(self): '''Card suit name post: Returns one of ('clubs', 'diamonds', 'hearts', 'spades') corrresponding to self's suit.'''
index = self.SUITS.index(self.suit_char) return self.SUIT_NAMES[index]
def rankName(self): '''Card rank name post: Returns one of ('ace', 'two', 'three', ..., 'king') corresponding to self's rank.'''
index = self.RANKS.index(self.rank_num) return self.RANK_NAMES[index]
def __str__(self): '''String representation post: Returns string representing self, e.g. 'Ace of Spades' '''
return self.rankName() + ' of ' + self.suitName()
And these are the tests I am supposed to write codes for the above one:
# unitTestingCard.py # unit testing rank
import sys import unittest
from Card import *
class RankTest(unittest.TestCase): """ Tests Rank methods: rank() and rankName() """ def testRanks(self): # unit test for ranks 1-13 """ creates cards of rank 1 through 13 of clubs and verifies that the created card's rank is equal to the rank it was created with """ for i in range(1,14): myCard = Card(i,'c') # create i of clubs self.assertEqual(myCard.rank(),i) # verifies that the card's rank is i
def testRankName(self): # unit test for rank names, 'ace', 'two', 'three',... """ creates cards with rank names "ace", ... and then checks for equivalency: card's rank name, by calling method rankName, and the rank name that was given to the card"""
for i in range(1,14): hisCard= Card(i,'c') self.assertEqual(hisCard.rankName(),Card.RANK_NAMES[i-1])#because you are testing for the #of RankName, you call rankName() and Card(class).RANK_NAMES and the index that is [i-1] #because you want to begin from 0 not 1
class SuitTest(unittest.TestCase): """ Tests Suit methods: suit() and suitName() """ def testSuits(self): # unit test for suits, 'c', 'd', 'h', 's' """ creates cards of rank ... of c (clubs), d (diamonds), h(hearts) and s (spades), and verifies that the created card's suit is equal to the suit it was created with (c,d,h,s) """ for i in ('c','d','h','s'): topCard=Card(7,i)#the number(rank) and the suit self.assertEqual(topCard.suit(),i) def testSuitName(self): # unit test for suit names, 'clubs', 'diamonds',... """ creates cards with all the suit names ... and then checks for equivalency: card's suit name, by calling method suitName, and the suit name that was given to the card""" for i in ('c','d','h','s'): herCard= Card(6,i) self.assertEqual(herCard.suitName(),Card.SUIT_NAMES) def main(argv): unittest.main()
if __name__ == '__main__': main(sys.argv)
and i am having a problem writing the test for define testSuitName so i need help. In python please!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
