有没有办法配置Python日志记录内容或断言失败的上下文? [英] Is there a way to configure Python logging to log content or context of assert failures?

查看:350
本文介绍了有没有办法配置Python日志记录内容或断言失败的上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行测试用例,我想以这样的方式设置日志记录,以便自动记录所有测试失败的情况 - 但是我想获得自定义响应,例如,如果声明失败我想获得我的测试所做的请求的响应,而不仅仅是默认消息哪个断言失败。目前我只知道这个断言失败了,但是我不知道程序返回了什么。

所以说我正在测试一个视图函数,比如我有(自测):
return self .app.get(/ edit_profile,follow_redirects = True)

def test_edit_profile(self):
rv = self.edit_profile()
assertEdit your profile admin在rv.data中

有没有办法让我以这样的方式配置日志记录:每个测试失败会记录rv.data到一个日志文件?



目前我简单地添加logging.debug(rv.data)在以前的测试失败的断言,再次运行测试,调试这个问题,然后继续,但是这样做效果不好,以后很容易忘记这些loggging.debug(),如果我有一个函数来自动将网页响应记录到测试请求

解决方案

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

使用 assertWhatever 方法。我不完全明白为什么,但你不应该使用 assert 语句来断言 unittest 中的断言。 (其他框架可以让你用 assert 来声明)。

为了参考,把消息添加到 assert 断言的工作方式如下:

  assert'编辑个人档案admin'在rv.data ,rv.data 


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

这篇关于有没有办法配置Python日志记录内容或断言失败的上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