Reputation: 171
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.
Upvotes: 17
Views: 10821
Reputation: 73
Regarding CLI, I have tried setting environment variables CI
and COLUMNS
, flags like --trace
, --capture
, --full-trace
, and --tb
and --verbose
with different values. After all, only --verbosity="2"
is working for NOT truncating error messages from the short test summary info.
Any value equal to or large than 2
will work.
Upvotes: 0
Reputation: 131
Another trick I just used for this (but for manual inspection, nothing automated beyond pytest itself) was to use the output as the second 'argument' to assert (i.e., as the 'assert message'). Pytest shows the full assert message without truncation. So I had:
assert shortString in veryLongString
and I wanted to see veryLongString without truncation so I changed that to:
assert shortString in veryLongString, veryLongString
The output is ugly and poorly formatted, but I can see it which was enough to let me solve my problem.
Upvotes: 1
Reputation: 2505
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. :)
Upvotes: 12
Reputation: 377
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.
Upvotes: -5