0

I have written this program as my final project. How would I write a pytest file to assure the program is properly tested. It really confused me since the functions I implemented in the program do NOT return values when executed. Please give an example of get_user_input() function and how can I pytest assure it.

import sys

def main():
    print("~~~~~~~~~~~~~~~~~~~~~~~~~ Welcome to the Expense Tracker ~~~~~~~~~~~~~~~~~~~~~~~~~")
    print("This program helps track your total expenses according to their categories")
    print("Please Note that the categories are Utilities, Rent, Insurance, Entertainment, Travel & Payroll")
    categories = ["utilities", "rent", "insurance", "entertainment", "travel", "payroll"]
    expenses = {category: [] for category in categories} #Initilizes a dictionary with empty key values (to be appended later by the function add_expense())
    
    
    while True:
        question = input("Type 1 to add one more or 2 to Quit the program to see the result: ").strip()
        if question == "1":
            get_user_input(categories, expenses)
        elif question == "2":
            view_expenses(expenses)
            sys.exit("System Quitted")  # Exit the program
        else:
            print("Invalid Input, Please type 1 to Continue or 2 to Quit")

def add_expense(amount, category, expenses):
    expenses[category].append(float(amount))  # Append the expense amount to the category list
    print("Expense added successfully!")

def view_expenses(expenses):
    print("\nCurrent Expenses:")
    total_expenses = 0
    for category, items in expenses.items():
        total_category = sum(items)
        total_expenses += total_category
        print(f"{category.capitalize()}: ${total_category:.2f}")
    print(f"\nTotal Expenses: ${total_expenses:.2f}")

def get_user_input(categories, expenses):
    while True:
        user_input = input("Please enter the amount in USD (WITHOUT DOLLAR SIGN) and the category of the expense separated by a comma: ").replace(" ", "").lower()

        if "," not in user_input:
            print("Invalid input format. Please use the format: amount, category")
            continue

        amount, category = user_input.split(",")

        if category not in categories:
            print("Invalid Category!")
            continue 
    
        else:
            add_expense(amount, category, expenses)
            break

if __name__ == "__main__":
    main()

Another off-topic question. Can that project be considered as enough for the final project? or is it not as complicated as it is required to be? What could be some tips/advices for tuning it?

Thanks!!

5
  • just my opinion but I dont think this is even in the vicinity of close enough for a final project. There is no complexity to it and it doesnt demonstrate mastery of most of the topics covered in the course.
    – UpAndAdam
    Commented Jan 9 at 21:45
  • ive reworded your question to be appropriate and remove nonsense. "Running well" is nonsense and you should remove it from your vocabulary. A program either works or it doesn't. It doesn't run well or poorly. Testing is to make sure your program is robust and handles all conditions.
    – UpAndAdam
    Commented Jan 9 at 21:47
  • You are looking at the testing wrong. And the fact that you have nothing you see to even test is a further testament that what you have written is not a sufficient piece of work for a final project. All you have written as in event loop to add items to a dictionary. Finally using a while true loop in get_user_input is a horrible practice and makes it untestable. Each function should have one and only one responsibility. This demonstrates you have not learned proper design from the course. Each of your functions is beyond trivial.
    – UpAndAdam
    Commented Jan 9 at 21:50
  • Advice is delete this code and start again. But dont start by coding. Start by coming up with an interesting problem that will make use of different things you have learned and then outline a solution and then code it. You should be applying the skills you learned about how to go about problem solving throughout the course.
    – UpAndAdam
    Commented Jan 9 at 21:52
  • Also I STRONGLY suggest you get rid of every while True and stop using that idiom. Everywhere you use it there is no need and there is a proper loop you could be using instead that would demonstrate you've actually learned techniques of python.
    – UpAndAdam
    Commented Jan 9 at 21:58

0

You must log in to answer this question.

Browse other questions tagged .