Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing truncation of long strings in pytest

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.

like image 593
gna gent Avatar asked Oct 04 '13 01:10

gna gent


1 Answers

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.

Edit As Cecil Curry points out, this hack is not intended as a complete solution. Hopefully this feature gets added to Pytest so we can get what we want without breaking encapsulation. :)

like image 184
user2859458 Avatar answered Oct 23 '22 00:10

user2859458