[Python] How can I apply side effects to a function of an instance that is created in a...

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a custom class in the file MyClass.py

    class MyClass:
    def__init__(self):
    self.fetched_data = False

    def get_data(self):
    self.fetched_data = True
    return [1, 2, 3]


    I import and use that class in the file function_to_test.py

    from MyClass import MyClass

    def do_data_calculations():
    my_class = MyClass()
    data = my_class.get_data()
    print(data)
    print(f"Data fetched: {my_class.fetched_data}")
    return True


    This is the code I got and I cannot change it. I am writing unit tests for it using the unittest library, and since I cannot execute the get_data functions properly I have to mock them. To do this I have this code

    from function_to_test import do_data_calculations
    import unittest
    from unittest.mock import patch

    def get_data_side_effect(self):
    self.fetched_data = True
    return [10, 9, 8]

    @patch('function_to_test.MyClass.get_data')
    class TestMyClass(unittest.TestCase):
    def test_greet(self, mock_get_data):
    mock_get_data.side_effect = get_data_side_effect
    do_data_calculations()
    self.assertTrue(mock_get_data.called)

    if __name__ == "__main__":
    unittest.main()


    running the unittest I get the error

    Traceback (most recent call last):
    File "C:\Users\user4219\AppData\Local\miniconda3\envs\azure\lib\unittest\mock.py", line 1379, in patched
    return func(*newargs, **newkeywargs)
    File "c:\Users\user4219\source\projects\mockingtest\test_function.py", line 14, in test_greet
    do_data_calculations()
    File "c:\Users\user4219\source\projects\mockingtest\function_to_test.py", line 5, in do_data_calculations
    data = my_class.get_data()
    File "C:\Users\user4219\AppData\Local\miniconda3\envs\azure\lib\unittest\mock.py", line 1114, in call
    return self._mock_call(*args, **kwargs)
    File "C:\Users\user4219\AppData\Local\miniconda3\envs\azure\lib\unittest\mock.py", line 1118, in _mock_call
    return self._execute_mock_call(*args, **kwargs)
    File "C:\Users\user4219\AppData\Local\miniconda3\envs\azure\lib\unittest\mock.py", line 1179, in _execute_mock_call
    result = effect(*args, **kwargs)
    TypeError: get_data_side_effect() missing 1 required positional argument: 'self'


    When I read the documentation it seems to be the case that the side effect gets the parameters with which the function is called. But when I try it it does not seem to be the case. If I ommit the 'self' parameter (and the self.fetched_data = True line), It works fine and it prints the [10, 9, 8] data. When I use the @patch line at the function level, using the new=callable parameter, I get it to work, but since I have to write loads of tests patching this same function, I want to keep it at the class level (and I cannot use the new=callable line since each function has to call it with different parameters).

    How can I change this to be able to apply the side effects to the class instance?

    Continue reading...

Compartilhe esta Página