refactor: format code with black

This commit is contained in:
debugtalk
2022-04-30 15:06:31 +08:00
parent 737379b49f
commit be8c053ed8
52 changed files with 1809 additions and 2254 deletions

View File

@@ -1,4 +1,3 @@
import os
import unittest
@@ -7,14 +6,15 @@ from httprunner.loader import buildup
class TestModuleLoader(unittest.TestCase):
def test_filter_module_functions(self):
module_functions = buildup.load_module_functions(buildup)
self.assertIn("load_module_functions", module_functions)
self.assertNotIn("is_py3", module_functions)
def test_load_debugtalk_module(self):
project_mapping = buildup.load_project_data(os.path.join(os.getcwd(), "httprunner"))
project_mapping = buildup.load_project_data(
os.path.join(os.getcwd(), "httprunner")
)
self.assertNotIn("alter_response", project_mapping["functions"])
project_mapping = buildup.load_project_data(os.path.join(os.getcwd(), "tests"))
@@ -28,51 +28,38 @@ class TestModuleLoader(unittest.TestCase):
project_mapping = buildup.load_project_data("tests/data/demo_testcase.yml")
project_working_directory = project_mapping["PWD"]
debugtalk_functions = project_mapping["functions"]
self.assertEqual(
project_working_directory,
os.path.join(os.getcwd(), "tests")
)
self.assertEqual(project_working_directory, os.path.join(os.getcwd(), "tests"))
self.assertIn("gen_md5", debugtalk_functions)
project_mapping = buildup.load_project_data("tests/base.py")
project_working_directory = project_mapping["PWD"]
debugtalk_functions = project_mapping["functions"]
self.assertEqual(
project_working_directory,
os.path.join(os.getcwd(), "tests")
)
self.assertEqual(project_working_directory, os.path.join(os.getcwd(), "tests"))
self.assertIn("gen_md5", debugtalk_functions)
project_mapping = buildup.load_project_data("httprunner/__init__.py")
project_working_directory = project_mapping["PWD"]
debugtalk_functions = project_mapping["functions"]
self.assertEqual(
project_working_directory,
os.getcwd()
)
self.assertEqual(project_working_directory, os.getcwd())
self.assertEqual(debugtalk_functions, {})
class TestSuiteLoader(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.project_mapping = buildup.load_project_data(os.path.join(os.getcwd(), "tests"))
cls.project_mapping = buildup.load_project_data(
os.path.join(os.getcwd(), "tests")
)
cls.tests_def_mapping = buildup.tests_def_mapping
def test_load_teststep_api(self):
raw_test = {
"name": "create user (override).",
"api": "api/create_user.yml",
"variables": [
{"uid": "999"}
]
"variables": [{"uid": "999"}],
}
teststep = buildup.load_teststep(raw_test)
self.assertEqual(
"create user (override).",
teststep["name"]
)
self.assertEqual("create user (override).", teststep["name"])
self.assertIn("api_def", teststep)
api_def = teststep["api_def"]
self.assertEqual(api_def["name"], "create user")
@@ -82,15 +69,10 @@ class TestSuiteLoader(unittest.TestCase):
raw_test = {
"name": "setup and reset all (override).",
"testcase": "testcases/setup.yml",
"variables": [
{"device_sn": "$device_sn"}
]
"variables": [{"device_sn": "$device_sn"}],
}
testcase = buildup.load_teststep(raw_test)
self.assertEqual(
"setup and reset all (override).",
testcase["name"]
)
self.assertEqual("setup and reset all (override).", testcase["name"])
tests = testcase["testcase_def"]["teststeps"]
self.assertEqual(len(tests), 2)
self.assertEqual(tests[0]["name"], "get token (setup)")
@@ -106,7 +88,7 @@ class TestSuiteLoader(unittest.TestCase):
def test_load_test_file_testcase(self):
for loaded_content in [
buildup.load_test_file("tests/testcases/setup.yml"),
buildup.load_test_file("tests/testcases/setup.json")
buildup.load_test_file("tests/testcases/setup.json"),
]:
self.assertEqual(loaded_content["type"], "testcase")
self.assertIn("path", loaded_content)
@@ -118,7 +100,7 @@ class TestSuiteLoader(unittest.TestCase):
def test_load_test_file_testcase_v2(self):
for loaded_content in [
buildup.load_test_file("tests/testcases/setup.v2.yml"),
buildup.load_test_file("tests/testcases/setup.v2.json")
buildup.load_test_file("tests/testcases/setup.v2.json"),
]:
self.assertEqual(loaded_content["type"], "testcase")
self.assertIn("path", loaded_content)
@@ -130,38 +112,45 @@ class TestSuiteLoader(unittest.TestCase):
def test_load_test_file_testsuite(self):
for loaded_content in [
buildup.load_test_file("tests/testsuites/create_users.yml"),
buildup.load_test_file("tests/testsuites/create_users.json")
buildup.load_test_file("tests/testsuites/create_users.json"),
]:
self.assertEqual(loaded_content["type"], "testsuite")
testcases = loaded_content["testcases"]
self.assertEqual(len(testcases), 2)
self.assertIn('create user 1000 and check result.', testcases)
self.assertIn('testcase_def', testcases["create user 1000 and check result."])
self.assertIn("create user 1000 and check result.", testcases)
self.assertIn(
"testcase_def", testcases["create user 1000 and check result."]
)
self.assertEqual(
testcases["create user 1000 and check result."]["testcase_def"]["config"]["name"],
"create user and check result."
testcases["create user 1000 and check result."]["testcase_def"][
"config"
]["name"],
"create user and check result.",
)
def test_load_test_file_testsuite_v2(self):
for loaded_content in [
buildup.load_test_file("tests/testsuites/create_users.v2.yml"),
buildup.load_test_file("tests/testsuites/create_users.v2.json")
buildup.load_test_file("tests/testsuites/create_users.v2.json"),
]:
self.assertEqual(loaded_content["type"], "testsuite")
testcases = loaded_content["testcases"]
self.assertEqual(len(testcases), 2)
self.assertIn('create user 1000 and check result.', testcases)
self.assertIn('testcase_def', testcases["create user 1000 and check result."])
self.assertIn("create user 1000 and check result.", testcases)
self.assertIn(
"testcase_def", testcases["create user 1000 and check result."]
)
self.assertEqual(
testcases["create user 1000 and check result."]["testcase_def"]["config"]["name"],
"create user and check result."
testcases["create user 1000 and check result."]["testcase_def"][
"config"
]["name"],
"create user and check result.",
)
def test_load_tests_api_file(self):
path = os.path.join(
os.getcwd(), 'tests/api/create_user.yml')
path = os.path.join(os.getcwd(), "tests/api/create_user.yml")
tests_mapping = loader.load_cases(path)
project_mapping = tests_mapping["project_mapping"]
api_list = tests_mapping["apis"]
@@ -170,8 +159,7 @@ class TestSuiteLoader(unittest.TestCase):
def test_load_tests_testcase_file(self):
# absolute file path
path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase_hardcode.json')
path = os.path.join(os.getcwd(), "tests/data/demo_testcase_hardcode.json")
tests_mapping = loader.load_cases(path)
project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"]
@@ -180,7 +168,7 @@ class TestSuiteLoader(unittest.TestCase):
self.assertIn("get_sign", project_mapping["functions"])
# relative file path
path = 'tests/data/demo_testcase_hardcode.yml'
path = "tests/data/demo_testcase_hardcode.yml"
tests_mapping = loader.load_cases(path)
project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"]
@@ -189,91 +177,75 @@ class TestSuiteLoader(unittest.TestCase):
self.assertIn("get_sign", project_mapping["functions"])
def test_load_tests_testcase_file_2(self):
testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase.yml')
testcase_file_path = os.path.join(os.getcwd(), "tests/data/demo_testcase.yml")
tests_mapping = loader.load_cases(testcase_file_path)
testcases = tests_mapping["testcases"]
self.assertIsInstance(testcases, list)
self.assertEqual(testcases[0]["config"]["name"], '123t$var_a')
self.assertIn(
"sum_two",
tests_mapping["project_mapping"]["functions"]
self.assertEqual(testcases[0]["config"]["name"], "123t$var_a")
self.assertIn("sum_two", tests_mapping["project_mapping"]["functions"])
self.assertEqual(
testcases[0]["config"]["variables"]["var_c"], "${sum_two($var_a, $var_b)}"
)
self.assertEqual(
testcases[0]["config"]["variables"]["var_c"],
"${sum_two($var_a, $var_b)}"
)
self.assertEqual(
testcases[0]["config"]["variables"]["PROJECT_KEY"],
"${ENV(PROJECT_KEY)}"
testcases[0]["config"]["variables"]["PROJECT_KEY"], "${ENV(PROJECT_KEY)}"
)
def test_load_tests_testcase_file_with_api_ref(self):
path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase_layer.yml')
path = os.path.join(os.getcwd(), "tests/data/demo_testcase_layer.yml")
tests_mapping = loader.load_cases(path)
project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"]
self.assertIn('device_sn', testcases_list[0]["config"]["variables"])
self.assertIn("device_sn", testcases_list[0]["config"]["variables"])
self.assertIn("gen_md5", project_mapping["functions"])
self.assertIn("base_url", testcases_list[0]["config"])
test_dict0 = testcases_list[0]["teststeps"][0]
self.assertEqual(
"get token with $user_agent, $app_version",
test_dict0["name"]
)
self.assertEqual("get token with $user_agent, $app_version", test_dict0["name"])
self.assertIn("/api/get-token", test_dict0["api_def"]["request"]["url"])
self.assertIn(
{'eq': ['status_code', 200]},
test_dict0["validate"]
)
self.assertIn({"eq": ["status_code", 200]}, test_dict0["validate"])
def test_load_tests_testsuite_file_with_testcase_ref(self):
path = os.path.join(
os.getcwd(), 'tests/testsuites/create_users.yml')
path = os.path.join(os.getcwd(), "tests/testsuites/create_users.yml")
tests_mapping = loader.load_cases(path)
project_mapping = tests_mapping["project_mapping"]
testsuites_list = tests_mapping["testsuites"]
self.assertEqual("create users with uid", testsuites_list[0]["config"]["name"])
self.assertEqual(
"create users with uid",
testsuites_list[0]["config"]["name"]
)
self.assertEqual(
'${gen_random_string(15)}',
testsuites_list[0]["config"]["variables"]['device_sn']
"${gen_random_string(15)}",
testsuites_list[0]["config"]["variables"]["device_sn"],
)
self.assertIn(
"create user 1000 and check result.",
testsuites_list[0]["testcases"]
"create user 1000 and check result.", testsuites_list[0]["testcases"]
)
self.assertEqual(
testsuites_list[0]["testcases"]["create user 1000 and check result."]["testcase_def"]["config"]["name"],
"create user and check result."
testsuites_list[0]["testcases"]["create user 1000 and check result."][
"testcase_def"
]["config"]["name"],
"create user and check result.",
)
def test_load_tests_folder_path(self):
# absolute folder path
path = os.path.join(os.getcwd(), 'tests/data')
path = os.path.join(os.getcwd(), "tests/data")
tests_mapping = loader.load_cases(path)
testcase_list_1 = tests_mapping["testcases"]
self.assertGreater(len(testcase_list_1), 4)
# relative folder path
path = 'tests/data/'
path = "tests/data/"
tests_mapping = loader.load_cases(path)
testcase_list_2 = tests_mapping["testcases"]
self.assertEqual(len(testcase_list_1), len(testcase_list_2))
def test_load_tests_path_not_exist(self):
# absolute folder path
path = os.path.join(os.getcwd(), 'tests/data_not_exist')
path = os.path.join(os.getcwd(), "tests/data_not_exist")
with self.assertRaises(exceptions.FileNotFound):
loader.load_cases(path)
# relative folder path
path = 'tests/data_not_exist'
path = "tests/data_not_exist"
with self.assertRaises(exceptions.FileNotFound):
loader.load_cases(path)
@@ -281,5 +253,11 @@ class TestSuiteLoader(unittest.TestCase):
buildup.load_project_data(os.path.join(os.getcwd(), "tests"))
self.assertIn("gen_md5", self.project_mapping["functions"])
self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")
self.assertEqual(self.project_mapping["PWD"], os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
self.assertEqual(self.project_mapping["test_path"], os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
self.assertEqual(
self.project_mapping["PWD"],
os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
)
self.assertEqual(
self.project_mapping["test_path"],
os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
)

View File

@@ -4,7 +4,6 @@ from httprunner.loader import check
class TestLoaderCheck(unittest.TestCase):
def test_is_testcases(self):
data_structure = "path/to/file"
self.assertFalse(check.is_test_content(data_structure))
@@ -12,11 +11,7 @@ class TestLoaderCheck(unittest.TestCase):
self.assertFalse(check.is_test_content(data_structure))
data_structure = {
"project_mapping": {
"PWD": "XXXXX",
"functions": {},
"env": {}
},
"project_mapping": {"PWD": "XXXXX", "functions": {}, "env": {}},
"testcases": [
{ # testcase data structure
"config": {
@@ -27,19 +22,19 @@ class TestLoaderCheck(unittest.TestCase):
"teststeps": [
# test data structure
{
'name': 'test step desc1',
'variables': [], # optional
'extract': {}, # optional
'validate': [],
'request': {
"name": "test step desc1",
"variables": [], # optional
"extract": {}, # optional
"validate": [],
"request": {
"method": "GET",
"url": "https://docs.httprunner.org"
}
"url": "https://docs.httprunner.org",
},
},
# test_dict2 # another test dict
]
],
},
# testcase_dict_2 # another testcase dict
]
],
}
self.assertTrue(check.is_test_content(data_structure))

View File

@@ -7,11 +7,10 @@ from httprunner.loader.buildup import load_test_file
class TestFileLoader(unittest.TestCase):
def test_load_yaml_file_file_format_error(self):
yaml_tmp_file = "tests/data/tmp.yml"
# create empty yaml file
with open(yaml_tmp_file, 'w') as f:
with open(yaml_tmp_file, "w") as f:
f.write("")
with self.assertRaises(exceptions.FileFormatError):
@@ -20,7 +19,7 @@ class TestFileLoader(unittest.TestCase):
os.remove(yaml_tmp_file)
# create invalid format yaml file
with open(yaml_tmp_file, 'w') as f:
with open(yaml_tmp_file, "w") as f:
f.write("abc")
with self.assertRaises(exceptions.FileFormatError):
@@ -31,7 +30,7 @@ class TestFileLoader(unittest.TestCase):
def test_load_json_file_file_format_error(self):
json_tmp_file = "tests/data/tmp.json"
# create empty file
with open(json_tmp_file, 'w') as f:
with open(json_tmp_file, "w") as f:
f.write("")
with self.assertRaises(exceptions.FileFormatError):
@@ -40,7 +39,7 @@ class TestFileLoader(unittest.TestCase):
os.remove(json_tmp_file)
# create empty json file
with open(json_tmp_file, 'w') as f:
with open(json_tmp_file, "w") as f:
f.write("{}")
with self.assertRaises(exceptions.FileFormatError):
@@ -49,7 +48,7 @@ class TestFileLoader(unittest.TestCase):
os.remove(json_tmp_file)
# create invalid format json file
with open(json_tmp_file, 'w') as f:
with open(json_tmp_file, "w") as f:
f.write("abc")
with self.assertRaises(exceptions.FileFormatError):
@@ -58,62 +57,62 @@ class TestFileLoader(unittest.TestCase):
os.remove(json_tmp_file)
def test_load_testcases_bad_filepath(self):
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo')
testcase_file_path = os.path.join(os.getcwd(), "tests/data/demo")
with self.assertRaises(exceptions.FileNotFound):
load.load_file(testcase_file_path)
def test_load_json_testcases(self):
testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase_hardcode.json')
os.getcwd(), "tests/data/demo_testcase_hardcode.json"
)
testcases = load.load_file(testcase_file_path)
self.assertEqual(len(testcases), 3)
test = testcases[0]["test"]
self.assertIn('name', test)
self.assertIn('request', test)
self.assertIn('url', test['request'])
self.assertIn('method', test['request'])
self.assertIn("name", test)
self.assertIn("request", test)
self.assertIn("url", test["request"])
self.assertIn("method", test["request"])
def test_load_yaml_testcases(self):
testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase_hardcode.yml')
os.getcwd(), "tests/data/demo_testcase_hardcode.yml"
)
testcases = load.load_file(testcase_file_path)
self.assertEqual(len(testcases), 3)
test = testcases[0]["test"]
self.assertIn('name', test)
self.assertIn('request', test)
self.assertIn('url', test['request'])
self.assertIn('method', test['request'])
self.assertIn("name", test)
self.assertIn("request", test)
self.assertIn("url", test["request"])
self.assertIn("method", test["request"])
def test_load_csv_file_one_parameter(self):
csv_file_path = os.path.join(
os.getcwd(), 'tests/data/user_agent.csv')
csv_file_path = os.path.join(os.getcwd(), "tests/data/user_agent.csv")
csv_content = load.load_file(csv_file_path)
self.assertEqual(
csv_content,
[
{'user_agent': 'iOS/10.1'},
{'user_agent': 'iOS/10.2'},
{'user_agent': 'iOS/10.3'}
]
{"user_agent": "iOS/10.1"},
{"user_agent": "iOS/10.2"},
{"user_agent": "iOS/10.3"},
],
)
def test_load_csv_file_multiple_parameters(self):
csv_file_path = os.path.join(
os.getcwd(), 'tests/data/account.csv')
csv_file_path = os.path.join(os.getcwd(), "tests/data/account.csv")
csv_content = load.load_file(csv_file_path)
self.assertEqual(
csv_content,
[
{'username': 'test1', 'password': '111111'},
{'username': 'test2', 'password': '222222'},
{'username': 'test3', 'password': '333333'}
]
{"username": "test1", "password": "111111"},
{"username": "test2", "password": "222222"},
{"username": "test3", "password": "333333"},
],
)
def test_load_folder_files(self):
folder = os.path.join(os.getcwd(), 'tests')
file1 = os.path.join(os.getcwd(), 'tests', 'test_utils.py')
file2 = os.path.join(os.getcwd(), 'tests', 'api', 'reset_all.yml')
folder = os.path.join(os.getcwd(), "tests")
file1 = os.path.join(os.getcwd(), "tests", "test_utils.py")
file2 = os.path.join(os.getcwd(), "tests", "api", "reset_all.yml")
files = load.load_folder_files(folder, recursive=False)
self.assertEqual(files, [])
@@ -129,25 +128,25 @@ class TestFileLoader(unittest.TestCase):
self.assertEqual([], files)
def test_load_dot_env_file(self):
dot_env_path = os.path.join(
os.getcwd(), "tests", ".env"
)
dot_env_path = os.path.join(os.getcwd(), "tests", ".env")
env_variables_mapping = load.load_dot_env_file(dot_env_path)
self.assertIn("PROJECT_KEY", env_variables_mapping)
self.assertEqual(env_variables_mapping["UserName"], "debugtalk")
def test_load_custom_dot_env_file(self):
dot_env_path = os.path.join(
os.getcwd(), "tests", "data", "test.env"
)
dot_env_path = os.path.join(os.getcwd(), "tests", "data", "test.env")
env_variables_mapping = load.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")
self.assertEqual(
env_variables_mapping["content_type"], "application/json; charset=UTF-8"
)
def test_load_env_path_not_exist(self):
dot_env_path = os.path.join(
os.getcwd(), "tests", "data",
os.getcwd(),
"tests",
"data",
)
env_variables_mapping = load.load_dot_env_file(dot_env_path)
self.assertEqual(env_variables_mapping, {})

