Skip to content

Commit

Permalink
Merge pull request #32 from LucasAndradeDias/unit-tests
Browse files Browse the repository at this point in the history
Added more tests for more scenarios
  • Loading branch information
LucasAndradeDias authored Oct 3, 2023
2 parents c732778 + 4f0300a commit 4cd7fd7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
1 change: 0 additions & 1 deletion tests/data/module_testing_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ def func2(self):
def test2():
a = test()
return

52 changes: 49 additions & 3 deletions tests/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,29 @@ def setUp(self):
self.parser = Trace()

def tearDown(self):
self.parser.detailed_data = collections.defaultdict()
self.parser.detailed_data = collections.defaultdict(
lambda: {"call": 0, "return": 0, "line": 0}
)
self.parser.functions_flow = collections.OrderedDict()

def test_run_positive_input(self):
"""
Given a valid module path and a callable function "test",
When Trace.run is called with these parameters,
Then it should trace the function and produce the expected trace result.
"""
module_path = self.mock_path + "/module_testing_1.py"
self.parser.run(module_path, "test")
expected_trace = r"Trace object\nrunned objects:\n __init__ + \n func1 + \n func2 + \n "
self.assertEqual(expected_trace, repr(self.parser))

def test_run_positive_input_with_parameters(self):
"""
Given a valid module path, a callable function "numberToIp",
and function parameters ["000"],
When Trace.run is called with these parameters,
Then it should trace the function and produce the expected trace result.
"""
module_path = self.mock_path + "/module_testing_2.py"
self.parser.run(module_path, "numberToIp", ["000"])

Expand All @@ -34,9 +47,42 @@ def test_run_positive_input_with_parameters(self):

self.assertEqual(expected_trace, repr(self.parser))

def test_run_negative_no_module(self):
def test_trace_run_negative_no_module(self):
"""
Given an invalid module path,
When Trace.run is called with these parameters,
Then it should raise an exception with an appropriate error message.
"""
with self.assertRaises(Exception) as error:
self.parser.run("not_valid_path", "object")
self.assertTrue("Error on the execution of the module" in str(error.exception))

def test_trace_run_negative_non_callable_object(self):
"""
Given a valid module path and a non-callable object "non_callable",
When Trace.run is called with these parameters,
Then it should raise an exception with an appropriate error message.
"""
module_path = (
self.mock_path + "/module_testing_2.py"
) # Contains a non-callable object
with self.assertRaises(Exception) as error:
self.parser.run("not valid path", "object")
self.parser.run(module_path, "non_callable")
self.assertEqual(
"Object 'non_callable' not found in module" in str(error.exception)
)

def test_trace_run_positive_multiple_runs(self):
"""
Given a valid module path and a callable function "test",
When Trace.run is called twice with these parameters,
Then it should trace the functions individually and produce the expected trace result for each run.
"""
module_path = self.mock_path + "/module_testing_1.py"
self.parser.run(module_path, "test")
self.parser.run(module_path, "test2")
expected_trace = r"Trace object\nrunned objects:\n __init__ + \n func1 + \n func2 + \n test2 + \n "
self.assertEqual(expected_trace, repr(self.parser))


if __name__ == "__main__":
Expand Down

0 comments on commit 4cd7fd7

Please sign in to comment.