def test_set_os_environ(self): self.assertNotIn("abc", os.environ) variables_mapping = { "abc": "123" } utils.set_os_environ(variables_mapping) self.assertIn("abc", os.environ) self.assertEqual(os.environ["abc"], "123")
def __init__(self, **kwargs): """ initialize test runner @param (dict) kwargs: key-value arguments used to initialize TextTestRunner - resultclass: HtmlTestResult or TextTestResult - failfast: False/True, stop the test run on the first error or failure. - dot_env_path: .env file path """ dot_env_path = kwargs.pop("dot_env_path", None) utils.set_os_environ(loader.load_dot_env_file(dot_env_path)) kwargs.setdefault("resultclass", HtmlTestResult) self.runner = unittest.TextTestRunner(**kwargs)
def load_dot_env_file(): """ load .env file, .env file should be located in project working directory by default. If dot_env_path is specified, it will be loaded instead. Returns: dict: environment variables mapping { "UserName": "******", "Password": "******", "PROJECT_KEY": "ABCDEFGH" } Raises: exceptions.FileFormatError: If env file format is invalid. """ path = dot_env_path or os.path.join(project_working_directory, ".env") if not os.path.isfile(path): if dot_env_path: logger.log_error(".env file not exist: {}".format(dot_env_path)) sys.exit(1) else: logger.log_debug( ".env file not exist in: {}".format(project_working_directory)) return {} logger.log_info("Loading environment variables from {}".format(path)) env_variables_mapping = {} with io.open(path, 'r', encoding='utf-8') as fp: for line in fp: # maxsplit=1 if "=" in line: variable, value = line.split("=", 1) elif ":" in line: variable, value = line.split(":", 1) else: raise exceptions.FileFormatError(".env format error") env_variables_mapping[variable.strip()] = value.strip() project_mapping["env"] = env_variables_mapping utils.set_os_environ(env_variables_mapping) return env_variables_mapping
def load_dot_env_file(dot_env_path: Text) -> Dict: """load .env file. Args: dot_env_path (str): .env file path Returns: dict: environment variables mapping { "UserName": "******", "Password": "******", "PROJECT_KEY": "ABCDEFGH" } Raises: exceptions.FileFormatError: If .env file format is invalid. """ if not os.path.isfile(dot_env_path): return {} logger.info(f"Loading environment variables from {dot_env_path}") env_variables_mapping = {} with open(dot_env_path, mode="rb") as fp: for line in fp: # maxsplit=1 line = line.strip() if not len(line) or line.startswith(b"#"): continue if b"=" in line: variable, value = line.split(b"=", 1) elif b":" in line: variable, value = line.split(b":", 1) else: raise exceptions.FileFormatError(".env format error") env_variables_mapping[variable.strip().decode( "utf-8")] = value.strip().decode("utf-8") utils.set_os_environ(env_variables_mapping) return env_variables_mapping
def load_dot_env_file(dot_env_path): """ load .env file. Args: dot_env_path (str): .env file path Returns: dict: environment variables mapping { "UserName": "******", "Password": "******", "PROJECT_KEY": "ABCDEFGH" } Raises: exceptions.FileFormatError: If .env file format is invalid. """ if not os.path.isfile(dot_env_path): return {} logger.log_info( "Loading environment variables from {}".format(dot_env_path)) env_variables_mapping = {} with io.open(dot_env_path, 'r', encoding='utf-8') as fp: for line in fp: # maxsplit=1 if "=" in line: variable, value = line.split("=", 1) elif ":" in line: variable, value = line.split(":", 1) elif line.startswith("#") or line.startswith("\n"): pass else: raise exceptions.FileFormatError(".env format error") env_variables_mapping[variable.strip()] = value.strip() utils.set_os_environ(env_variables_mapping) return env_variables_mapping
def __init__(self, **kwargs): """ initialize HttpRunner. Args: kwargs (dict): key-value arguments used to initialize TextTestRunner. Commonly used arguments: resultclass (class): HtmlTestResult or TextTestResult failfast (bool): False/True, stop the test run on the first error or failure. dot_env_path (str): .env file path. Attributes: project_mapping (dict): save project loaded api/testcases, environments and debugtalk.py module. """ dot_env_path = kwargs.pop("dot_env_path", None) loader.load_dot_env_file(dot_env_path) loader.load_project_tests("tests") # TODO: remove tests self.project_mapping = loader.project_mapping utils.set_os_environ(self.project_mapping["env"]) kwargs.setdefault("resultclass", HtmlTestResult) self.runner = unittest.TextTestRunner(**kwargs)
def load_env_file(): ''' load .env file, .env file should be located in project working directory. Returns: dict: enviroment variables mapping { 'username':'******', 'password':'******', 'PROJECT_KEY':'ABCDEFGH' } Raises: exceptions.FileFormatError: If env file format is invalid. ''' path = os.path.join(project_working_directory, '.env') if not os.path.isfile(path): logger.log_debug( f'.env file not exist in: {project_working_directory}') return {} logger.log_info(f'Loading enviroment variables from {path}') env_variables_mapping = {} with open(path, 'r', encoding='utf-8') as fp: for line in fp: if '=' in line: variable, value = line.split('=') elif ':' in line: variable, value = line.split(':') else: raise exceptions.FileFormatError('.env format error') env_variables_mapping[variable.strip()] = value.strip() project_mapping['env'] = env_variables_mapping utils.set_os_environ(env_variables_mapping) return env_variables_mapping