def generate_file(config: dict): -> None:
...
my_obj = create_my_obj(config)
my_obj_binary = my_obj.export()
write_file(my_obj_binary, "some_path", mode="wb")
def create_my_obj(config: dict) -> MyObj:
# some configuration checking etc.
...
...
return MyObj(opt1="something", opt2="something_else", value="VALUE_TO_MOCK")
# TEST
def test_my_obj():
config = ... # get config data from json or whatever...
generate_file(config)
# load a golden sample binary
...
# load binary generated by my file
...
# compare those two
assert golden_sample_binary == generated_binary
Above is a snippet of my code I'm testing. My code generates a binary file and I need to compare this binary file with a golden sample to test, whether my code generates the file properly. I need to "mock" one input argument to a very specific value in MyObj() invoked in create_my_obj(). I'm using unittest library, unfortunately I'm not able to figure this out.
Thanks for any hints
MyObj.attribute = "value"inlinemy_obj = create_my_obj(config)that just hasmy_obj.value = "1234"which means it will be baked into the export for you.generate_file( )can't be modified this way, as it's part of the "API" to say. I need to do it fromtest_my_obj. So my idea was, "redefine" MyObj in a way, where value wouldn't be None by default, but '1234' and I thought mocking MyObj could work, but can't figure out how.Patch the function's __defaults__ attributesection of this article might help mdawar.dev/blog/mock-python-default-function-argument