fix circular reference in utils and testcase module

This commit is contained in:
debugtalk
2017-11-02 12:45:59 +08:00
parent 6f04f5008a
commit 5fd4e28d34
7 changed files with 132 additions and 125 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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"),

View File

@@ -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')