New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use better-exceptions with unittest? #76
Comments
Hi @ocavue. The I don't know if this is possible to change this behavior. |
Found a simple but not perfect solution: import better_exceptions
import unittest
import sys
def wrap_test(func):
def new_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
exc, value, tb = sys.exc_info()
print(better_exceptions.format_exception(exc, value, tb))
raise e
return new_func
class MyTestCase(unittest.TestCase):
def add(self, a, b):
return a + b
@wrap_test
def test_add(self,):
r1 = self.add(1, 2)
r2 = self.add(2, "1")
self.assertTrue(r1, r2)
if __name__ == "__main__":
unittest.main() $ python3 test2.py
Traceback (most recent call last):
File "test2.py", line 9, in new_func
return func(*args, **kwargs)
│ │ └ {}
│ └ (<__main__.MyTestCase testMethod=test_add>,)
└ <function MyTestCase.test_add at 0x7fceeb6febf8>
File "test2.py", line 25, in test_add
r2 = self.add(2, '1')
└ <__main__.MyTestCase testMethod=test_add>
File "test2.py", line 20, in add
return a + b
│ └ '1'
└ 2
TypeError: unsupported operand type(s) for +: 'int' and 'str'
E
======================================================================
ERROR: test_add (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test2.py", line 13, in new_func
raise e
File "test2.py", line 9, in new_func
return func(*args, **kwargs)
File "test2.py", line 25, in test_add
r2 = self.add(2, "1")
File "test2.py", line 20, in add
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
----------------------------------------------------------------------
Ran 1 test in 0.014s
FAILED (errors=1) |
@Delgan how good/bad would it be to provide an option to |
@Qix- Honestly, I don't know, I don't have a strong opinion about this. The
Also, some users may require the same improvement for unit tests run using @ocavue In the meantime, you can also try this solution: import unittest
import better_exceptions
def patch(self, err, test):
return better_exceptions.format_exception(*err)
unittest.result.TestResult._exc_info_to_string = patch |
^ That seems like the better approach in terms of hooking, even if it's using an undocumented method. If that works @ocavue an you let us know? |
Although it uses an undocumented method, it seems to work well on all python versions. So I think this solution is great and we should write it in README. ( I used docker-compose to test it )
|
If you guys agree with me, I can create a PR with some tests about this hooking, to make sure that future python nightly version doesn't break this behavior. |
@ocavue yes please. :) |
@issuehunt has funded $40.00 to this issue.
|
Interesting bot. |
I have already posted a PR for this issue: #77 |
I think the reason why #77 is not merged yet is that I didn't click the "Slove Conversation" button before. 😂 |
"Solve Conversation" button ??? i never know github has such feature :P |
I forgot what its name is. I think it has the word "solve" (not slove) in it |
@Qix- has rewarded $28.00 to @ocavue. See it on IssueHunt
|
I want to use better-exceptions to show error when using unittest. Here is my example:
test.py:
shell:
Is there any way to use better-exceptions to print exception stack?
IssueHunt Summary
ocavue has been rewarded.
Backers (Total: $40.00)
Submitted pull Requests
Tips
IssueHunt has been backed by the following sponsors. Become a sponsor
The text was updated successfully, but these errors were encountered: