Skip to content

Commit

Permalink
[STYLE] Renamed tests by new convention ( closes #34 )
Browse files Browse the repository at this point in the history
  • Loading branch information
reactive-firewall committed Mar 3, 2022
1 parent 8643d4d commit 1ddaa5a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 47 deletions.
65 changes: 58 additions & 7 deletions tests/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,71 @@ def checkPythonFuzzing(args=[None], stderr=None):
"""function for backend subprocess check_output command"""
theOutput = None
try:
if args is None or args is [None]:
if args is None or args is [None]: # pragma: no branch
theOutput = subprocess.check_output(["exit 1 ; #"])
else:
if str("coverage") in args[0]:
args = checkCovCommand(args)
theOutput = subprocess.check_output(args, stderr=stderr)
except Exception as err:
except BaseException as err: # pragma: no branch
theOutput = None
raise RuntimeError(err)
theOutput = checkStrOrByte(theOutput)
return theOutput


def debugBlob(blob=None):
"""Helper function to debug unexpected outputs.
Especialy usefull for cross-python testing where output may differ
yet may be from the same logical data.
Meta Testing:
First setup test fixtures by importing test context.
>>> import tests.context
>>>
>>> norm_fixture = "Example Sample"
>>> othr_fixture = \"""'Example Sample'\"""
>>>
Testcase 1: function should have a output.
>>> debugBlob(norm_fixture) #doctest: -DONT_ACCEPT_BLANKLINE, +ELLIPSIS
<BLANKLINE>
String:
"
Example Sample
"
<BLANKLINE>
Data:
"
'Example Sample'
"
<BLANKLINE>
True
>>>
Testcase 2: function should have a output even with bad input.
>>> debugBlob(othr_fixture) #doctest: -DONT_ACCEPT_BLANKLINE, +ELLIPSIS
<BLANKLINE>
String:
"
...Example Sample...
"
<BLANKLINE>
Data:
"
...'Example Sample'...
"
<BLANKLINE>
True
>>>
"""
try:
print(__BLANK)
print(str("String:"))
Expand All @@ -333,8 +384,7 @@ def debugBlob(blob=None):


def debugtestError(someError):
"""
Helper function to debug unexpected outputs.
"""Helper function to debug unexpected outputs.
Meta Testing:
Expand Down Expand Up @@ -384,7 +434,9 @@ def debugtestError(someError):

def check_exec_command_has_output(test_case, someArgs):
"""Test case for command output != None.
returns True if has output and False otherwise."""
returns True if has output and False otherwise.
"""
theResult = False
fail_msg_fixture = str("""Expecting output: CLI test had no output.""")
try:
Expand All @@ -407,8 +459,7 @@ def check_exec_command_has_output(test_case, someArgs):


def debugUnexpectedOutput(expectedOutput, actualOutput, thepython):
"""
Helper function to debug unexpected outputs.
"""Helper function to debug unexpected outputs.
Meta Testing:
Expand Down
111 changes: 71 additions & 40 deletions tests/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Tests of integration by usage.
Meta
tests.test_usage.BasicIntegrationTestSuite
Integration Tests - Fixtures:
Test fixtures by importing test context.
>>> import tests.test_usage as test_usage
>>> import tests
>>>
>>> tests.test_usage.MulticastTestSuite #doctest: -DONT_ACCEPT_BLANKLINE, +ELLIPSIS
<class...tests.test_usage.MulticastTestSuite...>
>>>
>>> tests.test_usage.BasicIntegrationTestSuite #doctest: -DONT_ACCEPT_BLANKLINE, +ELLIPSIS
<class...tests.test_usage.BasicIntegrationTestSuite...>
>>>
"""


try:
import sys
if sys.__name__ is None: # pragma: no branch
Expand Down Expand Up @@ -48,7 +75,9 @@ class MulticastTestSuite(context.BasicUsageTestSuite):

__module__ = """tests.test_usage"""

def test_multicast_insane_none(self):
__name__ = """tests.test_usage.MulticastTestSuite"""

def test_aborts_WHEN_calling_multicast_GIVEN_invalid_tools(self):
"""Tests the imposible state for CLI tools given bad tools"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(JUNK) == error""")
Expand All @@ -62,7 +91,7 @@ def test_multicast_insane_none(self):
theResult = False
self.assertTrue(theResult, fail_fixture)

def test_multicast_message_arg_main(self):
def test_say_is_stable_WHEN_calling_multicast_GIVEN_say_tool(self):
"""Tests the message argument for expected syntax given simple args"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(SAY, message) == error""")
Expand All @@ -77,7 +106,7 @@ def test_multicast_message_arg_main(self):
theResult = False
self.assertTrue(theResult, fail_fixture)

def test_multicast_hear_invalid_arg_main(self):
def test_hear_aborts_WHEN_calling_multicast_GIVEN_invalid_args(self):
"""Tests the message argument for failure given invalid input"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(HEAR, junk) != 2""")
Expand All @@ -94,7 +123,7 @@ def test_multicast_hear_invalid_arg_main(self):
theResult = False
self.assertTrue(theResult, fail_fixture)

def test_multicast_hexdump_arg_main(self):
def test_hear_aborts_WHEN_calling_multicast_GIVEN_invalid_tool(self):
"""Tests the hexdump argument for failure given future tools"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(HEAR, hex) == error""")
Expand All @@ -108,7 +137,7 @@ def test_multicast_hexdump_arg_main(self):
theResult = False
self.assertTrue(theResult, fail_fixture)

