python断言语句失败,有没有办法配置Python日志记录内容或断言失败的上下文?

I'm running test cases and I'd like to set up a my logging in such a way that it automatically logs all cases where tests fails - but I'd like to get a custom response, for example if an assertion fails I'd like to get the response to a request made by my test, not just default message which assertion failed. At present I only know that the assertion failed, but I don't know what the program returned.

So say I'm testing a view function, for example I have a test which looks roughly likes this (part of whole TestCase class)

def edit_profile(self):

return self.app.get("/edit_profile", follow_redirects=True)

def test_edit_profile(self):

rv = self.edit_profile()

assert "Edit your profile admin" in rv.data

Is there a way for me to configure logging in such a way that each test failure will log rv.data to a logs file?

Currently I simply add logging.debug(rv.data) before the assertion that failed in previous tests, run the test again, debug the issue, and go on, but this is ineffective, it's easy to forget about those loggging.debug() later on, and it would be much faster if I had an function to automatically log my webpage response to test requests if they fail.

解决方案

self.assertIn('Edit your profile admin', rv.data, msg=rv.data)

Use the assertWhatever methods. I don't fully understand why, but you're not supposed to use assert statements for assertions in unittest. (Other frameworks let you assert with assert.)

For reference, adding a message to an assert assertion works as follows:

assert 'Edit your profile admin' in rv.data, rv.data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值