From 0466c3406b2dc1e41fc32bfd77f1b8af22a5c69f Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Mon, 1 Aug 2022 16:53:02 +0200 Subject: Add facilities to run self-tests from a command. --- hazwaz/command.py | 3 ++- hazwaz/unittest.py | 25 +++++++++++++++++++++++++ tests/test_command.py | 3 +-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/hazwaz/command.py b/hazwaz/command.py index 185070f..1faf0fa 100644 --- a/hazwaz/command.py +++ b/hazwaz/command.py @@ -1,5 +1,6 @@ import argparse import logging +import typing logger = logging.getLogger(__name__) @@ -128,7 +129,7 @@ class Command: the :py:attr:`MainCommand.subcommands`. """ - name = None + name: typing.Optional[str] = None """ The name used to call this subcommand from the command line. diff --git a/hazwaz/unittest.py b/hazwaz/unittest.py index 96689d5..0092899 100644 --- a/hazwaz/unittest.py +++ b/hazwaz/unittest.py @@ -4,6 +4,8 @@ import sys import typing import unittest +from .command import Command + class HazwazTestCase(unittest.TestCase): def run_with_argv( @@ -30,3 +32,26 @@ class HazwazTestCase(unittest.TestCase): 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() diff --git a/tests/test_command.py b/tests/test_command.py index 96b36de..1d1402c 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -1,5 +1,4 @@ import logging -import unittest import hazwaz import hazwaz.unittest @@ -190,4 +189,4 @@ class testCommand(hazwaz.unittest.HazwazTestCase): if __name__ == '__main__': - unittest.main() + hazwaz.unittest.main() -- cgit v1.2.3