0

I'm new to programming and this is my first time trying to create test cases. My goal is to use the unittesting module and run test cases on my program for each possible scenario. My question is how do you determine which functions need testing or where to begin? Can someone provide an example from my code snippet to help me get started? And once you start creating test cases how do you know when you're finished?

import string

# -----------------------------

def createPokedex(filename):
    pokedex = {}
    file = open(filename, "r")
    for pokemon in file:
        pokelist = pokemon.strip().split(",")
        index = int(pokelist.pop(0))
        pokedex[index] = [pokelist.pop(0)]         
        pokedex[index] += [int(pokelist.pop(0))]   
        pokedex[index] += [pokelist.pop(0)]
        if len(pokelist) == 1:
            pokedex[index] += [pokelist.pop(0)]    
    return pokedex

# -----------------------------

def print_pokedex(pokedex):
    print("\n------ The Pokedex --------\n")
    for i in pokedex:
        list = pokedex.get(i)
        n = len(list)
        if n == 3:
            print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2])
        if n == 4:
            print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2] + " and " + list[3])
    print()


def getChoice(low, high, message):
    legal_choice = False
    while not legal_choice:
        legal_choice = True
        answer = input(message)
        for character in answer:
            if character not in string.digits:
                legal_choice = False
                print("That is not a number, try again.")
                break
        if legal_choice:
            answer = int(answer)
            if (answer < low) or (answer > high):
                legal_choice = False
                print("That is not a valid choice, try again.")
    return answer
  

def lookup_by_number(pokedex, number):
    if number in pokedex:
        list = pokedex.get(number)
        n = len(list)
        if n == 3:
            print("Number = ", number, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2])
        if n == 4:
            print("Number = ", number, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2] + " and " + list[3])
        print()
    else:
        print("Invalid number!\n")


def lookup_by_name(pokedex, name):
    found = 0
    for i in pokedex:
        list = pokedex.get(i)
        if list[0].lower() == name.lower():
            found = 1
            n = len(list)
            if n == 3:
                print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2])
            if n == 4:
                print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2] + " and " + list[3])
            print()
    if found == 0:
        print("Invalid name!\n")
      

def total_by_type(pokedex, type):
    count = 0
    for i in pokedex:
        list = pokedex.get(i)
        n = len(list)
        if n == 4:
            if ((list[2].lower() == type.lower()) or (list[2].lower() == type.lower())):
                count = count + 1
        if n == 3:
            if (list[2] == type):
                count = count + 1
    print("Number of pokemon of given type = ", count)
    print()


def average_hit_points(pokedex):
    count = 0
    for i in pokedex:
        list = pokedex.get(i)
        count = count + list[1]
    avg = count / 30
    print("Average hit points = %.2f" %avg)
    print()


def print_menu():
    print("1. Print Pokedex")
    print("2. Print Pokemon by Name")
    print("3. Print Pokemon by Number")
    print("4. Count pokemon by type")
    print("5. Print Average Hit Points")
    print("6. Quit")

# -----------------------------

def main():
    pokedex = createPokedex(r"C:\Users\phill\OneDrive\Documents\pokedex.txt")
    choice = 0
    while choice != 6:
        print_menu()
        choice = getChoice(1, 6, "Enter a menu option: ")
        if choice == 1:  
            print_pokedex(pokedex)
        elif choice == 2:
            name = input("Enter a Pokemon name: ")
            name = name.capitalize()
            lookup_by_name(pokedex, name)
        elif choice == 3:
            number = getChoice(1, 1000, "Enter a Pokemon number: ")
            lookup_by_number(pokedex, number)
        elif choice == 4:
            type = input("Enter a Type: ")
            total_by_type(pokedex, type.lower())
        elif choice == 5:
            average_hit_points(pokedex)
    print("Thank you. Goodbye!")

main()
flyingdutchman
  • 1,012
  • 13
  • 16

0 Answers0