aboutsummaryrefslogtreecommitdiff
path: root/hazwaz/unittest.py
blob: 00928996b70ab3b8fa68cee0fc5c08c3b995220d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import contextlib
import io
import sys
import typing
import unittest

from .command import Command


class HazwazTestCase(unittest.TestCase):
    def run_with_argv(
            self, cmd, argv: typing.List[str]
    ) -> typing.Dict[str, io.StringIO]:
        """
        Run a command with a list of command line options.

        :param argv: the full command line except for the program name,
                     as a list of strings; e.g. ``["subcommand",
                     "--help"]`` or ``["subcommand", "--option",
                     "value"]``.

        :return: stdout and stderr resulting from the command.
        """
        stream = {
            'stdout': io.StringIO(),
            'stderr': io.StringIO(),
        }
        old_argv = sys.argv
        sys.argv = argv
        with contextlib.redirect_stdout(stream['stdout']):
            with contextlib.redirect_stderr(stream['stderr']):
                cmd.run()
        sys.argv = old_argv
        return stream


class TestCommand(Command):
    """
    Run unittests.
    """
    name = "test"

    def __init__(self, test_cases: typing.Iterable[unittest.TestCase]):
        self.test_cases = test_cases
        super().__init__()

    def main(self):
        suite = unittest.TestSuite()
        for test_case in self.test_cases:
            suite.addTests(
                unittest.TestLoader().loadTestsFromTestCase(test_case)
            )
        unittest.TextTestRunner(verbosity=1).run(suite)


def main():
    unittest.main()