禁止来自单元测试的补充错误消息

达拉

我在单元测试中使用assertRegexpMatches

self.assertRegexpMatches(text, regexp, msg='custom short message')

问题是unittest将自己的错误消息添加到msg指定的参数中:

AssertionError: <custom short message>: '<regexp>' not found in '<text>'

由于要匹配的文本非常大(约1页),因此会弄乱测试报告。有什么方法可以抑制unittest将'<regexp>' not found in '<text>'零件添加到指定的错误消息中?

虚假的

根据源代码,只要您使用,就不可能抑制该消息TestCase.assertRegexpMatches

def assertRegexpMatches(self, text, expected_regexp, msg=None):
    """Fail the test unless the text matches the regular expression."""
    if isinstance(expected_regexp, basestring):
        expected_regexp = re.compile(expected_regexp)
    if not expected_regexp.search(text):
        msg = msg or "Regexp didn't match"
        msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text) #<-
        raise self.failureException(msg)

您需要定义自己的断言方法或使用自定义字符串类,如下所示:

示例(不是漂亮的解决方案,但是可以工作):

import unittest

class CustomString(str):
    # XXX: implementation dependant
    # redefine `__repr__` because `assertRegexpMatches` use `%r`
    def __repr__(self):
        return '<Huge string>'

class TestFoo(unittest.TestCase):
    def test_foo(self):
        self.assertRegexpMatches(CustomString('1234'), 'abcd', msg='custom msg')

if __name__ == '__main__':
    unittest.main()

re.search与配合使用assertTrue

class TestFoo(unittest.TestCase):
    def test_foo(self):
        self.assertTrue(re.search(regexp, text), msg='custom msg')

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

VSTS 单元测试错误消息

来自分类Dev

在单元测试中,它给出此错误消息

来自分类Dev

错误单元测试ModelForm

来自分类Dev

ActiveMQ消息分组单元测试

来自分类Dev

单元测试 wpf(禁用消息框)

来自分类Dev

禁止来自外部功能的错误消息

来自分类Dev

单元测试服务返回“无法添加服务类”错误消息

来自分类Dev

Rust紧急单元测试-匹配错误消息的详细信息

来自分类Dev

如何在Web API中对冲突响应的错误消息进行单元测试

来自分类Dev

用Kotlin编写单元测试时IntelliJ显示错误消息

来自分类Dev

如何在单元测试中禁止Spark日志记录?

来自分类Dev

Noda时间单元测试XML错误

来自分类Dev

单元测试中的部分模拟/错误

来自分类Dev

捕获单元测试库链接错误

来自分类Dev

角度单元测试Jasmine Spy错误

来自分类Dev

Python单元测试断言错误

来自分类Dev

Python单元测试模拟错误

来自分类Dev

Django单元测试断言错误

来自分类Dev

Angular单元测试Jasmine Spy错误

来自分类Dev

单元测试输出中的编码错误

来自分类Dev

Python单元测试模拟错误

来自分类Dev

c#单元测试错误

来自分类Dev

硒+ Python的单元测试+:错误消息在执行测试时,“其他元素将收到点击”

来自分类Dev

禁止来自MPI的消息

来自分类Dev

来自组件的 Angular 单元测试 spyOn 服务

来自分类Dev

如何使用HTTPResponseRedirect对django消息进行单元测试?

来自分类Dev

捕获异常后写入的日志消息的单元测试

来自分类Dev

grunt 测试 - 致命错误:尚未实施单元测试

来自分类Dev

单元测试#ifdef