def create_patch(test_case: unittest.TestCase, *args, **kwargs) -> Any: """Wrap asynctest.patch such that it will be unpatched as part of test case cleanup.""" patcher = asynctest.patch(*args, **kwargs) mock_obj = patcher.start() test_case.addCleanup(patcher.stop) return mock_obj
def use_cm(cm_factory: Callable[[], ContextManager[_T]], testcase: TestCase) -> _T: cm = cm_factory() ret = cm.__enter__() testcase.addCleanup(cm.__exit__, None, None, None) return ret
def setup_data_path(test_class: TestCase): test_class.addCleanup(mock.patch.stopall) mock.patch("tools.bots.pi._DATA_PATH", _DATA_PATH_TEST).start() _remove_data_folder() os.mkdir(_DATA_PATH_TEST)
def setup_with_context_manager(testcase: unittest.TestCase, cm: Any) -> Any: """Use a contextmanager to setUp a test case.""" val = cm.__enter__() testcase.addCleanup(cm.__exit__, None, None, None) return val
def setup_data_path(test_class: TestCase): test_class.addCleanup(mock.patch.stopall) mock.patch("tools.bots._DATA_PATH", _DATA_PATH_TEST).start() _remove_data_folder() os.mkdir(_DATA_PATH_TEST)