Skip to content

Commit 3564af5

Browse files
[TESTING] Refactor duplicate code into helper function (- WIP #222 -)
* moved common logic of checkoing test output into a helper function. Changes in file tests/test_usage.py: * simplified `test_Usage_Error_WHEN_the_help_command_is_called` * simplified `test_Usage_Error_WHEN_the_help_sub_command_is_called` * new helper function `_validate_help_output`
1 parent 344208a commit 3564af5

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

tests/test_usage.py

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,33 @@ def test_prints_version_WHEN_called_GIVEN_version_argument(self):
661661
theResult = False
662662
self.assertTrue(theResult, str("""Could Not find version from multicast --version"""))
663663

664+
def _validate_help_output(self, args):
665+
"""
666+
Helper method to validate help command output.
667+
668+
Args:
669+
args: List of command arguments to execute
670+
671+
Returns:
672+
bool: True if validation passes, False otherwise
673+
"""
674+
theOutputtxt = context.checkPythonCommand(args, stderr=subprocess.STDOUT)
675+
subResult = False
676+
try:
677+
if isinstance(theOutputtxt, bytes):
678+
theOutputtxt = theOutputtxt.decode('utf8')
679+
except UnicodeDecodeError:
680+
theOutputtxt = str(repr(bytes(theOutputtxt)))
681+
self.assertIsNotNone(theOutputtxt)
682+
self.assertIn(str("usage:"), str(theOutputtxt))
683+
if str("usage:") in str(theOutputtxt):
684+
subResult = True
685+
else:
686+
context.debugUnexpectedOutput(
687+
str("usage:"), str(theOutputtxt), self._thepython
688+
)
689+
return subResult
690+
664691
def test_Usage_Error_WHEN_the_help_command_is_called(self):
665692
"""Test case for multicast* --help."""
666693
theResult = False
@@ -674,24 +701,9 @@ def test_Usage_Error_WHEN_the_help_command_is_called(self):
674701
str("multicast{}").format(str(test_case)),
675702
str("--help")
676703
]
677-
theOutputtxt = context.checkPythonCommand(args, stderr=subprocess.STDOUT)
678-
context.debugBlob(theOutputtxt)
679-
# now test it
680-
try:
681-
if isinstance(theOutputtxt, bytes):
682-
theOutputtxt = theOutputtxt.decode('utf8')
683-
except UnicodeDecodeError:
684-
theOutputtxt = str(repr(bytes(theOutputtxt)))
685-
# or simply:
686-
self.assertIsNotNone(theOutputtxt)
687-
self.assertIn(str("""usage:"""), str(theOutputtxt))
688-
if (str("""usage:""") in str(theOutputtxt)):
689-
theResult = True or theResult
690-
else:
691-
theResult = False
692-
context.debugUnexpectedOutput(
693-
str("usage:"), str(theOutputtxt), self._thepython
694-
)
704+
with self.subTest(args=args):
705+
if self._validate_help_output(args):
706+
theResult = True
695707
except Exception as err:
696708
context.debugtestError(err)
697709
err = None
@@ -736,24 +748,9 @@ def test_Usage_Error_WHEN_the_help_sub_command_is_called(self):
736748
str(test_case_i.mode).format(str(test_case_i.command)),
737749
str("--help")
738750
]
739-
theOutputtxt = context.checkPythonCommand(args, stderr=subprocess.STDOUT)
740-
context.debugBlob(theOutputtxt)
741-
# now test it
742-
try:
743-
if isinstance(theOutputtxt, bytes):
744-
theOutputtxt = theOutputtxt.decode('utf8')
745-
except UnicodeDecodeError:
746-
theOutputtxt = str(repr(bytes(theOutputtxt)))
747-
# or simply:
748-
self.assertIsNotNone(theOutputtxt)
749-
self.assertIn(str("""usage:"""), str(theOutputtxt))
750-
if (str("""usage:""") in str(theOutputtxt)):
751-
theResult = True and theResult
752-
else:
753-
theResult = False
754-
context.debugUnexpectedOutput(
755-
str("usage:"), str(theOutputtxt), self._thepython
756-
)
751+
with self.subTest(args=args):
752+
if not self._validate_help_output(args):
753+
theResult = False
757754
except Exception as err:
758755
context.debugtestError(err)
759756
err = None

0 commit comments

Comments
 (0)