对于单元测试和Python一般来说是新手,在单元测试的教程介绍中遇到了一个例子,其中使用with语句来捕获ValueError。
正在测试的脚本(invoice_calculator.py)是:
def divide_pay(amount, staff_hours):
"""
Divide an invoice evenly amongst staff depending on how many hours they
worked on a project
"""
total_hours = 0
for person in staff_hours:
total_hours += staff_hours[person]
if total_hours == 0:
raise ValueError("No hours entered")
per_hour = amount / total_hours
staff_pay = {}
for person in staff_hours:
pay = staff_hours[person] * per_hour
staff_pay[person] = pay
return staff_pay
单元测试包括此函数,以便捕获边缘情况,其中staff_hours = None
:
import unittest
from invoice_calculator import divide_pay
class InvoiceCalculatorTests(unittest.TestCase):
def test_equality(self):
pay = divide_pay(300.0, {"Alice": 3.0, "Bob": 6.0, "Carol": 0.0})
self.assertEqual(pay, {'Bob': 75.0, 'Alice': 75.0, 'Carol': 150.0})
def test_zero_hours_total(self):
with self.assertRaises(ValueError):
pay = divide_pay(360.0, {"Alice": 0.0, "Bob": 0.0, "Carol": 0.0})
if __name__ == "__main__":
unittest.main()
关于test_zero_hours_total(self)
中with
语句的使用,就该语句如何工作/执行而言,这里实际发生了什么?test_zero_hours_total()
函数的工作原理如下(外行人的描述):当360.0, {"Alice": 0.0, "Bob": 0.0, "Carol": 0.0}
(这将在divide_pay()
中引发ValueError
)作为参数传递给divide_pay()
函数时,预期的错误应该是ValueError
(我们通过将ValueError
传递给函数assertRaises()
来实现)?
3条答案
按热度按时间piztneat1#
我不是百分百确定你的问题是什么...
TestCase.assertRaises
创建了一个对象,该对象可以用作上下文管理器(这就是为什么它可以与with
语句一起使用)。当这样使用时:上下文管理器的
__exit__
方法将检查传入的异常信息。如果缺少,则会抛出AssertionError
,导致测试失败。如果异常类型错误(例如不是SomeExceptionClass
的示例),也会抛出AssertionError
。qoefvg9y2#
听起来你好像知道测试在做什么。如果
assertRaises
不存在,您可能会发现如何编写测试很有用。请注意,您不必使用
assertRaises
作为上下文管理器。你也可以向它传递异常、一个可调用对象和该可调用对象的参数:mec1mxoz3#
如果是:self.assertEqual(pay,'Bob':100.0,爱丽丝:200.0,“卡罗”:0.0})