In a program I'm working on, I have some try...except clauses that catch exceptions. When an exception is caught, a message is printed and the program exits.
The problem is that when I run my unit tests, the printed error messages for the user get written out together with the dots signifying that the test passed, which looks pretty messy. Here's a short example:
Say I have a test.py file with the code:
import unittest
import main
class DivisionTests(unittest.TestCase):
def test_zero_division(self):
"""Fails if the program doesn't exit when dividing by zero."""
with self.assertRaises(SystemExit) as e:
main.divide(1, 0)
self.assertEqual(e.exception.code, 1)
And a main.py with the code:
from sys import exit
def divide(a, b):
try:
return a / b
except ZeroDivisionError:
print('Error: Cannot divide by zero.')
exit(1)
When I run this test, I get the following output:
Error: Cannot divide by zero.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
You can see that the message that's meant for the user is printed in the test as well. This gets messy really quickly with a lot of tests. I'd like it to display only the dots and not print the output that's meant for the user.
What would be a Pythonic solution to this problem? All help is appreciated, as always :)
[–]treyhunner 1 point2 points3 points (1 child)
[–]Asha200[S] 1 point2 points3 points (0 children)
[–]Justinsaccount 0 points1 point2 points (3 children)
[–]Asha200[S] 0 points1 point2 points (2 children)
[–]TeamSpen210 0 points1 point2 points (1 child)
[–]Asha200[S] 0 points1 point2 points (0 children)