Preventing truncation of long strings in pytest

℡╲_俬逩灬. 提交于 2020-02-21 10:21:26

问题


I have written a test harness for system tests of our code using pytest. These tests are used in our continuous integration system so I am using the junit xml output option. The truncation of long strings by pytest is causing me problems. I know I can prevent it using the -vv option but then that gives verbose output for the results of each test which is difficult to read. Essentially I want a different way to prevent the truncation of the long string at least in the junit xml file. If it also worked in the console output, that would be better but not essential.

Our code produces reports with a large number of values and I compare the output to a set of output known to be correct. I am reporting all fields that are in error not just the first error. So I am generating a list of strings with one error per string. I then join the strings with newlines to create one long string and long string that contains all the errors. If the assertion fails I need to see the entire contents of the string which could be several hundred lines.

errors = []
error.extend(get_report_errors())
s = '\n'.join(errors)
assert (s == '')

Any suggestions

I am using python 2.6 and 2.7 and pytest 2.3.5. I can upgrade the version of pytest of needed.


回答1:


You can use the tb flag this is the traceback output for pytest. There is a few options for that: --tb=style traceback print mode (auto/long/short/line/native/no)

You need to chooise what is the best for you.




回答2:


A simple hack that works with higher versions of pytest (e.g. 5) is to modify the values that control how much of the diff is truncated.

# conftest.py
from _pytest.assertion import truncate
truncate.DEFAULT_MAX_LINES = 9999
truncate.DEFAULT_MAX_CHARS = 9999  

This allows you to leave verbosity at 0 but still see what failed for long comparisons. Just make sure to check whenever you update pytest to make sure the internals didn't change since this is mucking around inside there.



来源:https://stackoverflow.com/questions/19171554/preventing-truncation-of-long-strings-in-pytest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!