I am trying to mock a function call which is inside the actual function I am trying to mock. I want to check that an algorithm is correct, but I have a draw_data()
inside the function, which is recieved as a parameter. I don’t want to use that in the unittests, however. My actual program is a algorithm visualiser, so the draw_data()
is supposed to display it on a gui.
Below is what I’ve tried, although it doesn’t seem to work. It should “ignore” the draw_data, as it is unneeded for the unittests. Recieved error on run: AttributeError: <module 'src.algorithms' from 'C:\Users\frowl\PycharmProjects\StructViz\src\algorithms.py'> does not have the attribute 'draw_data'
/src/tests/algorithms_test.py
:
class TestCase(unittest.TestCase):
@patch('src.algorithms.draw_data')
def test_bubble_sort(self, draw_data_mock):
draw_data_mock.return_value = 'mocked stuff'
data = [9, 2, 7, 5, 5, 23, 61, 9, 9, 11]
algorithms.bubble_sort(data, draw_data_mock, 0)
self.assertEqual([2, 5, 5, 7, 9, 9, 9, 11, 23, 61], data)
/src/algorithms.py
:
"""Bubble sort algorithm"""
data_len = len(data)
for i in range(data_len):
for j in range(0, data_len - i - 1):
if data[j] > data[j + 1]:
data[j], data[j + 1] = data[j + 1], data[j]
draw_data(data, ['Green' if x == j + 1 else 'Red' for x in range(len(data))])
time.sleep(speed)
/src/app.py
:
def draw_data(self, data: list[int], color: list[str]) -> None:
self.sort_canvas.delete('all')
canvas_height, canvas_width = 380, 500
x_width = canvas_width / (len(data) + 1)
offset, spacing = 30, 10
normalized_data = [i / max(data) for i in data]
Harbourheading is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.