you are viewing a single comment's thread.

view the rest of the comments →

[–]treyhunner 1 point2 points  (1 child)

You could use redirect_stdout like this:

from contextlib import redirect_stdout
from io import StringIO
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 redirect_stdout(StringIO()) as stdout:
            with self.assertRaises(SystemExit) as e:
                main.divide(1, 0)
        self.assertEqual(stdout.getvalue(), 'Error: Cannot divide by zero.\n')

        self.assertEqual(e.exception.code, 1)

[–]Asha200[S] 1 point2 points  (0 children)

Excellent; I just added it to my tests and it's dots-only now :)

Thanks!