From 5fd4e28d34f5fe43c2c9851ddfebe84f5df27b9e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 2 Nov 2017 12:45:59 +0800 Subject: [PATCH] fix circular reference in utils and testcase module --- ate/exception.py | 7 ++++ ate/testcase.py | 37 +++++++++++++++++++- ate/utils.py | 36 +------------------- tests/test_context.py | 4 +-- tests/test_runner.py | 21 ++++++------ tests/test_testcase.py | 77 +++++++++++++++++++++++++++++++++++++++++- tests/test_utils.py | 75 ---------------------------------------- 7 files changed, 132 insertions(+), 125 deletions(-) diff --git a/ate/exception.py b/ate/exception.py index 6abf7ae3..70461d59 100644 --- a/ate/exception.py +++ b/ate/exception.py @@ -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 diff --git a/ate/testcase.py b/ate/testcase.py index 1e393fe7..0f0dfed4 100644 --- a/ate/testcase.py +++ b/ate/testcase.py @@ -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: diff --git a/ate/utils.py b/ate/utils.py index 915d58e6..e904d6f2 100644 --- a/ate/utils.py +++ b/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 diff --git a/tests/test_context.py b/tests/test_context.py index ce07f7af..484b5f2d 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -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) diff --git a/tests/test_runner.py b/tests/test_runner.py index 006d7256..0ef7458f 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -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: diff --git a/tests/test_testcase.py b/tests/test_testcase.py index 5b7ffdf5..a6f3060e 100644 --- a/tests/test_testcase.py +++ b/tests/test_testcase.py @@ -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"), diff --git a/tests/test_utils.py b/tests/test_utils.py index fd956547..d56f0573 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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')