如何让我在 pytest 中的断言不再被省略号缩写? [英] How can I get my assertions in pytest to stop being abbreviated with ellipsis?
问题描述
如何让我在 pytest 中的断言不再被省略号缩写?
How can I get my assertions in pytest to stop being abbreviated with ellipsis ?
每次我有一个报告错误的断言时,它都会用..."截断我的所有字符串,这是为什么?我怎么能阻止呢?
Every time I have an assertion that reports error it truncates all my strings with "..." why is that? how can I stop that?
推荐答案
其他解决方案没有 在我们的用例中,例如pytest截断传递给函数的参数从断言语句中调用:
The other solution doesn't work in general. In our use case, for example, pytest truncates parameters passed to functions called from assertion statements:
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 在未经我们许可的情况下在子字符串违反 PEP 类型提示
和 7fe278838830> 和
7fe278838830> 之间插入的省略号(即,
在上述输出中,替换理解该输出所需的关键任务子字符串....
):\n
Note the ellipses (i.e., ...
) that pytest interjected without our permission between the substrings violates PEP type hint
and 7fe278838830>,):\n
in the above output, replacing a mission-critical substring needed to make sense of that output.
谢谢,非常有帮助的 pytest 开发人员.你不应该有.真的.
Thanks, overly helpful pytest developers. You shouldn't have. Really.
为了证实 @NicholasDiPiazza 和 @MatthiasUrlichs,Pytest 遗憾地忽略了 -vcode> 截断传递给从断言语句调用的函数的参数时的选项——这有点令人恼火.
To corroborate both @NicholasDiPiazza and @MatthiasUrlichs, Pytest sadly ignores -v
options when truncating parameters passed to functions called from assertion statements – which is more than mildly infuriating.
Pytest 开发人员,如果我们要求冗长四次,这意味着:是的,我们确实想要冗长,对潜在的陷阱了如指掌,并且可以不在乎,因为未经审查的错误报告更重要而不是最大限度地减少那些坦率地说不适用于现实世界的持续集成的模糊理论危害."
Pytest developers, if we ask for verbosity four friggin' times, it means: "Yes, we really do want verbosity, are well-apprised of the potential pitfalls, and could care less because uncensored error reporting is much more important than minimizing those nebulous theoretical harms that frankly don't apply to real-world continuous integration."
我们的工作解决方案如下:
Our working solution is as follows:
- Pass the
--showlocals
option to theaddopts
setting in the project's top-levelpytest.ini
file: e.g.,
addopts = -vvvv --showlocals -p no:xvfb -r a --doctest-glob=
- 将被截断的详细字符串(或对象表示,或任何)分配给有问题的测试中的局部变量.例如,在我们的用例中,我们将被截断的详细异常消息分配给该测试中的局部变量:例如,
- Assign the verbose string (or object representation, or whatever) being truncated to a local variable in the problematic test. In our use case, for example, we assign the verbose exception message being truncated to a local variable in that test: e.g.,
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
- 改为手动检查 pytest 输出的变量值:例如,
========================================================== 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...aaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaa'
beartype_test/unit/pep/p484/test_p484.py:39: AssertionError
虽然 AssertionError
本身仍然被截断,但 --showlocals
选项正确地保留了本地 exception_str
测试变量的未截断值.瞧!
While the AssertionError
itself remains truncated, the --showlocals
option correctly preserves the untruncated value of the local exception_str
test variable. Voilà!
这实际上像宣传的那样工作,但需要注意的是,这仅适用于可在测试中直接分配给局部变量的字符串(或对象表示,或任何).这通常但并非总是如此——这就是为什么 pytest 真的 需要在其错误报告中开始尊重 -vvvvvvvvvvvvvvv
选项.
This actually works as advertised, with the caveat that this only works as advertised for strings (or object representations, or whatever) that are directly assignable to local variables in tests. That's typically but not always the case – which is why pytest really needs to start respecting -vvvvvvvvvvvvvv
options in its error reporting.
这篇关于如何让我在 pytest 中的断言不再被省略号缩写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!