如何在pytest中获取断言以不再使用省略号缩写?
每当我有一个报告错误的断言时,它会用“...”截断我的所有字符串,为什么会这样?我怎么能阻止它?
答案 0 :(得分:4)
你能展示一个例子吗?这就是输出看起来的方式:
def test_foo():
> assert "a"*100 == "a"*100 + "b"
E assert 'aaaaaaaaaaaa...aaaaaaaaaaaaa' == 'aaaaaaaaaaaaa...aaaaaaaaaaaab'
E Skipping 89 identical leading characters in diff, use -v to show
E - aaaaaaaaaaa
E + aaaaaaaaaaab
E ? +
我认为差异很清楚它是什么失败了 - 但是-v
(正如消息所示)我得到了完整的差异:
def test_foo():
> assert "a"*100 == "a"*100 + "b"
E assert 'aaaaaaaaaaaa...aaaaaaaaaaaaa' == 'aaaaaaaaaaaaa...aaaaaaaaaaaab'
E - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
E + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab
E ?
答案 1 :(得分:0)
accepted solution通常无法正常工作。例如,在our use case中,pytest会截断传递给从断言语句调用的函数的参数:
E AssertionError: assert None is not None
E + where None = search('\\n\\*\\s.*\\b[Tt]upla item 0\\b',
'@beartyped pep_hinted() parameter pep_hinted_param=((<function <lambda> at
0x7fe278838830>,),) violates PEP type hint...7fe278838830>,):\n * Not int, bool, or
float.\n * Tuple item 0 value <function <lambda> at 0x7fe278838830> not str.')
beartype_test/unit/pep/p484/test_p484.py:124: AssertionError
请注意在上面的输出中pytest未经我们许可而在子字符串...
和violates PEP type hint
之间插入了省略号(即7fe278838830>,):\n
),替换了有意义的关键任务子字符串的输出。
非常感谢pytest开发人员。你不应该的 真的。
要证实@NicholasDiPiazza和@MatthiasUrlichs,Pytest在截断传递给从断言语句调用的函数的参数时会遗憾地忽略-v
选项–有点令人不快。
Pytest开发人员,如果我们四次要求冗长,这意味着:“是的,我们确实希望冗长,对潜在的陷阱有充分的了解,并且可以忽略不计,因为未经审查的错误报告比尽量减少那些坦率地说不适用于现实世界中持续集成的模糊理论伤害。”
我们的解决方案如下:
addopts = -vvvv --showlocals -p no:xvfb -r a --doctest-glob=
def test_problem_function():
with raises(Exception) as exception_info:
raise ValueError("a"*1024)
exception_str = str(exception_info.value) # <--- This is where the magic happens.
assert problem_function("a"*1024, exception_str)
def problem_function(substr, bigstr):
return substr not in bigstr
========================================================== FAILURES ===========================================================
______________________________________________ test_truncated_exception_message _______________________________________________
def test_truncated_exception_message():
with raises(Exception) as exception_info:
raise ValueError("a"*1024)
exception_str = str(exception_info.value)
> assert problem_function("a"*1024, exception_str)
E AssertionError: assert False
E + where False = problem_function(('a' * 1024), 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
exception_info = <ExceptionInfo ValueError('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') tblen=1>
exception_str = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaa'
beartype_test/unit/pep/p484/test_p484.py:39: AssertionError
虽然AssertionError
本身仍然被截断,但是--showlocals
选项正确地保留了本地exception_str
测试变量的未截断值。 Voilà!
这实际上像广告中那样工作,但要注意的是,这仅对直接可分配给测试中的局部变量的字符串(或对象表示形式,或任何)作广告。这是通常的情况,但并非总是如此-这就是pytest 真的在错误报告中需要开始尊重-vvvvvvvvvvvvvv
选项的原因。