View File

@@ -1,4 +1,3 @@
import os
import unittest
@@ -7,7 +6,6 @@ from httprunner.loader import locate
class TestLoaderLocate(unittest.TestCase):
def test_locate_file(self):
with self.assertRaises(exceptions.FileNotFound):
locate.locate_file(os.getcwd(), "debugtalk.py")
@@ -18,23 +16,21 @@ class TestLoaderLocate(unittest.TestCase):
start_path = os.path.join(os.getcwd(), "tests")
self.assertEqual(
locate.locate_file(start_path, "debugtalk.py"),
os.path.join(
os.getcwd(), "tests/debugtalk.py"
)
os.path.join(os.getcwd(), "tests/debugtalk.py"),
)
self.assertEqual(
locate.locate_file("tests/", "debugtalk.py"),
os.path.join(os.getcwd(), "tests", "debugtalk.py")
os.path.join(os.getcwd(), "tests", "debugtalk.py"),
)
self.assertEqual(
locate.locate_file("tests", "debugtalk.py"),
os.path.join(os.getcwd(), "tests", "debugtalk.py")
os.path.join(os.getcwd(), "tests", "debugtalk.py"),
)
self.assertEqual(
locate.locate_file("tests/base.py", "debugtalk.py"),
os.path.join(os.getcwd(), "tests", "debugtalk.py")
os.path.join(os.getcwd(), "tests", "debugtalk.py"),
)
self.assertEqual(
locate.locate_file("tests/data/demo_testcase.yml", "debugtalk.py"),
os.path.join(os.getcwd(), "tests", "debugtalk.py")
os.path.join(os.getcwd(), "tests", "debugtalk.py"),
)