Skip to content Skip to sidebar Skip to footer

How Do I Mock The Default Arguments Of A Function That Is Used By The Function I'm Testing?

I have this my_module.py: def _sub_function(do_the_thing=True): if do_the_thing: do_stuff() else: do_something_else() def main_function(): # do some s

Solution 1:

The only problem is you are trying to patch an attribute of a str object, not your function:

class TestMyModule(unittest.TestCase):
    @mock.patch.object(my_module._sub_function, "__defaults__", (False,))
    def test_main_function(self):
        print(my_module.main_function())

The AttributeError being raised doesn't make that clear, unfortunately.


Post a Comment for "How Do I Mock The Default Arguments Of A Function That Is Used By The Function I'm Testing?"