为 python 计算器创建测试用例

Creating Testcases for python Calculator

计算器假定为每位客人确定相等的金额以支付总账单

我的代码:

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()

我需要创建测试用例但不完全确定每次我如何完全这样做运行这段代码来测试它是否有效它说测试失败并且它还要求我输入值还有

测试用例代码:

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()

你的测试失败了,因为你 运行 对浮点数进行了测试,就好像它是一个函数一样。 Total_Tip 不是带有参数 (Calc_Tip,Num_of_Guest) 的函数,而是存储 (Calc_Tip/Num_of_Guests).

结果的浮点变量

Total_Tip 要通过测试,它应该类似于:

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

它不起作用,因为 Total_tip 变量是作为其他变量的结果获得的,这些变量从用户输入中获取值。仅使用带括号的变量不会传达变量的那些值(执行)。除非该变量已分配函数,否则您必须将大部分 BillCalc 存储在函数中,这是一个示例:

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

并且在测试文件中:

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