def test_filter_module_functions(self): imported_module = utils.get_imported_module("httprunner.utils") self.assertIn("PYTHON_VERSION", dir(imported_module)) functions_dict = utils.filter_module(imported_module, "function") self.assertIn("filter_module", functions_dict) self.assertNotIn("PYTHON_VERSION", functions_dict)
def import_module_items(self, modules, level="testcase"): """ import modules and bind all functions within the context """ sys.path.insert(0, os.getcwd()) for module_name in modules: imported_module = utils.get_imported_module(module_name) imported_functions_dict = utils.filter_module(imported_module, "function") self.__update_context_functions_config(level, imported_functions_dict) imported_variables_dict = utils.filter_module(imported_module, "variable") self.bind_variables(imported_variables_dict, level)
def test_validators(self): imported_module = utils.get_imported_module("httprunner.built_in") functions_mapping = utils.filter_module(imported_module, "function") functions_mapping["equals"](None, None) functions_mapping["equals"](1, 1) functions_mapping["equals"]("abc", "abc") with self.assertRaises(AssertionError): functions_mapping["equals"]("123", 123) functions_mapping["less_than"](1, 2) functions_mapping["less_than_or_equals"](2, 2) functions_mapping["greater_than"](2, 1) functions_mapping["greater_than_or_equals"](2, 2) functions_mapping["not_equals"](123, "123") functions_mapping["length_equals"]("123", 3) functions_mapping["length_greater_than"]("123", 2) functions_mapping["length_greater_than_or_equals"]("123", 3) functions_mapping["contains"]("123abc456", "3ab") functions_mapping["contains"](['1', '2'], "1") functions_mapping["contains"]({'a': 1, 'b': 2}, "a") functions_mapping["contained_by"]("3ab", "123abc456") functions_mapping["regex_match"]("123abc456", "^123\w+456$") with self.assertRaises(AssertionError): functions_mapping["regex_match"]("123abc456", "^12b.*456$") functions_mapping["startswith"]("abc123", "ab") functions_mapping["startswith"]("123abc", 12) functions_mapping["startswith"](12345, 123) functions_mapping["endswith"]("abc123", 23) functions_mapping["endswith"]("123abc", "abc") functions_mapping["endswith"](12345, 45) functions_mapping["type_match"](580509390, int) functions_mapping["type_match"](580509390, "int") functions_mapping["type_match"]([], list) functions_mapping["type_match"]([], "list") functions_mapping["type_match"]([1], "list") functions_mapping["type_match"]({}, "dict") functions_mapping["type_match"]({"a": 1}, "dict")
def import_module_items(self, modules, level="testcase"): """ import modules and bind all functions within the context """ #在程序的生命周期内引入工作目录路径到环境变量 sys.path.insert(0, os.getcwd()) for module_name in modules: #导入并返回指定的模块 imported_module = utils.get_imported_module(module_name) #将导入的模块里,类型是function的筛选出来,存入字典imported_functions_dict中 imported_functions_dict = utils.filter_module( imported_module, "function") #将imported_functions_dict字典更新到列表testcase_functions_config #和实例testcase_parser的列表bind_functions self.__update_context_functions_config(level, imported_functions_dict) # 将导入的模块里,类型是variable的筛选出来,存入字典imported_variables_dict中 imported_variables_dict = utils.filter_module( imported_module, "variable") self.bind_variables(imported_variables_dict, level)
def test_validators(self): imported_module = utils.get_imported_module("httprunner.built_in") functions_mapping = utils.filter_module(imported_module, "function") functions_mapping["equals"](None, None) functions_mapping["equals"](1, 1) functions_mapping["equals"]("abc", "abc") with self.assertRaises(AssertionError): functions_mapping["equals"]("123", 123) functions_mapping["less_than"](1, 2) functions_mapping["less_than_or_equals"](2, 2) functions_mapping["greater_than"](2, 1) functions_mapping["greater_than_or_equals"](2, 2) functions_mapping["not_equals"](123, "123") functions_mapping["length_equals"]("123", 3) functions_mapping["length_greater_than"]("123", 2) functions_mapping["length_greater_than_or_equals"]("123", 3) functions_mapping["contains"]("123abc456", "3ab") functions_mapping["contains"](['1', '2'], "1") functions_mapping["contains"]({'a':1, 'b':2}, "a") functions_mapping["contained_by"]("3ab", "123abc456") functions_mapping["regex_match"]("123abc456", "^123\w+456$") with self.assertRaises(AssertionError): functions_mapping["regex_match"]("123abc456", "^12b.*456$") functions_mapping["startswith"]("abc123", "ab") functions_mapping["startswith"]("123abc", 12) functions_mapping["startswith"](12345, 123) functions_mapping["endswith"]("abc123", 23) functions_mapping["endswith"]("123abc", "abc") functions_mapping["endswith"](12345, 45)
def setUp(self): imported_module = utils.get_imported_module("httprunner.built_in") self.functions_mapping = utils.filter_module(imported_module, "function")
def import_requires(self, modules): """ import required modules dynamically """ for module_name in modules: globals()[module_name] = utils.get_imported_module(module_name)
def test_get_imported_module(self): imported_module = utils.get_imported_module("os") self.assertIn("walk", dir(imported_module))
def load_test_file(file_path): """ load testcase file or suite file @param file_path: absolute valid file path file_path should be in format below: [ { "config": { "name": "", "def": "suite_order()", "request": {} } }, { "test": { "name": "add product to cart", "api": "api_add_cart()", "validate": [] } }, { "test": { "name": "checkout cart", "request": {}, "validate": [] } } ] @return testset dict { "name": "desc1", "config": {}, "testcases": [testcase11, testcase12] } """ testset = { "name": "", "config": { "path": file_path }, "testcases": [] # TODO: rename to tests } for item in FileUtils.load_file(file_path): if not isinstance(item, dict) or len(item) != 1: raise exception.FileFormatError( "Testcase format error: {}".format(file_path)) key, test_block = item.popitem() if not isinstance(test_block, dict): raise exception.FileFormatError( "Testcase format error: {}".format(file_path)) if key == "config": testset["config"].update(test_block) testset["name"] = test_block.get("name", "") #add by zhengchun, can define runner if "runner" in test_block: # module_name, cls_name=item["config"]["runner"].split(".")[0:2] s = test_block["runner"] inx = s.rfind(".") if (inx <= 0): raise exception.ParamsError("runner format error[%s]" % (s)) module_name = s[0:inx] cls_name = s[inx + 1:] imported_module = utils.get_imported_module(module_name) ip_module_cls = getattr(imported_module, cls_name) testset["runner"] = ip_module_cls else: #defalut runner imported_module = utils.get_imported_module("runner") ip_module_cls = getattr(imported_module, "Runner") testset["runner"] = ip_module_cls elif key == "test": if "api" in test_block: ref_call = test_block["api"] def_block = TestcaseLoader._get_block_by_name( ref_call, "api") TestcaseLoader._override_block(def_block, test_block) testset["testcases"].append(test_block) elif "suite" in test_block: ref_call = test_block["suite"] block = TestcaseLoader._get_block_by_name( ref_call, "suite") testset["testcases"].extend(block["testcases"]) else: testset["testcases"].append(test_block) else: logger.log_warning( "unexpected block key: {}. block key should only be 'config' or 'test'." .format(key)) return testset