def replaceSysModules(self, sysModules):
        """
        Replace sys.modules, for the duration of the test, with the given value.
        """
        originalSysModules = sys.modules.copy()
        def cleanUpSysModules():
            sys.modules.clear()
            sys.modules.update(originalSysModules)
        self.addCleanup(cleanUpSysModules)
        sys.modules.clear()
        sys.modules.update(sysModules)

        Replace sys.modules, for the duration of the test, with the given value.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/python/test/modules_helpers.py