summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_command.py37
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",