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 /tests | |
parent | eaeb5f7567d51d2e89875e9b51012a05fa3397de (diff) |
Start adding testing utilities
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_command.py | 37 |
1 files changed, 11 insertions, 26 deletions
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", |