aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/source/reference/hazwaz.rst1
-rw-r--r--docs/source/reference/hazwaz.unittest.rst7
-rw-r--r--hazwaz/unittest.py29
-rw-r--r--tests/test_command.py37
4 files changed, 48 insertions, 26 deletions
diff --git a/docs/source/reference/hazwaz.rst b/docs/source/reference/hazwaz.rst
index 32a91d1..fd849c2 100644
--- a/docs/source/reference/hazwaz.rst
+++ b/docs/source/reference/hazwaz.rst
@@ -14,3 +14,4 @@ Submodules
hazwaz.command
hazwaz.mixins
+ hazwaz.unittest
diff --git a/docs/source/reference/hazwaz.unittest.rst b/docs/source/reference/hazwaz.unittest.rst
new file mode 100644
index 0000000..8c09131
--- /dev/null
+++ b/docs/source/reference/hazwaz.unittest.rst
@@ -0,0 +1,7 @@
+hazwaz.unittest module
+======================
+
+.. automodule:: hazwaz.unittest
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/hazwaz/unittest.py b/hazwaz/unittest.py
new file mode 100644
index 0000000..2b28cdc
--- /dev/null
+++ b/hazwaz/unittest.py
@@ -0,0 +1,29 @@
+import contextlib
+import io
+import sys
+import unittest
+
+
+class HazwazTestCase(unittest.TestCase):
+ def run_with_argv(self, cmd, argv: list[str]) -> 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
diff --git a/tests/test_command.py b/tests/test_command.py
index cbb7aad..96b36de 100644
--- a/tests/test_command.py
+++ b/tests/test_command.py
@@ -1,10 +1,8 @@
-import contextlib
-import io
import logging
-import sys
import unittest
import hazwaz
+import hazwaz.unittest
class MySubCommand(hazwaz.Command):
@@ -76,20 +74,7 @@ class MyCommandWithNoVerbose(hazwaz.MainCommand):
)
-class testCommand(unittest.TestCase):
- def _run_with_argv(self, cmd, argv):
- 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(hazwaz.unittest.HazwazTestCase):
def test_description(self):
cmd = MyCommand()
self.assertEqual(
@@ -134,18 +119,18 @@ class testCommand(unittest.TestCase):
def test_run(self):
cmd = MyCommand()
cmd_help = cmd.parser.format_help()
- stream = self._run_with_argv(cmd, ["mycommand"])
+ stream = self.run_with_argv(cmd, ["mycommand"])
self.assertEqual(stream["stdout"].getvalue(), cmd_help)
def test_run_with_option(self):
cmd = MyCommand()
cmd_help = cmd.parser.format_help()
- stream = self._run_with_argv(cmd, [
+ stream = self.run_with_argv(cmd, [
"mycommand",
"--verbose",
])
self.assertEqual(stream["stdout"].getvalue(), cmd_help)
- stream = self._run_with_argv(cmd, [
+ stream = self.run_with_argv(cmd, [
"mycommand",
"--debug",
])
@@ -153,12 +138,12 @@ class testCommand(unittest.TestCase):
def test_run_subcommand(self):
cmd = MyCommand()
- stream = self._run_with_argv(cmd, ["mycommand", "mysubcommand"])
+ stream = self.run_with_argv(cmd, ["mycommand", "mysubcommand"])
self.assertEqual(stream["stdout"].getvalue(), "Hello World\n")
def test_run_subcommand_with_option(self):
cmd = MyCommand()
- stream = self._run_with_argv(cmd, [
+ stream = self.run_with_argv(cmd, [
"mycommand",
"mysubcommand",
"--bar",
@@ -168,13 +153,13 @@ class testCommand(unittest.TestCase):
def test_run_no_verbose(self):
cmd = MyCommandWithNoVerbose()
cmd_help = cmd.parser.format_help()
- stream = self._run_with_argv(cmd, ["mycommand"])
+ stream = self.run_with_argv(cmd, ["mycommand"])
self.assertEqual(stream["stdout"].getvalue(), cmd_help)
def test_logging_regular(self):
cmd = MyCommand()
with self.assertLogs():
- stream = self._run_with_argv(cmd, [
+ stream = self.run_with_argv(cmd, [
"mycommand",
"loggingsubcommand",
])
@@ -184,7 +169,7 @@ class testCommand(unittest.TestCase):
def test_logging_verbose(self):
cmd = MyCommand()
with self.assertLogs():
- stream = self._run_with_argv(cmd, [
+ stream = self.run_with_argv(cmd, [
"mycommand",
"--verbose",
"loggingsubcommand",
@@ -195,7 +180,7 @@ class testCommand(unittest.TestCase):
def test_logging_debug(self):
cmd = MyCommand()
with self.assertLogs():
- stream = self._run_with_argv(cmd, [
+ stream = self.run_with_argv(cmd, [
"mycommand",
"--debug",
"loggingsubcommand",