gpt4 book ai didi

python - 测试期间出现断言错误,没有明确的错误消息

转载 作者:行者123 更新时间:2023-12-01 00:14:02 28 4
gpt4 key购买 nike

这可能非常简单。

我写了这门课

class Pants :
def __init__(self, pants_color, waist_size, length, price):
self.color = pants_color
self.waist_size = waist_size
self.length = length
self.price = price

def change_price(self, new_price):
self.price = new_price

def discount(self, discount):
self.price = self.price * (1 - discount)

我正在对其运行这些测试:

def check_results():
pants = Pants('red', 35, 36, 15.12)
assert pants.color == 'red'
assert pants.waist_size == 35
assert pants.length == 36
assert pants.price == 15.12

pants.change_price(10) == 10
assert pants.price == 10

assert pants.discount(.1) == 9

print('You made it to the end of the check. Nice job!')

check_results()

由于某种原因,我不断看到错误消息,但没有实际错误,它只是说 AssertionError:

    ---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-5-50abebbadc01> in <module>()
13 print('You made it to the end of the check. Nice job!')
14
---> 15 check_results()

<ipython-input-5-50abebbadc01> in check_results()
9 assert pants.price == 10
10
---> 11 assert pants.discount(.1) == 9
12
13 print('You made it to the end of the check. Nice job!')

AssertionError:

最佳答案

assert 只会打印您给出的错误消息,例如:

assert pants.discount(.1) == 9, "Pants discount should be {}, was {}".format(9, pants.discount(0.1))

会报错

AssertionError: Pants discount should be 9, was None

建议为每个断言语句添加一条消息。或者,您可以使用从 unittest 模块继承 UnitTest 的方法,并使用特殊的 assertEquals 方法,该方法具有内置的漂亮的错误打印功能。

关于python - 测试期间出现断言错误,没有明确的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com