diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-07-17 14:39:35 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-07-17 14:39:35 +0200 |
commit | be8226f0ddb1f412cd8d608ee0270b894b814a99 (patch) | |
tree | ea3e5fbd793156caeb9699baa3aeb37df7e7d010 | |
parent | eaeb5f7567d51d2e89875e9b51012a05fa3397de (diff) |
Start adding testing utilities
-rw-r--r-- | docs/source/reference/hazwaz.rst | 1 | ||||
-rw-r--r-- | docs/source/reference/hazwaz.unittest.rst | 7 | ||||
-rw-r--r-- | hazwaz/unittest.py | 29 | ||||
-rw-r--r-- | tests/test_command.py | 37 |
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", |