Python 3单元测试用户输入

时间:2017-12-07 07:45:23

标签: python python-3.x unit-testing

我绝对不擅长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()

1 个答案:

答案 0 :(得分:5)

您可以使用 mocking ,在其中使用测试提供的版本替换函数或类。您可以使用unittest.mock() module

执行此操作

在这种情况下,您可以在模块中修补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]})

因为input在您的模块中不存在(它是内置函数),所以我告诉mock.patch() decorator创建名称;现在将使用此input而不是内置函数。

side_effect attribute可让您说明多个结果;每次调用模拟时,它都会返回该列表中的下一个值。因此,第一次返回'Albert Einstein'时,下一次'42.81'等等。

这可以让您模拟实际的用户输入。

如果您的测试正确,您会注意到您的功能存在错误;当输入除float()以外的任何内容或输入有效数值时,ValueError调用将抛出done异常。您需要重新编写代码以解决此问题。尝试使用mocked_input.side_effect = ['Albert Einstein', 'Not an expense', '42.81', 'done']来触发错误。