def METHOD_NAME(self, module, mocks, module_globals):
    salt_dunder_dicts = self.salt_module_dunders + self.salt_module_dunders_optional
    allowed_salt_dunders = salt_dunder_dicts + self.salt_module_dunder_attributes
    for key in mocks:
        if key == "sys.modules":
            # sys.modules is addressed on another function
            continue
        if key.startswith("__"):
            if key in ("__init__", "__virtual__"):
                raise RuntimeError(
                    "No need to patch {!r}. Passed loader module dict: {}".format(
                        key,
                        self.setup_loader_modules,
                    )
                )
            elif key not in allowed_salt_dunders:
                raise RuntimeError(
                    "Don't know how to handle {!r}. Passed loader module dict: {}".format(
                        key,
                        self.setup_loader_modules,
                    )
                )
            elif key in salt_dunder_dicts and not hasattr(module, key):
                # Add the key as a dictionary attribute to the module so it can be patched by `patch.dict`'
                setattr(module, key, {})
                # Remove the added attribute after the test finishes
                self.addfinalizer(delattr, module, key)
        if not hasattr(module, key):
            # Set the key as an attribute so it can be patched
            setattr(module, key, None)
            # Remove the added attribute after the test finishes
            self.addfinalizer(delattr, module, key)
        module_globals[key] = mocks[key]
    # Patch the module!
    log.trace("Patching globals for %s; globals: %s", module, module_globals)
    patcher = patch.multiple(module, **module_globals)
    patcher.start()
    self.addfinalizer(patcher.stop)