def test_multicast_invalid_main(self):
def test_noop_stable_WHEN_calling_multicast_GIVEN_noop_args(self):
"""Tests the NOOP state for multicast given bad input"""
theResult = False
fail_fixture = str("""multicast.__main__.main(NOOP) == empty""")
Expand All @@ -121,7 +150,7 @@ def test_multicast_invalid_main(self):
theResult = False
self.assertTrue(theResult, fail_fixture)

def test_multicast_help_arg_main(self):
def test_help_works_WHEN_calling_multicast_GIVEN_help_tool(self):
"""Tests the HELP argument for help usage"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(HELP, []) == error""")
Expand All @@ -134,15 +163,15 @@ def test_multicast_help_arg_main(self):
theResult = False
self.assertTrue(theResult, fail_fixture)

def test_multicast_message_send_recv(self):
def test_hear_works_WHEN_say_works(self):
"""Tests the basic send and recv test"""
theResult = False
fail_fixture = str("""SAY --> HEAR == error""")
try:
_fixture_SAY_args = [
"""--port=19991""",
"""--mcast-group='224.0.0.1'""",
"""--message='test'"""
"""--message='test message'"""
]
_fixture_HEAR_args = [
"""--port=19991""",
Expand All @@ -160,6 +189,7 @@ def test_multicast_message_send_recv(self):
raise unittest.SkipTest(fail_fixture)
p.join()
self.assertIsNotNone(p.exitcode)
self.assertEqual(int(p.exitcode), int(0))
theResult = True
except Exception as err:
context.debugtestError(err)
Expand All @@ -169,24 +199,13 @@ def test_multicast_message_send_recv(self):
self.assertTrue(theResult, fail_fixture)


def debugIfNoneResult(thepython, theArgs, theOutput):
"""In case you need it."""
try:
if (str(theOutput) is not None):
theResult = True
else:
theResult = False
context.debugUnexpectedOutput(theOutput, None, thepython)
except Exception:
theResult = False
return theResult


class BasicIntegrationTestSuite(context.BasicUsageTestSuite):
"""Basic functional test cases."""

__module__ = """tests.test_usage"""

__name__ = """tests.test_usage.BasicIntegrationTestSuite"""

def setUp(self):
super(self.__class__, self).setUp()
if (self._thepython is None):
Expand Down Expand Up @@ -263,16 +282,8 @@ def test_prints_version_WHEN_called_GIVEN_version_argument(self):
str("--version")
]
theOutputtxt = context.checkPythonCommand(args, stderr=subprocess.STDOUT)
# now test it
try:
if isinstance(theOutputtxt, bytes):
theOutputtxt = theOutputtxt.decode('utf8')
except UnicodeDecodeError:
theOutputtxt = str(repr(bytes(theOutputtxt)))
# ADD REAL VERSION TEST HERE
theResult = debugIfNoneResult(self._thepython, args, theOutputtxt)
# or simply:
self.assertIsNotNone(theOutputtxt)
context.check_exec_command_has_output(self, args)
theResult = (theOutputtxt is not None)
except Exception as err:
context.debugtestError(err)
err = None
Expand Down Expand Up @@ -312,7 +323,7 @@ def test_Usage_Error_WHEN_the_help_command_is_called(self):
theResult = False
self.assertTrue(theResult, str("""Could Not find usage from multicast --help"""))

def test_profile_template_case(self):
def test_profile_WHEN_the_noop_command_is_called(self):
"""Test case template for profiling"""
theResult = False
if (self._thepython is not None):
Expand Down Expand Up @@ -345,9 +356,35 @@ def test_profile_template_case(self):
theResult = False
assert theResult

def test_invalid_Error_WHEN_cli_called_GIVEN_bad_input(self):
def test_stable_WHEN_the_noop_command_is_called(self):
"""Test case template for profiling"""
theResult = False
if (self._thepython is not None):
try:
for test_case in ["NOOP"]:
args = [
str(self._thepython),
str("-m"),
str("multicast"),
str("{}").format(
str(
test_case
)
)
]
context.checkPythonFuzzing(args, stderr=None)
# now test it
theResult = True
except Exception as err:
context.debugtestError(err)
err = None
del err
theResult = False
self.assertTrue(theResult, str("""Could Not handle multicast NOOP"""))

def test_invalid_Error_WHEN_cli_called_GIVEN_bad_input(self):
"""Test case template for invalid input to multicast CLI."""
theResult = False
if (self._thepython is not None):
try:
for test_case in ["BAdInPut", "1", "exit"]:
Expand All @@ -362,17 +399,11 @@ def test_invalid_Error_WHEN_cli_called_GIVEN_bad_input(self):
)
]
theOutputtxt = context.checkPythonCommand(args, stderr=subprocess.STDOUT)
# now test it
try:
if isinstance(theOutputtxt, bytes):
theOutputtxt = theOutputtxt.decode('utf8')
except UnicodeDecodeError:
theOutputtxt = str(repr(bytes(theOutputtxt)))
theResult = debugIfNoneResult(self._thepython, args, theOutputtxt)
# or simply:
self.assertIsNotNone(theOutputtxt)
self.assertIn(str("invalid choice:"), str(theOutputtxt))
self.assertIn(str(test_case), str(theOutputtxt))
theResult = True
except Exception as err:
context.debugtestError(err)
err = None
Expand Down

0 comments on commit 1ddaa5a

Please sign in to comment.