0

so just like the title says, I'm trying to create a unittest, but I'm not exactly sure why it's not working. I have "test" in the name.

Algorithms involved:

def Add_Budget(self, budget):
    """Adds the budget"""

    self.budget = budget

def add_expense(self, new_expense):
    """Calculates the new budget"""

    self.budget = self.budget - new_expense

    return self.budget

def purchase_prompt(self):
    """Prompts user for a numerical value when adding their expense"""
    is_valid = False

    while is_valid == False:
        try:
            self.new_expense = float(input("Please enter your expense($):"))

            if self.new_expense == int:
                is_valid = True

        except Exception as e:
            print("Wrong input!")

        return self.new_expense

Test:

import unittest
from Expense_Tracker import Expense

class TestCurrentBudget(unittest.TestCase):
    def test_add_expense(self):
        # Tests if adding the expense makes sense
        E = Expense()
        E.Add_Budget(2000)

        self.assertEqual(E.add_expense(100), 1900)
        self.assertEqual(E.purchase_prompt(), True)

I'm trying to have it come back to me as OK. It's just not running

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Kayla
  • 1
  • 1

1 Answers1

0

Based on the code provided I created two modules.

"""
Module: Expense_Tracker.py
"""
class Expense():
    def Add_Budget(self, budget):
        """Adds the budget"""

        self.budget = budget

    def add_expense(self, new_expense):
        """Calculates the new budget"""

        self.budget = self.budget - new_expense

        return self.budget

    def purchase_prompt(self):
        """Prompts user for a numerical value when adding their expense"""
        is_valid = False

        while is_valid == False:
            try:
                self.new_expense = float(
                    input("Please enter your expense($):"))

                if self.new_expense == int:
                    is_valid = True

            except Exception as e:
                print("Wrong input!")

            return self.new_expense

and

"""
Module: test_Expense_Tracker.py
"""
import unittest

from Expense_Tracker import Expense


class TestCurrentBudget(unittest.TestCase):
    def test_add_expense(self):
        # Tests if adding the expense makes sense
        E = Expense()
        E.Add_Budget(2000)

        self.assertEqual(E.add_expense(100), 1900)
        self.assertEqual(E.purchase_prompt(), True)


# Add the following lines to the test module
if __name__ == '__main__':
    unittest.main()

Notice to get the tests to run the following lines are at the end of test_Expense_Tracker.py.

# Add the following lines to the test module
if __name__ == '__main__':
    unittest.main()
Carl_M
  • 861
  • 1
  • 7
  • 14
  • Thank you! I'm just a little confused, why are those two lines at the end of test_Expense_Tracker? Because I've seen other examples where the two lines are at the end of the main code. – Kayla Mar 30 '23 at 15:37
  • If you look at the example in unittest https://docs.python.org/3/library/unittest.html#basic-example, it is in the main code because the tests are in the same module. We put the two lines in the module containing the test code. to run the test code. This is one approach. There are other approaches, as mentioned in the link above. I will say I disagree with sending someone who has something close to working to a link that basically says, "read this book". The link has 13 pages. BTW if this helps, I would appreciate your accepting the answer if still possible. – Carl_M Mar 30 '23 at 18:36