# Testing in Python Homework Sample

You need to write a simple set of functions to handle cards in a deck. The card should have one of 4 suits, and a rank (between 2-14, 11 = Jack, 12 = Queen, 13 = King, 14 = Ace). The deck has methods to shuffle the cards, to deal a card, to add to the top or bottom of the deck and whether the deck is empty or not. Create a file test1 and write methods to test all the functions, and use print statements to confirm the values. Then create another test2 and write proper unit tests for all the methods. For more Python programming assignments contact us for a quote.

Solution:

card.py

class Card:

def __init__(self: “Card”, rank: int, suit: str):
“””
Create a new card (rank is 2-14 where 14 is Ace)
:param rank: 2-14 (11 = Jack, 12 = Queen, 13 = King)
:param suit: CSDH (Clubs Spades Diamonds Hearts)
“””
self.rank = rank
self.suit = suit

def color(self):
“””
Return the color of the card
:return: Clubs and Spades are black, others are red
“””
return “black” if self.suit in “CS” else “red”

def __str__(self):
“””
Convert the card to a string
:return: The card as string format 2H, JC, QD, KS, AS
“””
symbols = [“”, “”, “2”, “3”, “4”, “5”, “6”,
“7”, “8”, “9”, “10”, “J”, “Q”, “K”, “A”]
return symbols[self.rank] + self.suit

deck.py

import random
from card import Card

class Deck:
“””
Create a deck of cards
“””

def __init__(self):
“””
Create the deck in order
“””
self._cards = [Card(rank, suit) for suit in “CDHS” for rank in range(2, 15)]

def __str__(self):
“””
Convert the deck into a string
:return: The deck as a string
“””
return ” “.join(str(card) for card in self._cards)

def deal(self) -> Card:
“””
Draw a card (remove it from the deck)
:return: The card at the top of the deck
“””
return self._cards.pop()

“””
Add a card back onto the top of the deck
:param card: Card to add to the deck
:return: None
“””
self._cards.append(card)

“””
Add a card back onto the bottom of the deck
:param card: Card to add to the deck
:return: None
“””
self._cards.insert(0, card)

def count(self) -> int:
“””
How many cards are in the deck
:return: The number of cards remaining
“””
return len(self._cards)

def empty(self) -> bool:
“””
Is the deck exhausted?
:return: True if no more cards are present
“””
return len(self._cards) == 0

def shuffle(self):
“””
Shuffle the order of the cards
:return: The deck is shuffled
“””
random.shuffle(self._cards)

test1.py

from card import Card
from deck import Deck

card = Card(2, “D”)
print(card) # 2D expected

# Check for 9 through Ace for the edge cases
for rank in range(9, 15):
card = Card(rank, “S”)
print(card, card.rank, card.suit) #9, 10, Jack, Queen, King, Ace

# Check for each suit, including color
for suit in “CDHS”:
card = Card(4, suit)
print(card, card.suit, card.color())

deck = Deck()
print(“In order”, deck) # Deck in order
first = deck.deal()
print(first) # First card
print(“In order-1”, deck) # Should be 1 less
print(“In order”, deck) # Deck in order
first = deck.deal()
deck.add_to_bottom(first) # Put it back on bottom
print(“In order (apart from last)”, deck) # Deck in order (except for last card)
deck.shuffle()
print(“Shuffled”, deck) # Shuffled deck
print(“Count”, deck.count(), “Empty”, deck.empty())
while not deck.empty():
deck.deal()
print(“Count”, deck.count(), “Empty”, deck.empty())
print(“1 card”, deck) # Deck in order
print(“Count”, deck.count(), “Empty”, deck.empty())

test2.py

import unittest
from card import Card
from deck import Deck

class TestCardMethods(unittest.TestCase):

# Check a single card
def test_single(self):
card = Card(2, “D”)
self.assertEqual(str(card), “2D”)

# Check for 9 through Ace for the edge cases
def test_rank(self):
checks = {9: “9S”, 10: “10S”, 11: “JS”, 12: “QS”, 13: “KS”, 14: “AS”}
for rank in checks:
card = Card(rank, “S”)
self.assertEqual(str(card), checks[rank])

# Check for each suit, including color
def test_suit(self):
colors = {“C”: “black”, “D”: “red”, “H”: “red”, “S”: “black”}
for suit in colors:
card = Card(4, suit)
self.assertEqual(str(card), “4%s” % suit)
# Check the color is correct
self.assertEqual(card.color(), colors[suit])

class TestDeckMethods(unittest.TestCase):

def setUp(self):
self.deck = Deck()

# Check drawing a single card
def test_single(self):
card = self.deck.deal()
self.assertEqual(str(card), “AS”)

# Check count of cards
def test_count(self):
self.assertEqual(self.deck.count(), 52)
self.deck.deal()
self.assertEqual(self.deck.count(), 51)
self.empty()
self.assertEqual(self.deck.count(), 0)

def empty(self):
while not self.deck.empty():
self.deck.deal()

# Check for empty
def test_empty(self):
self.assertEqual(self.deck.empty(), False)
self.empty()
self.assertEqual(self.deck.empty(), True)

# Check different cards
def test_all_different(self):
cards = set() # Count all the cards, make sure they are all different
while not self.deck.empty():
self.assertEqual(len(cards), 52)

# Test string
def test_str(self):
self.assertTrue(str(self.deck).startswith(“2C 3C”))
self.assertTrue(str(self.deck).endswith(“10S JS QS KS AS”))

card = self.deck.deal()
self.assertEqual(str(card), “AS”)
while not self.deck.empty():
card = self.deck.deal()
self.assertEqual(str(card), “AS”)