def test_parse_parameters_custom_function(self): parameters = [ {"user_agent": "${get_user_agent()}"}, {"app_version": "${gen_app_version()}"}, {"username-password": "******"}, {"username2-password2": "${get_account_in_tuple()}"} ] dot_env_path = os.path.join( os.getcwd(), "tests", ".env" ) loader.load_dot_env_file(dot_env_path) from tests import debugtalk cartesian_product_parameters = parser.parse_parameters( parameters, functions_mapping=loader.load_module_functions(debugtalk) ) self.assertIn( { 'user_agent': 'iOS/10.1', 'app_version': '2.8.5', 'username': '******', 'password': '******', 'username2': 'user1', 'password2': '111111' }, cartesian_product_parameters ) self.assertEqual( len(cartesian_product_parameters), 2 * 2 * 2 * 2 )
def load_tests(): try: with open(P('testcase.json'), 'r') as tj: items = json.load(tj) except FileNotFoundError: return {}, [] try: load_dot_env_file(P('.env')) except FileNotFoundError: pass config_obj = {} test_objs = [] for item in items: if 'config' in item: config_obj = item elif 'test' in item: test_objs.append(item) tests = [] for idx, test_obj in enumerate(test_objs): name = f'{idx}.json' with open(P(name), 'w') as fp: json.dump([config_obj, test_obj], fp) for _ in range(test_obj['test']['weight']): tests.append(name) config = config_obj['config'] return config, tests
def test_load_env_path_not_exist(self): loader.project_working_directory = os.path.join( os.getcwd(), "tests", "data", ) loader.load_dot_env_file()
def test_load_env_path_not_exist(self): dot_env_path = os.path.join( os.getcwd(), "tests", "data", ) with self.assertRaises(exceptions.FileNotFound): loader.load_dot_env_file(dot_env_path)
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) loader.load_dot_env_file(dot_env_path) kwargs.setdefault("resultclass", HtmlTestResult) self.runner = unittest.TextTestRunner(**kwargs)
def test_get_module_item_variables(self): dot_env_path = os.path.join(os.getcwd(), "tests", ".env") loader.load_dot_env_file(dot_env_path) from tests import debugtalk module_mapping = loader.load_python_module(debugtalk) SECRET_KEY = loader.get_module_item(module_mapping, "variables", "SECRET_KEY") self.assertTrue(validator.is_variable(("SECRET_KEY", SECRET_KEY))) self.assertEqual(SECRET_KEY, "DebugTalk") with self.assertRaises(exceptions.VariableNotFound): loader.get_module_item(module_mapping, "variables", "SECRET_KEY2")
def test_load_custom_dot_env_file(self): dot_env_path = os.path.join(os.getcwd(), "tests", "data", "test.env") env_variables_mapping = loader.load_dot_env_file(dot_env_path) self.assertIn("PROJECT_KEY", env_variables_mapping) self.assertEqual(env_variables_mapping["UserName"], "test") self.assertEqual(env_variables_mapping["content_type"], "application/json; charset=UTF-8")
def test_parse_parameters_custom_function(self): parameters = [{ "app_version": "${gen_app_version()}" }, { "username-password": "******" }] testcase_path = os.path.join(os.getcwd(), "tests/data/demo_parameters.yml") dot_env_path = os.path.join(os.getcwd(), "tests", ".env") loader.load_dot_env_file(dot_env_path) from tests import debugtalk debugtalk_module = loader.load_python_module(debugtalk) cartesian_product_parameters = parser.parse_parameters( parameters, debugtalk_module["variables"], debugtalk_module["functions"]) self.assertEqual(len(cartesian_product_parameters), 2 * 2)
def test_load_dot_env_file(self): dot_env_path = os.path.join( os.getcwd(), "tests", ".env" ) env_variables_mapping = loader.load_dot_env_file(dot_env_path) self.assertIn("PROJECT_KEY", env_variables_mapping) self.assertEqual(env_variables_mapping["UserName"], "debugtalk")
def test_load_env_path_not_exist(self): dot_env_path = os.path.join( os.getcwd(), "tests", "data", ) env_variables_mapping = loader.load_dot_env_file(dot_env_path) self.assertEqual(env_variables_mapping, {})
def test_load_dot_env_file(self): loader.project_working_directory = os.path.join( os.getcwd(), "tests", ) env_variables_mapping = loader.load_dot_env_file() self.assertIn("PROJECT_KEY", env_variables_mapping) self.assertEqual(env_variables_mapping["UserName"], "debugtalk")
def test_load_custom_dot_env_file(self): loader.project_working_directory = os.path.join( os.getcwd(), "tests", ) loader.dot_env_path = "tests/data/test.env" env_variables_mapping = loader.load_dot_env_file() self.assertIn("PROJECT_KEY", env_variables_mapping) self.assertEqual(env_variables_mapping["UserName"], "test") loader.dot_env_path = None
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 test_load_env_path_not_exist(self): with self.assertRaises(exceptions.FileNotFound): loader.load_dot_env_file("not_exist.env")
def test_load_dot_env_file(self): env_variables_mapping = loader.load_dot_env_file("tests/data/test.env") self.assertIn("PROJECT_KEY", env_variables_mapping) self.assertEqual(env_variables_mapping["UserName"], "debugtalk")
def test_load_dot_env_file(self): self.assertNotIn("PROJECT_KEY", os.environ) loader.load_dot_env_file("tests/data/test.env") self.assertIn("PROJECT_KEY", os.environ) self.assertEqual(os.environ["UserName"], "debugtalk")