diff --git a/httprunner/loader.py b/httprunner/loader.py index 6e897d75..0ea7e2db 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -4,7 +4,7 @@ import json import os import yaml -from httprunner import exceptions, logger, parser, utils +from httprunner import exceptions, logger, parser, utils, validator ############################################################################### ## file loader @@ -399,7 +399,7 @@ def load_testcases(path): testcases_list = [] else: - err_msg = "file not found: {}".format(path) + err_msg = "path not exist: {}".format(path) logger.log_error(err_msg) raise exceptions.FileNotFound(err_msg) @@ -412,5 +412,8 @@ def load(path): @param (str) path: testcase file/folder path @return (list) testcases list """ + if validator.is_testcases(path): + return path + _load_test_dependencies() return load_testcases(path) diff --git a/httprunner/task.py b/httprunner/task.py index b1aab280..964adcbb 100644 --- a/httprunner/task.py +++ b/httprunner/task.py @@ -177,10 +177,7 @@ def init_test_suites(path_or_testsets, mapping=None, http_client_session=None): mapping (dict): passed in variables mapping, it will override variables in config block """ - if not testcase.is_testsets(path_or_testsets): - testsets = loader.load(path_or_testsets) - else: - testsets = path_or_testsets + testsets = loader.load(path_or_testsets) # TODO: move comparator uniform here mapping = mapping or {} diff --git a/httprunner/testcase.py b/httprunner/testcase.py index f50c7058..b3f7ba8e 100644 --- a/httprunner/testcase.py +++ b/httprunner/testcase.py @@ -32,47 +32,6 @@ def extract_functions(content): return [] -def is_testset(data_structure): - """ check if data_structure is a testset - testset should always be in the following data structure: - { - "name": "desc1", - "config": {}, - "api": {}, - "testcases": [testcase11, testcase12] - } - """ - if not isinstance(data_structure, dict): - return False - - if "name" not in data_structure or "testcases" not in data_structure: - return False - - if not isinstance(data_structure["testcases"], list): - return False - - return True - -def is_testsets(data_structure): - """ check if data_structure is testset or testsets - testsets should always be in the following data structure: - testset_dict - or - [ - testset_dict_1, - testset_dict_2 - ] - """ - if not isinstance(data_structure, list): - return is_testset(data_structure) - - for item in data_structure: - if not is_testset(item): - return False - - return True - - def gen_cartesian_product(*args): """ generate cartesian product for lists @param diff --git a/httprunner/validator.py b/httprunner/validator.py new file mode 100644 index 00000000..0dbf0cb0 --- /dev/null +++ b/httprunner/validator.py @@ -0,0 +1,45 @@ +# encoding: utf-8 + +""" validate data format +TODO: refactor with JSON schema validate +""" + +def is_testcase(data_structure): + """ check if data_structure is a testcase + testcase should always be in the following data structure: + { + "name": "desc1", + "config": {}, + "api": {}, + "testcases": [testcase11, testcase12] + } + """ + if not isinstance(data_structure, dict): + return False + + if "name" not in data_structure or "testcases" not in data_structure: + return False + + if not isinstance(data_structure["testcases"], list): + return False + + return True + +def is_testcases(data_structure): + """ check if data_structure is testcase or testcases list + testsets should always be in the following data structure: + testset_dict + or + [ + testset_dict_1, + testset_dict_2 + ] + """ + if not isinstance(data_structure, list): + return is_testcase(data_structure) + + for item in data_structure: + if not is_testcase(item): + return False + + return True diff --git a/tests/test_testcase.py b/tests/test_testcase.py index c16583e2..f39f8f55 100644 --- a/tests/test_testcase.py +++ b/tests/test_testcase.py @@ -368,32 +368,3 @@ class TestcaseParserUnittest(unittest.TestCase): parsed_testcase["headers"]["sum"], 3 ) - - def test_is_testsets(self): - data_structure = "path/to/file" - self.assertFalse(testcase.is_testsets(data_structure)) - data_structure = ["path/to/file1", "path/to/file2"] - self.assertFalse(testcase.is_testsets(data_structure)) - - data_structure = { - "name": "desc1", - "config": {}, - "api": {}, - "testcases": ["testcase11", "testcase12"] - } - self.assertTrue(data_structure) - data_structure = [ - { - "name": "desc1", - "config": {}, - "api": {}, - "testcases": ["testcase11", "testcase12"] - }, - { - "name": "desc2", - "config": {}, - "api": {}, - "testcases": ["testcase21", "testcase22"] - } - ] - self.assertTrue(data_structure) diff --git a/tests/test_validator.py b/tests/test_validator.py new file mode 100644 index 00000000..5e1822da --- /dev/null +++ b/tests/test_validator.py @@ -0,0 +1,35 @@ +import unittest + +from httprunner import validator + + +class TestValidator(unittest.TestCase): + + def test_is_testcases(self): + data_structure = "path/to/file" + self.assertFalse(validator.is_testcases(data_structure)) + data_structure = ["path/to/file1", "path/to/file2"] + self.assertFalse(validator.is_testcases(data_structure)) + + data_structure = { + "name": "desc1", + "config": {}, + "api": {}, + "testcases": ["testcase11", "testcase12"] + } + self.assertTrue(data_structure) + data_structure = [ + { + "name": "desc1", + "config": {}, + "api": {}, + "testcases": ["testcase11", "testcase12"] + }, + { + "name": "desc2", + "config": {}, + "api": {}, + "testcases": ["testcase21", "testcase22"] + } + ] + self.assertTrue(data_structure)