Monday, 24 August 2020

Unittest mock patch fails randomly within loop

Can't provide much context given the complexity, but I'm hoping for some insight/thought-provoking questions as to why this is happening.

I'm testing a process which loads files into a database, so I'm patching the credentials for a database connection using unittest.mock.patch to use test and not production credentials. We have a series of mocks that are applied as a contextmanager, simplified version here:

from contextlib import ExitStack

def context_stack(contexts):
    stack = ExitStack()
    for context in contexts:
        stack.enter_context(context)
    return stack

def patch_mocks():
    mocks = [
        patch('db_config.ReadWrite', db_mocks.ReadWrite),
        patch('db_config.ReadWrite', db_mocks.ReadWrite)
    ]
    return context_stack(mocks)

It gets used as such (simplified):

with patch_mocks():
    LoadFiles(file_list)

LoadFiles will iterate over each file in file_list and attempt to insert the contents into the database. The underlying methods connect to the database using db_config.ReadWrite but of course they are patched by db_mocks.ReadWrite. This works pretty consistently except, seemingly very randomly, it will fail as it tries to instead use db_config.ReadWrite when trying to create the connection.

So for example, there could be a hundred files, and it will patch the most of them successfully, but it will randomly stop using the patch halfway through and fail the test. What conditions/variables could be causing this patch to not be applied? Is there a limit to the number of patches that can be applied? Should it be applied in another way?



from Unittest mock patch fails randomly within loop

No comments:

Post a Comment