mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-04 13:06:57 +08:00
refactor load_testcases function name
This commit is contained in:
@@ -236,7 +236,7 @@ def load_api_file(file_path):
|
|||||||
|
|
||||||
|
|
||||||
def load_test_file(file_path):
|
def load_test_file(file_path):
|
||||||
""" load testcase file or suite file
|
""" load testcase file or testsuite file
|
||||||
@param file_path: absolute valid file path
|
@param file_path: absolute valid file path
|
||||||
file_path should be in format below:
|
file_path should be in format below:
|
||||||
[
|
[
|
||||||
@@ -355,28 +355,28 @@ def _get_test_definition(name, ref_type):
|
|||||||
return block
|
return block
|
||||||
|
|
||||||
|
|
||||||
def load_testsets_by_path(path):
|
def load_testcases(path):
|
||||||
""" load testcases from file path
|
""" load testcases from file path
|
||||||
@param path: path could be in several type
|
@param path: path could be in several type
|
||||||
- absolute/relative file path
|
- absolute/relative file path
|
||||||
- absolute/relative folder path
|
- absolute/relative folder path
|
||||||
- list/set container with file(s) and/or folder(s)
|
- list/set container with file(s) and/or folder(s)
|
||||||
@return testcase sets list, each testset is corresponding to a file
|
@return testcases list, each testcase is corresponding to a file
|
||||||
[
|
[
|
||||||
testset_dict_1,
|
testcase_dict_1,
|
||||||
testset_dict_2
|
testcase_dict_2
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
if isinstance(path, (list, set)):
|
if isinstance(path, (list, set)):
|
||||||
testsets = []
|
testcases_list = []
|
||||||
|
|
||||||
for file_path in set(path):
|
for file_path in set(path):
|
||||||
testset = load_testsets_by_path(file_path)
|
testcases = load_testcases(file_path)
|
||||||
if not testset:
|
if not testcases:
|
||||||
continue
|
continue
|
||||||
testsets.extend(testset)
|
testcases_list.extend(testcases)
|
||||||
|
|
||||||
return testsets
|
return testcases_list
|
||||||
|
|
||||||
if not os.path.isabs(path):
|
if not os.path.isabs(path):
|
||||||
path = os.path.join(os.getcwd(), path)
|
path = os.path.join(os.getcwd(), path)
|
||||||
@@ -386,21 +386,22 @@ def load_testsets_by_path(path):
|
|||||||
|
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
files_list = load_folder_files(path)
|
files_list = load_folder_files(path)
|
||||||
testcases_list = load_testsets_by_path(files_list)
|
testcases_list = load_testcases(files_list)
|
||||||
|
|
||||||
elif os.path.isfile(path):
|
elif os.path.isfile(path):
|
||||||
try:
|
try:
|
||||||
testset = load_test_file(path)
|
testcase = load_test_file(path)
|
||||||
if testset["testcases"] or testset["api"]:
|
if testcase["testcases"]:
|
||||||
testcases_list = [testset]
|
testcases_list = [testcase]
|
||||||
else:
|
else:
|
||||||
testcases_list = []
|
testcases_list = []
|
||||||
except exceptions.FileFormatError:
|
except exceptions.FileFormatError:
|
||||||
testcases_list = []
|
testcases_list = []
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.log_error(u"file not found: {}".format(path))
|
err_msg = "file not found: {}".format(path)
|
||||||
testcases_list = []
|
logger.log_error(err_msg)
|
||||||
|
raise exceptions.FileNotFound(err_msg)
|
||||||
|
|
||||||
testcases_cache_mapping[path] = testcases_list
|
testcases_cache_mapping[path] = testcases_list
|
||||||
return testcases_list
|
return testcases_list
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ def init_test_suites(path_or_testsets, mapping=None, http_client_session=None):
|
|||||||
"""
|
"""
|
||||||
if not testcase.is_testsets(path_or_testsets):
|
if not testcase.is_testsets(path_or_testsets):
|
||||||
loader.load_test_dependencies()
|
loader.load_test_dependencies()
|
||||||
testsets = loader.load_testsets_by_path(path_or_testsets)
|
testsets = loader.load_testcases(path_or_testsets)
|
||||||
else:
|
else:
|
||||||
testsets = path_or_testsets
|
testsets = path_or_testsets
|
||||||
|
|
||||||
|
|||||||
@@ -240,7 +240,7 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
# absolute file path
|
# absolute file path
|
||||||
path = os.path.join(
|
path = os.path.join(
|
||||||
os.getcwd(), 'tests/data/demo_testset_hardcode.json')
|
os.getcwd(), 'tests/data/demo_testset_hardcode.json')
|
||||||
testset_list = loader.load_testsets_by_path(path)
|
testset_list = loader.load_testcases(path)
|
||||||
self.assertEqual(len(testset_list), 1)
|
self.assertEqual(len(testset_list), 1)
|
||||||
self.assertIn("path", testset_list[0]["config"])
|
self.assertIn("path", testset_list[0]["config"])
|
||||||
self.assertEqual(testset_list[0]["config"]["path"], path)
|
self.assertEqual(testset_list[0]["config"]["path"], path)
|
||||||
@@ -249,7 +249,7 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
|
|
||||||
# relative file path
|
# relative file path
|
||||||
path = 'tests/data/demo_testset_hardcode.yml'
|
path = 'tests/data/demo_testset_hardcode.yml'
|
||||||
testset_list = loader.load_testsets_by_path(path)
|
testset_list = loader.load_testcases(path)
|
||||||
self.assertEqual(len(testset_list), 1)
|
self.assertEqual(len(testset_list), 1)
|
||||||
self.assertIn("path", testset_list[0]["config"])
|
self.assertIn("path", testset_list[0]["config"])
|
||||||
self.assertIn(path, testset_list[0]["config"]["path"])
|
self.assertIn(path, testset_list[0]["config"]["path"])
|
||||||
@@ -261,7 +261,7 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
os.path.join(os.getcwd(), 'tests/data/demo_testset_hardcode.json'),
|
os.path.join(os.getcwd(), 'tests/data/demo_testset_hardcode.json'),
|
||||||
'tests/data/demo_testset_hardcode.yml'
|
'tests/data/demo_testset_hardcode.yml'
|
||||||
]
|
]
|
||||||
testset_list = loader.load_testsets_by_path(path)
|
testset_list = loader.load_testcases(path)
|
||||||
self.assertEqual(len(testset_list), 2)
|
self.assertEqual(len(testset_list), 2)
|
||||||
self.assertEqual(len(testset_list[0]["testcases"]), 3)
|
self.assertEqual(len(testset_list[0]["testcases"]), 3)
|
||||||
self.assertEqual(len(testset_list[1]["testcases"]), 3)
|
self.assertEqual(len(testset_list[1]["testcases"]), 3)
|
||||||
@@ -279,12 +279,12 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
loader.load_test_dependencies()
|
loader.load_test_dependencies()
|
||||||
# absolute folder path
|
# absolute folder path
|
||||||
path = os.path.join(os.getcwd(), 'tests/data')
|
path = os.path.join(os.getcwd(), 'tests/data')
|
||||||
testset_list_1 = loader.load_testsets_by_path(path)
|
testset_list_1 = loader.load_testcases(path)
|
||||||
self.assertGreater(len(testset_list_1), 4)
|
self.assertGreater(len(testset_list_1), 4)
|
||||||
|
|
||||||
# relative folder path
|
# relative folder path
|
||||||
path = 'tests/data/'
|
path = 'tests/data/'
|
||||||
testset_list_2 = loader.load_testsets_by_path(path)
|
testset_list_2 = loader.load_testcases(path)
|
||||||
self.assertEqual(len(testset_list_1), len(testset_list_2))
|
self.assertEqual(len(testset_list_1), len(testset_list_2))
|
||||||
|
|
||||||
# list/set container with file(s)
|
# list/set container with file(s)
|
||||||
@@ -292,33 +292,33 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
os.path.join(os.getcwd(), 'tests/data'),
|
os.path.join(os.getcwd(), 'tests/data'),
|
||||||
'tests/data/'
|
'tests/data/'
|
||||||
]
|
]
|
||||||
testset_list_3 = loader.load_testsets_by_path(path)
|
testset_list_3 = loader.load_testcases(path)
|
||||||
self.assertEqual(len(testset_list_3), 2 * len(testset_list_1))
|
self.assertEqual(len(testset_list_3), 2 * len(testset_list_1))
|
||||||
|
|
||||||
def test_load_testcases_by_path_not_exist(self):
|
def test_load_testcases_by_path_not_exist(self):
|
||||||
# absolute folder path
|
# absolute folder path
|
||||||
path = os.path.join(os.getcwd(), 'tests/data_not_exist')
|
path = os.path.join(os.getcwd(), 'tests/data_not_exist')
|
||||||
testset_list_1 = loader.load_testsets_by_path(path)
|
with self.assertRaises(exceptions.FileNotFound):
|
||||||
self.assertEqual(testset_list_1, [])
|
loader.load_testcases(path)
|
||||||
|
|
||||||
# relative folder path
|
# relative folder path
|
||||||
path = 'tests/data_not_exist'
|
path = 'tests/data_not_exist'
|
||||||
testset_list_2 = loader.load_testsets_by_path(path)
|
with self.assertRaises(exceptions.FileNotFound):
|
||||||
self.assertEqual(testset_list_2, [])
|
loader.load_testcases(path)
|
||||||
|
|
||||||
# list/set container with file(s)
|
# list/set container with file(s)
|
||||||
path = [
|
path = [
|
||||||
os.path.join(os.getcwd(), 'tests/data_not_exist'),
|
os.path.join(os.getcwd(), 'tests/data_not_exist'),
|
||||||
'tests/data_not_exist/'
|
'tests/data_not_exist/'
|
||||||
]
|
]
|
||||||
testset_list_3 = loader.load_testsets_by_path(path)
|
with self.assertRaises(exceptions.FileNotFound):
|
||||||
self.assertEqual(testset_list_3, [])
|
loader.load_testcases(path)
|
||||||
|
|
||||||
def test_load_testcases_by_path_layered(self):
|
def test_load_testcases_by_path_layered(self):
|
||||||
loader.load_test_dependencies()
|
loader.load_test_dependencies()
|
||||||
path = os.path.join(
|
path = os.path.join(
|
||||||
os.getcwd(), 'tests/data/demo_testset_layer.yml')
|
os.getcwd(), 'tests/data/demo_testset_layer.yml')
|
||||||
testsets_list = loader.load_testsets_by_path(path)
|
testsets_list = loader.load_testcases(path)
|
||||||
self.assertIn("variables", testsets_list[0]["config"])
|
self.assertIn("variables", testsets_list[0]["config"])
|
||||||
self.assertIn("request", testsets_list[0]["config"])
|
self.assertIn("request", testsets_list[0]["config"])
|
||||||
self.assertIn("request", testsets_list[0]["testcases"][0])
|
self.assertIn("request", testsets_list[0]["testcases"][0])
|
||||||
|
|||||||
@@ -380,7 +380,7 @@ class TestRunner(ApiServerUnittest):
|
|||||||
def test_run_testcase_with_empty_header(self):
|
def test_run_testcase_with_empty_header(self):
|
||||||
testcase_file_path = os.path.join(
|
testcase_file_path = os.path.join(
|
||||||
os.getcwd(), 'tests/data/test_bugfix.yml')
|
os.getcwd(), 'tests/data/test_bugfix.yml')
|
||||||
testsets = loader.load_testsets_by_path(testcase_file_path)
|
testsets = loader.load_testcases(testcase_file_path)
|
||||||
testset = testsets[0]
|
testset = testsets[0]
|
||||||
config_dict_headers = testset["config"]["request"]["headers"]
|
config_dict_headers = testset["config"]["request"]["headers"]
|
||||||
test_dict_headers = testset["testcases"][0]["request"]["headers"]
|
test_dict_headers = testset["testcases"][0]["request"]["headers"]
|
||||||
|
|||||||
Reference in New Issue
Block a user