Bry*_*hor 2 python unit-testing python-3.x
我绝对是Python单元测试的新手.我需要将它用于我必须提交的项目.我有点想知道从哪里开始,看起来我们基本上将测试参数放入我们在程序中定义的函数中,然后输入预期的结果.如果输出预期结果,我们就可以了,否则我们会得到失败或错误.
所以我的问题是我有多个用户输入存储在for循环或while循环中的变量中.我不知道从哪里开始为它们设置测试值.
这是我的所有代码:
studentTripExpenses = {}
def dictCreate(studentAmount):
for i in range(0, studentAmount):
studentName = input("What is the name of the student? ")
expenseList = []
print("Enter 'done' to move to the next student.")
while True:
expense = input("What is the cost of this expense? ")
if expense.lower() == 'done':
break
elif (float(expense) >= 0) or (float(expense) < 0):
expenseList.append(float(expense))
elif not expense.isdigit():
print("Please enter a number or enter 'done' to move on.")
studentTripExpenses[studentName] = expenseList
return studentTripExpenses
def studentCost(dct):
for i in dct:
#Variable for individual costs of student
personalCost = 0
#Determines the total cost for each student
for x in dct[i]:
personalCost = personalCost + x
#Sets each students value to their total cost to two decimal places
dct[i] = float("%.2f" % personalCost)
return dct
def amountsDue(expenseLst, studentAvgPrice):
#Runs through the dictionary of students and individual total trip costs
for key in expenseLst:
maxPerson = max(expenseLst, key=expenseLst.get)
costDifference = 0
#Determines who owes who how much money
if max(expenseLst.values()) > expenseLst[key]:
costDifference = studentAvgPrice-expenseLst[key]
if (costDifference < 0):
costDifference = costDifference * -1
print("%s owes %s $%.2f" % (key, maxPerson, costDifference))
def main():
numOfStudents = int(input("How many students are going on the trip? "))
studentCostDict = dictCreate(numOfStudents)
studentTripExpenses = studentCost(studentCostDict)
totalCost = 0
#Gets the total cost for all students
for key in (studentTripExpenses):
totalCost = totalCost + studentTripExpenses[key]
#Changes the total cost to 2 decimal places
totalCost = float("%.2f" % totalCost)
#Determines the average amount spent per student
avgCost = float("%.2f" % (totalCost/len(studentTripExpenses)))
amountsDue(studentTripExpenses, avgCost)
main()
Run Code Online (Sandbox Code Playgroud)
你可以用嘲讽,在那里你与测试提供的版本替换一个函数或类.您可以使用该unittest.mock()模块执行此操作.
在这种情况下,您可以input()在模块中修补名称; 而不是内置函数,将调用模拟对象:
from unittest import mock
from unittest import TestCase
import module_under_test
class DictCreateTests(TestCase):
@mock.patch('module_under_test.input', create=True)
def testdictCreateSimple(self, mocked_input):
mocked_input.side_effect = ['Albert Einstein', '42.81', 'done']
result = dictCreate(1)
self.assertEqual(result, {'Albert Einstein': [42.81]})
Run Code Online (Sandbox Code Playgroud)
因为input你的模块中不存在(它是一个内置函数),我告诉mock.patch()装饰者创建名称; 现在input将使用它代替内置函数.
该side_effect属性允许您声明多个结果; 每次调用mock时,它都会返回该列表中的下一个值.所以第一次'Albert Einstein'返回,下一次'42.81',等等
这可以让您模拟实际的用户输入.
如果你做对了你的测试,你会注意到你的功能有一个错误; 当输入除(或)有效数值以外的任何内容时,该float()调用将抛出ValueError异常done.您需要重新编写代码以解决此问题.尝试mocked_input.side_effect = ['Albert Einstein', 'Not an expense', '42.81', 'done']触发错误.
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |