首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AssertionError在测试过程中没有明确的错误消息

AssertionError在测试过程中没有明确的错误消息
EN

Stack Overflow用户
提问于 2019-12-22 11:00:59
回答 2查看 105关注 0票数 2

这可能是很简单的事情。

我写过这门课

代码语言:javascript
复制
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)

我正在做这些测试:

代码语言:javascript
复制
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:

代码语言:javascript
复制
    ---------------------------------------------------------------------------
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: 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-22 11:23:30

assert只会打印您提供的错误消息,例如:

代码语言:javascript
复制
assert pants.discount(.1) == 9, "Pants discount should be {}, was {}".format(9, pants.discount(0.1))

会给出错误

代码语言:javascript
复制
AssertionError: Pants discount should be 9, was None

建议为每个assert语句添加一条消息。或者,您可以使用从UnitTest继承的unittest模块,并使用专门的assertEquals方法,该方法内置了相当错误的打印。

票数 2
EN

Stack Overflow用户

发布于 2019-12-22 11:00:59

是的,显然我不得不增加一个return,而不是仅仅设置新的价格。不过,我还是会分享的,以防另一个人包围它。

代码语言:javascript
复制
 def discount(self, discount):
    return self.price * (1 - discount)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59443622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档