这可能是很简单的事情。
我写过这门课
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: 发布于 2019-12-22 11:23:30
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建议为每个assert语句添加一条消息。或者,您可以使用从UnitTest继承的unittest模块,并使用专门的assertEquals方法,该方法内置了相当错误的打印。
发布于 2019-12-22 11:00:59
是的,显然我不得不增加一个return,而不是仅仅设置新的价格。不过,我还是会分享的,以防另一个人包围它。
def discount(self, discount):
return self.price * (1 - discount)https://stackoverflow.com/questions/59443622
复制相似问题