mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
fix circular reference in utils and testcase module
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
#coding: utf-8
|
||||
import json
|
||||
|
||||
try:
|
||||
FileNotFoundError = FileNotFoundError
|
||||
except NameError:
|
||||
FileNotFoundError = IOError
|
||||
|
||||
try:
|
||||
JSONDecodeError = json.decoder.JSONDecodeError
|
||||
except AttributeError:
|
||||
JSONDecodeError = ValueError
|
||||
|
||||
class MyBaseError(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import ast
|
||||
import codecs
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
import yaml
|
||||
from ate import exception, utils
|
||||
|
||||
variable_regexp = r"\$([\w_]+)"
|
||||
@@ -16,6 +19,38 @@ test_def_overall_dict = {
|
||||
testcases_cache_mapping = {}
|
||||
|
||||
|
||||
def load_yaml_file(yaml_file):
|
||||
""" load yaml file and check file content format
|
||||
"""
|
||||
with codecs.open(yaml_file, 'r+', encoding='utf-8') as stream:
|
||||
yaml_content = yaml.load(stream)
|
||||
check_format(yaml_file, yaml_content)
|
||||
return yaml_content
|
||||
|
||||
def load_json_file(json_file):
|
||||
""" load json file and check file content format
|
||||
"""
|
||||
with codecs.open(json_file, encoding='utf-8') as data_file:
|
||||
try:
|
||||
json_content = json.load(data_file)
|
||||
except exception.JSONDecodeError:
|
||||
err_msg = "JSONDecodeError: JSON file format error: {}".format(json_file)
|
||||
logging.error(err_msg)
|
||||
raise exception.FileFormatError(err_msg)
|
||||
|
||||
check_format(json_file, json_content)
|
||||
return json_content
|
||||
|
||||
def load_tests(testcase_file_path):
|
||||
file_suffix = os.path.splitext(testcase_file_path)[1]
|
||||
if file_suffix == '.json':
|
||||
return load_json_file(testcase_file_path)
|
||||
elif file_suffix in ['.yaml', '.yml']:
|
||||
return load_yaml_file(testcase_file_path)
|
||||
else:
|
||||
# '' or other suffix
|
||||
return []
|
||||
|
||||
def extract_variables(content):
|
||||
""" extract all variable names from content, which is in format $variable
|
||||
@param (str) content
|
||||
@@ -191,7 +226,7 @@ def load_test_file(file_path):
|
||||
"api": {},
|
||||
"testcases": []
|
||||
}
|
||||
tests_list = utils.load_tests(file_path)
|
||||
tests_list = load_tests(file_path)
|
||||
|
||||
for item in tests_list:
|
||||
for key in item:
|
||||
|
||||
36
ate/utils.py
36
ate/utils.py
@@ -1,9 +1,7 @@
|
||||
import codecs
|
||||
import hashlib
|
||||
import hmac
|
||||
import imp
|
||||
import importlib
|
||||
import json
|
||||
import logging
|
||||
import os.path
|
||||
import random
|
||||
@@ -13,7 +11,7 @@ import types
|
||||
from collections import OrderedDict
|
||||
|
||||
import yaml
|
||||
from ate import exception, testcase
|
||||
from ate import exception
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
try:
|
||||
@@ -41,38 +39,6 @@ def get_sign(*args):
|
||||
sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest()
|
||||
return sign
|
||||
|
||||
def load_yaml_file(yaml_file):
|
||||
""" load yaml file and check file content format
|
||||
"""
|
||||
with codecs.open(yaml_file, 'r+', encoding='utf-8') as stream:
|
||||
yaml_content = yaml.load(stream)
|
||||
testcase.check_format(yaml_file, yaml_content)
|
||||
return yaml_content
|
||||
|
||||
def load_json_file(json_file):
|
||||
""" load json file and check file content format
|
||||
"""
|
||||
with codecs.open(json_file, encoding='utf-8') as data_file:
|
||||
try:
|
||||
json_content = json.load(data_file)
|
||||
except json.decoder.JSONDecodeError:
|
||||
err_msg = "JSONDecodeError: JSON file format error: {}".format(json_file)
|
||||
logging.error(err_msg)
|
||||
raise exception.FileFormatError(err_msg)
|
||||
|
||||
testcase.check_format(json_file, json_content)
|
||||
return json_content
|
||||
|
||||
def load_tests(testcase_file_path):
|
||||
file_suffix = os.path.splitext(testcase_file_path)[1]
|
||||
if file_suffix == '.json':
|
||||
return load_json_file(testcase_file_path)
|
||||
elif file_suffix in ['.yaml', '.yml']:
|
||||
return load_yaml_file(testcase_file_path)
|
||||
else:
|
||||
# '' or other suffix
|
||||
return []
|
||||
|
||||
def load_folder_files(folder_path, recursive=True):
|
||||
""" load folder path, return all files in list format.
|
||||
@param
|
||||
|
||||
@@ -2,7 +2,7 @@ import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from ate import runner, utils
|
||||
from ate import runner, testcase, utils
|
||||
from ate.context import Context
|
||||
from ate.exception import ParamsError
|
||||
|
||||
@@ -12,7 +12,7 @@ class VariableBindsUnittest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.context = Context()
|
||||
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml')
|
||||
self.testcases = utils.load_tests(testcase_file_path)
|
||||
self.testcases = testcase.load_tests(testcase_file_path)
|
||||
|
||||
def test_context_init_functions(self):
|
||||
self.assertIn("get_timestamp", self.context.testset_functions_config)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import os
|
||||
|
||||
import requests
|
||||
from ate import exception, runner, testcase, utils
|
||||
from ate import exception, runner, testcase
|
||||
|
||||
from tests.base import ApiServerUnittest
|
||||
|
||||
@@ -26,18 +25,18 @@ class TestRunner(ApiServerUnittest):
|
||||
|
||||
def test_run_single_testcase(self):
|
||||
for testcase_file_path in self.testcase_file_path_list:
|
||||
testcases = utils.load_tests(testcase_file_path)
|
||||
testcase = testcases[0]["test"]
|
||||
self.assertTrue(self.test_runner._run_test(testcase))
|
||||
testcases = testcase.load_tests(testcase_file_path)
|
||||
test = testcases[0]["test"]
|
||||
self.assertTrue(self.test_runner._run_test(test))
|
||||
|
||||
testcase = testcases[1]["test"]
|
||||
self.assertTrue(self.test_runner._run_test(testcase))
|
||||
test = testcases[1]["test"]
|
||||
self.assertTrue(self.test_runner._run_test(test))
|
||||
|
||||
testcase = testcases[2]["test"]
|
||||
self.assertTrue(self.test_runner._run_test(testcase))
|
||||
test = testcases[2]["test"]
|
||||
self.assertTrue(self.test_runner._run_test(test))
|
||||
|
||||
def test_run_single_testcase_fail(self):
|
||||
testcase = {
|
||||
test = {
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
@@ -63,7 +62,7 @@ class TestRunner(ApiServerUnittest):
|
||||
}
|
||||
|
||||
with self.assertRaises(exception.ValidationError):
|
||||
self.test_runner._run_test(testcase)
|
||||
self.test_runner._run_test(test)
|
||||
|
||||
def test_run_testset_hardcode(self):
|
||||
for testcase_file_path in self.testcase_file_path_list:
|
||||
|
||||
@@ -3,11 +3,86 @@ import time
|
||||
import unittest
|
||||
|
||||
from ate import testcase
|
||||
from ate.exception import ParamsError, ApiNotFound
|
||||
from ate.exception import ApiNotFound, FileFormatError, ParamsError
|
||||
|
||||
|
||||
class TestcaseParserUnittest(unittest.TestCase):
|
||||
|
||||
def test_load_testcases_bad_filepath(self):
|
||||
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo')
|
||||
self.assertEqual(testcase.load_tests(testcase_file_path), [])
|
||||
|
||||
def test_load_json_testcases(self):
|
||||
testcase_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/demo_testset_hardcode.json')
|
||||
testcases = testcase.load_tests(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'])
|
||||
|
||||
def test_load_yaml_testcases(self):
|
||||
testcase_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/demo_testset_hardcode.yml')
|
||||
testcases = testcase.load_tests(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'])
|
||||
|
||||
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:
|
||||
f.write("")
|
||||
|
||||
with self.assertRaises(FileFormatError):
|
||||
testcase.load_yaml_file(yaml_tmp_file)
|
||||
|
||||
os.remove(yaml_tmp_file)
|
||||
|
||||
# create invalid format yaml file
|
||||
with open(yaml_tmp_file, 'w') as f:
|
||||
f.write("abc")
|
||||
|
||||
with self.assertRaises(FileFormatError):
|
||||
testcase.load_yaml_file(yaml_tmp_file)
|
||||
|
||||
os.remove(yaml_tmp_file)
|
||||
|
||||
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:
|
||||
f.write("")
|
||||
|
||||
with self.assertRaises(FileFormatError):
|
||||
testcase.load_json_file(json_tmp_file)
|
||||
|
||||
os.remove(json_tmp_file)
|
||||
|
||||
# create empty json file
|
||||
with open(json_tmp_file, 'w') as f:
|
||||
f.write("{}")
|
||||
|
||||
with self.assertRaises(FileFormatError):
|
||||
testcase.load_json_file(json_tmp_file)
|
||||
|
||||
os.remove(json_tmp_file)
|
||||
|
||||
# create invalid format json file
|
||||
with open(json_tmp_file, 'w') as f:
|
||||
f.write("abc")
|
||||
|
||||
with self.assertRaises(FileFormatError):
|
||||
testcase.load_json_file(json_tmp_file)
|
||||
|
||||
os.remove(json_tmp_file)
|
||||
|
||||
def test_extract_variables(self):
|
||||
self.assertEqual(
|
||||
testcase.extract_variables("$var"),
|
||||
|
||||
@@ -8,81 +8,6 @@ from tests.base import ApiServerUnittest
|
||||
|
||||
class TestUtils(ApiServerUnittest):
|
||||
|
||||
def test_load_testcases_bad_filepath(self):
|
||||
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo')
|
||||
self.assertEqual(utils.load_tests(testcase_file_path), [])
|
||||
|
||||
def test_load_json_testcases(self):
|
||||
testcase_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/demo_testset_hardcode.json')
|
||||
testcases = utils.load_tests(testcase_file_path)
|
||||
self.assertEqual(len(testcases), 3)
|
||||
testcase = testcases[0]["test"]
|
||||
self.assertIn('name', testcase)
|
||||
self.assertIn('request', testcase)
|
||||
self.assertIn('url', testcase['request'])
|
||||
self.assertIn('method', testcase['request'])
|
||||
|
||||
def test_load_yaml_testcases(self):
|
||||
testcase_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/demo_testset_hardcode.yml')
|
||||
testcases = utils.load_tests(testcase_file_path)
|
||||
self.assertEqual(len(testcases), 3)
|
||||
testcase = testcases[0]["test"]
|
||||
self.assertIn('name', testcase)
|
||||
self.assertIn('request', testcase)
|
||||
self.assertIn('url', testcase['request'])
|
||||
self.assertIn('method', testcase['request'])
|
||||
|
||||
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:
|
||||
f.write("")
|
||||
|
||||
with self.assertRaises(exception.FileFormatError):
|
||||
utils.load_yaml_file(yaml_tmp_file)
|
||||
|
||||
os.remove(yaml_tmp_file)
|
||||
|
||||
# create invalid format yaml file
|
||||
with open(yaml_tmp_file, 'w') as f:
|
||||
f.write("abc")
|
||||
|
||||
with self.assertRaises(exception.FileFormatError):
|
||||
utils.load_yaml_file(yaml_tmp_file)
|
||||
|
||||
os.remove(yaml_tmp_file)
|
||||
|
||||
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:
|
||||
f.write("")
|
||||
|
||||
with self.assertRaises(exception.FileFormatError):
|
||||
utils.load_json_file(json_tmp_file)
|
||||
|
||||
os.remove(json_tmp_file)
|
||||
|
||||
# create empty json file
|
||||
with open(json_tmp_file, 'w') as f:
|
||||
f.write("{}")
|
||||
|
||||
with self.assertRaises(exception.FileFormatError):
|
||||
utils.load_json_file(json_tmp_file)
|
||||
|
||||
os.remove(json_tmp_file)
|
||||
|
||||
# create invalid format json file
|
||||
with open(json_tmp_file, 'w') as f:
|
||||
f.write("abc")
|
||||
|
||||
with self.assertRaises(exception.FileFormatError):
|
||||
utils.load_json_file(json_tmp_file)
|
||||
|
||||
os.remove(json_tmp_file)
|
||||
|
||||
def test_load_folder_files(self):
|
||||
folder = os.path.join(os.getcwd(), 'tests')
|
||||
file1 = os.path.join(os.getcwd(), 'tests', 'test_utils.py')
|
||||
|
||||
Reference in New Issue
Block a user