简体   繁体   中英

Creating Testcases for python Calculator

The Calculator is suppose to determine an equal amount for each guest to pay for the total bill

My code:

Total_Bill_Value=int(input("Enter your total cost of the Bill : "))
#Requests user to input the value of their bill

Num_of_Guests=int(input("Enter the total number of Guests : "))
#Requests users to input number of guests

Calc_Tip=(Total_Bill_Value/100)*15
#Calculates 15% of the bill as tip

Total=Total_Bill_Value+Calc_Tip
#total of the bill including tip

Total_Tip=Calc_Tip/Num_of_Guests
#Splits the tip equaly among all guests

Total_Pay=Total/Num_of_Guests
#Splits the total bill equaly among all guests

def main ():
    print("The Tip(15% of bill) is =${}".format(Calc_Tip))
    print("The Total cost of bill including Tip is = ${}".format(Total))
    print("Required amount from each Guest for the Tip is:")
    for i in range(1,Num_of_Guests+1):
        print("Guest{} =${:.2f}".format(i,Total_Tip))
    print("Required amount from each Guest for the Total bill is:")
    for i in range(1,Num_of_Guests+1):
        print("Guest{} =${:.2f}".format(i,Total_Pay))
if __name__ == '__main__':
    main()

I need to create testcases but not entirely sure how to entirely do so everytime i run this code to test if it works or not it says the test failed and it also is requiring me to input the values aswell

TestCase code:

import unittest
import BillCalc

class Test(unittest.TestCase):


    def test2(self): #it checks the main method
        self.assertEqual(3.75, BillCalc.Total_Tip(100,4))



if __name__ == '__main__':
    unittest.main()

Your test fails because you're running a test on a float as if it was a function. Total_Tip is not a function with the parameters (Calc_Tip,Num_of_Guest) but a float variable that stores the result for (Calc_Tip/Num_of_Guests).

For Total_Tip to pass the test it should look something like:

def Total_Tip(Total_Bill_Value, Num_of_Guests):
    Calc_Tip = (Total_Bill_Value / 100) * 15
    return Calc_Tip/Num_of_Guests

It isn't working because the Total_tip variable is obtained as a result of other variables, that get values from user input. Just using a variable with brackets doesn't communicate the variable those values (to execute). Unless that variable has a function assigned, therefore you must store the most BillCalc inside a function, here's a sample:

def calculate(total_bill, guests_no):
  Calc_Tip=(total_bill/100)*15
  #Calculates 15% of the bill as tip

  Total_Tip=Calc_Tip/guests_no
  #Splits the tip equaly among all guests

  return Total_Tip

And in the test file:

 def test2(self): #it checks the main method
        self.assertEqual(3.75, BillCalc.calculate(100,4))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM