aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2022-08-01 16:53:02 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2022-08-01 16:53:02 +0200
commit0466c3406b2dc1e41fc32bfd77f1b8af22a5c69f (patch)
tree9e5d6d73095adec7d01ce13d924fa9d7c004e949
parent7df64ba14453685ddd6940fd4e1d3f69c8db507f (diff)
Add facilities to run self-tests from a command.
-rw-r--r--hazwaz/command.py3
-rw-r--r--hazwaz/unittest.py25
-rw-r--r--tests/test_command.py3
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()