How to print traceback of error inside plugin (with color) #8972
Unanswered
Cameronsplaze
asked this question in
Q&A
Replies: 1 comment
-
The best way I could find was to call self.add_report_section, since this YamlItem is based on pytest.Item. Still return the repr_failure_py without converting it to a string, to get the normal output. Appending strings on the end of the normal output was the wrong way to go. def repr_failure(self, excinfo):
"""Called when self.runtest() raises an exception."""
# Use built in cli arg:
tbstyle = self.config.getoption("tbstyle", "auto")
error_msg = "\n".join(
[
"Test failed",
" Test: '{0}'".format(self.test_info["title"]),
" File: '{0}'".format(self.file_name),
]
)
# Add this section to the report:
self.add_report_section("call", "yaml test info", error_msg)
# Call the *real* report, and return that:
return self._repr_failure_py(excinfo, style=tbstyle) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a custom pytest.Item class, very similar to your example here: https://docs.pytest.org/en/6.2.x/example/nonpython.html
I'm trying to add extra error info to the end of a test, if it fails. If I don't declare "repr_failure", it prints the default. If I do, it only prints what I return in repr_failure.
I found 'self._repr_failure_py(excinfo, style="short")'. If I only return it, I get color. If I turn it to a string, and append my custom info after it, everything is in white text.
Is this how we're expected to edit error messages? I'd just like to append info to what already exists. Is there a way to do so, and keep the color highlighting?
Beta Was this translation helpful? Give feedback.
All reactions