Merge pull request #351 from HttpRunner/project_structure

refactor project structure
This commit is contained in:
debugtalk
2018-08-23 16:20:27 +08:00
committed by GitHub
15 changed files with 113 additions and 148 deletions

1
.gitignore vendored
View File

@@ -12,4 +12,3 @@ logs/%
.coverage
locustfile.py
site/
.env

View File

@@ -18,7 +18,6 @@ class HttpRunner(object):
resultclass (class): HtmlTestResult or TextTestResult
failfast (bool): False/True, stop the test run on the first error or failure.
dot_env_path (str): .env file path.
http_client_session (instance): requests.Session(), or locust.client.Session() instance.
Attributes:
@@ -37,26 +36,6 @@ class HttpRunner(object):
self.kwargs = kwargs
self.http_client_session = self.kwargs.pop("http_client_session", None)
self.__loader()
def __loader(self):
""" load project dependent files, including api/testcase definitions,
environment variables and builtin module.
"""
loader.reset_loader()
# load .env
dot_env_path = self.kwargs.pop("dot_env_path", None)
loader.load_dot_env_file(dot_env_path)
# load api/testcase definition and debugtalk.py module
project_folder_path = os.path.join(os.getcwd(), "tests") # TODO: remove tests
loader.load_project_tests(project_folder_path)
self.project_mapping = loader.project_mapping
utils.set_os_environ(self.project_mapping["env"])
def load_tests(self, path_or_testcases):
""" load testcases, extend and merge with api/testcase definitions.
@@ -73,7 +52,7 @@ class HttpRunner(object):
{
"config": {
"name": "desc1",
"path": "",
"path": "", # optional
"variables": [], # optional
"request": {} # optional
},
@@ -99,21 +78,23 @@ class HttpRunner(object):
if isinstance(path_or_testcases, list):
for testcase in path_or_testcases:
try:
dir_path = os.path.dirname(testcase["config"]["path"])
loader.load_debugtalk_module(dir_path)
test_path = os.path.dirname(testcase["config"]["path"])
except KeyError:
pass
test_path = os.getcwd()
loader.load_project_tests(test_path)
else:
try:
dir_path = os.path.dirname(path_or_testcases["config"]["path"])
loader.load_debugtalk_module(dir_path)
test_path = os.path.dirname(path_or_testcases["config"]["path"])
except KeyError:
pass
test_path = os.getcwd()
loader.load_project_tests(test_path)
testcases = path_or_testcases
else:
testcases = loader.load_testcases(path_or_testcases)
self.project_mapping = loader.project_mapping
if not testcases:
raise exceptions.TestcaseNotFound
@@ -250,8 +231,9 @@ class HttpRunner(object):
instance: HttpRunner() instance
"""
# parser
# loader
testcases_list = self.load_tests(path_or_testcases)
# parser
parsed_testcases_list = self.parse_tests(testcases_list)
# initialize

View File

@@ -39,9 +39,6 @@ def main_hrun():
parser.add_argument(
'--log-file',
help="Write logs to specified file path.")
parser.add_argument(
'--dot-env-path',
help="Specify .env file path, which is useful for keeping production credentials.")
parser.add_argument(
'--failfast', action='store_true', default=False,
help="Stop the test run on the first error or failure.")
@@ -78,7 +75,7 @@ def main_hrun():
create_scaffold(project_path)
exit(0)
runner = HttpRunner(failfast=args.failfast, dot_env_path=args.dot_env_path).run(args.testset_paths)
runner = HttpRunner(failfast=args.failfast).run(args.testset_paths)
if not args.no_html_report:
runner.gen_html_report(

View File

@@ -6,7 +6,7 @@ import json
import os
import yaml
from httprunner import built_in, exceptions, logger, parser, validator
from httprunner import built_in, exceptions, logger, parser, utils, validator
from httprunner.compat import OrderedDict
project_mapping = {
@@ -22,6 +22,7 @@ project_mapping = {
"""
testcases_cache_mapping = {}
project_working_directory = os.getcwd()
###############################################################################
@@ -156,12 +157,8 @@ def load_folder_files(folder_path, recursive=True):
return file_list
def load_dot_env_file(path):
""" load .env file
Args:
path (str): .env file path.
If path is None, it will find .env file in current working directory.
def load_dot_env_file():
""" load .env file, .env file should be located in project working directory.
Returns:
dict: environment variables mapping
@@ -173,18 +170,13 @@ def load_dot_env_file(path):
}
Raises:
exceptions.FileNotFound: If specified env file is not exist.
exceptions.FileFormatError: If env file format is invalid.
"""
if not path:
path = os.path.join(os.getcwd(), ".env")
if not os.path.isfile(path):
logger.log_debug(".env file not exist: {}".format(path))
return {}
else:
if not os.path.isfile(path):
raise exceptions.FileNotFound("env file not exist: {}".format(path))
path = os.path.join(project_working_directory, ".env")
if not os.path.isfile(path):
logger.log_debug(".env file not exist in : {}".format(project_working_directory))
return {}
logger.log_info("Loading environment variables from {}".format(path))
env_variables_mapping = {}
@@ -200,6 +192,8 @@ def load_dot_env_file(path):
env_variables_mapping[variable.strip()] = value.strip()
project_mapping["env"] = env_variables_mapping
utils.set_os_environ(env_variables_mapping)
return env_variables_mapping
@@ -239,6 +233,23 @@ def locate_file(start_path, file_name):
return locate_file(os.path.dirname(start_dir_path), file_name)
def locate_pwd(start_path):
""" locate project working directory.
The folder contains debugtalk.py will be used as PWD.
If debugtalk.py is not found, use os.getcwd() as default PWD.
Args:
start_path (str): start locating path, maybe testcase file path or directory path
"""
global project_working_directory
try:
debugtalk_path = locate_file(start_path, "debugtalk.py")
project_working_directory = os.path.dirname(debugtalk_path)
except exceptions.FileNotFound:
project_working_directory = os.getcwd()
###############################################################################
## debugtalk.py module loader
###############################################################################
@@ -305,25 +316,18 @@ def load_builtin_module():
project_mapping["debugtalk"] = built_in_module
def load_debugtalk_module(start_path=None):
def load_debugtalk_module():
""" load project debugtalk.py module and merge with builtin module.
Args:
start_path (str, optional): start locating path, maybe file path or directory path.
Defaults to current working directory.
Returns:
dict: variables and functions mapping for debugtalk.py
debugtalk.py should be located in project working directory.
variables and functions mapping for debugtalk.py
{
"variables": {},
"functions": {}
}
"""
start_path = start_path or os.getcwd()
try:
module_path = locate_file(start_path, "debugtalk.py")
module_path = locate_file(project_working_directory, "debugtalk.py")
module_name = convert_module_name(module_path)
except exceptions.FileNotFound:
return
@@ -420,9 +424,7 @@ def _load_test_file(file_path):
"""
testcase = {
"config": {
"path": file_path
},
"config": {},
"teststeps": []
}
@@ -742,7 +744,7 @@ def load_folder_content(folder_path):
return items_mapping
def load_api_folder(api_folder_path=None):
def load_api_folder(api_folder_path):
""" load api definitions from api folder.
Args:
@@ -783,7 +785,6 @@ def load_api_folder(api_folder_path=None):
"""
api_definition_mapping = {}
api_folder_path = api_folder_path or os.path.join(os.getcwd(), "api")
api_items_mapping = load_folder_content(api_folder_path)
for api_file_path, api_items in api_items_mapping.items():
@@ -805,7 +806,7 @@ def load_api_folder(api_folder_path=None):
return api_definition_mapping
def load_test_folder(test_folder_path=None):
def load_test_folder(test_folder_path):
""" load testcases definitions from folder.
Args:
@@ -847,17 +848,13 @@ def load_test_folder(test_folder_path=None):
"""
test_definition_mapping = {}
# TODO: replace suite with testcases
test_folder_path = test_folder_path or os.path.join(os.getcwd(), "suite")
test_items_mapping = load_folder_content(test_folder_path)
for test_file_path, items in test_items_mapping.items():
# TODO: add JSON schema validation
testcase = {
"config": {
"path": test_file_path
},
"config": {},
"teststeps": []
}
for item in items:
@@ -890,6 +887,9 @@ def load_test_folder(test_folder_path=None):
def reset_loader():
""" reset project mapping.
"""
global project_working_directory
project_working_directory = os.getcwd()
project_mapping["debugtalk"] = {
"variables": {},
"functions": {}
@@ -900,16 +900,22 @@ def reset_loader():
testcases_cache_mapping.clear()
def load_project_tests(folder_path):
""" load api, testcases and builtin module.
def load_project_tests(test_path):
""" load api, testcases, .env, builtin module and debugtalk.py.
api/testcases folder is relative to project_working_directory
Args:
folder_path (str): folder path.
test_path (str): test file/folder path, locate pwd from this path.
"""
reset_loader()
locate_pwd(test_path)
load_dot_env_file()
load_builtin_module()
load_api_folder(os.path.join(folder_path, "api"))
load_test_folder(os.path.join(folder_path, "suite"))
load_debugtalk_module()
load_api_folder(os.path.join(project_working_directory, "api"))
# TODO: replace suite with testcases
load_test_folder(os.path.join(project_working_directory, "suite"))
def load_testcases(path):
@@ -948,14 +954,13 @@ def load_testcases(path):
return testcases_cache_mapping[path]
if os.path.isdir(path):
load_debugtalk_module(path)
load_project_tests(path)
files_list = load_folder_files(path)
testcases_list = load_testcases(files_list)
elif os.path.isfile(path):
try:
dir_path = os.path.dirname(path)
load_debugtalk_module(dir_path)
load_project_tests(path)
testcase = _load_test_file(path)
if testcase["teststeps"]:
testcases_list = [testcase]

View File

@@ -9,7 +9,7 @@ from base64 import b64encode
from collections import Iterable
from datetime import datetime
from httprunner import logger
from httprunner import loader, logger
from httprunner.__about__ import __version__
from httprunner.compat import basestring, bytes, json, numeric_types
from jinja2 import Template, escape
@@ -95,7 +95,7 @@ def render_html_report(summary, html_report_name=None, html_report_template=None
logger.log_info("Start to render Html report ...")
logger.log_debug("render data: {}".format(summary))
report_dir_path = os.path.join(os.getcwd(), "reports")
report_dir_path = os.path.join(loader.project_working_directory, "reports")
start_at_timestamp = int(summary["time"]["start_at"])
summary["time"]["start_datetime"] = datetime.fromtimestamp(start_at_timestamp).strftime('%Y-%m-%d %H:%M:%S')
if html_report_name:

View File

@@ -45,7 +45,6 @@ class Runner(object):
testcase:
{
"name": "testcase description",
"path": "tests/data/demo_testset_variables.yml",
"variables": [], # optional
"request": {
"base_url": "http://127.0.0.1:5000",

View File

@@ -374,11 +374,12 @@ def create_scaffold(project_path):
path_list = [
(project_path, "folder"),
(os.path.join(project_path, "tests"), "folder"),
(os.path.join(project_path, "tests", "api"), "folder"),
(os.path.join(project_path, "tests", "suite"), "folder"),
(os.path.join(project_path, "tests", "testcases"), "folder"),
(os.path.join(project_path, "tests", "debugtalk.py"), "file")
(os.path.join(project_path, "api"), "folder"),
(os.path.join(project_path, "testcases"), "folder"),
(os.path.join(project_path, "testsuites"), "folder"),
(os.path.join(project_path, "reports"), "folder"),
(os.path.join(project_path, "debugtalk.py"), "file"),
(os.path.join(project_path, ".env"), "file")
]
msg = ""

View File

@@ -14,7 +14,6 @@ def is_testcase(data_structure):
{
"config": {
"name": "desc1",
"path": "",
"variables": [], # optional
"request": {} # optional
},

View File

@@ -2,7 +2,7 @@ import os
import shutil
import time
from httprunner import HttpRunner, LocustRunner
from httprunner import HttpRunner, LocustRunner, loader
from locust import HttpLocust
from tests.api_server import HTTPBIN_SERVER
from tests.base import ApiServerUnittest
@@ -21,7 +21,6 @@ class TestHttpRunner(ApiServerUnittest):
self.testcase = {
'name': 'testset description',
'config': {
'path': 'docs/data/demo-quickstart-2.yml',
'name': 'testset description',
'request': {
'base_url': '',
@@ -89,7 +88,7 @@ class TestHttpRunner(ApiServerUnittest):
self.assertEqual(summary["stat"]["skipped"], 4)
runner.gen_html_report(html_report_name=output_folder_name)
report_save_dir = os.path.join(os.getcwd(), 'reports', output_folder_name)
report_save_dir = os.path.join(loader.project_working_directory, 'reports', output_folder_name)
self.assertGreater(len(os.listdir(report_save_dir)), 0)
shutil.rmtree(report_save_dir)
@@ -155,7 +154,7 @@ class TestHttpRunner(ApiServerUnittest):
output_folder_name = os.path.basename(os.path.splitext(testset_path)[0])
report = runner.gen_html_report(html_report_name=output_folder_name)
self.assertTrue(os.path.isfile(report))
report_save_dir = os.path.join(os.getcwd(), 'reports', output_folder_name)
report_save_dir = os.path.join(loader.project_working_directory, 'reports', output_folder_name)
shutil.rmtree(report_save_dir)
def test_testcase_layer(self):
@@ -181,7 +180,7 @@ class TestHttpRunner(ApiServerUnittest):
{
"config": {
"name": "test teardown hooks",
'path': 'tests/httpbin/hooks.yml',
"path": "tests/httpbin/hooks.yml"
},
"teststeps": [
{
@@ -217,7 +216,7 @@ class TestHttpRunner(ApiServerUnittest):
{
"name": "test teardown hooks",
"config": {
'path': 'tests/httpbin/hooks.yml',
"path": "tests/httpbin/hooks.yml"
},
"teststeps": [
{
@@ -246,9 +245,7 @@ class TestHttpRunner(ApiServerUnittest):
testcases = [
{
"name": "test teardown hooks",
"config": {
'path': 'tests/httpbin/hooks.yml',
},
"config": {},
"teststeps": [
{
"name": "test teardown hooks",
@@ -339,14 +336,6 @@ class TestHttpRunner(ApiServerUnittest):
self.assertIn("in", summary["details"][0]["in_out"])
self.assertIn("out", summary["details"][0]["in_out"])
def test_loader(self):
hrunner = HttpRunner(dot_env_path="tests/data/test.env")
self.assertEqual(hrunner.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")
self.assertIn("debugtalk", hrunner.project_mapping)
self.assertIn("setup_and_reset", hrunner.project_mapping["def-testcase"])
self.assertIn("get_token", hrunner.project_mapping["def-api"])
self.assertIn("setup_and_reset", hrunner.project_mapping["def-testcase"])
def test_load_tests(self):
testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase.yml')

View File

@@ -9,9 +9,7 @@ from tests.base import ApiServerUnittest
class TestContext(ApiServerUnittest):
def setUp(self):
project_dir = os.path.join(os.getcwd(), "tests")
loader.load_project_tests(project_dir)
loader.load_debugtalk_module(project_dir)
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
self.debugtalk_module = loader.project_mapping["debugtalk"]
self.context = context.Context(

View File

@@ -133,13 +133,18 @@ class TestFileLoader(unittest.TestCase):
self.assertEqual([], files)
def test_load_dot_env_file(self):
env_variables_mapping = loader.load_dot_env_file("tests/data/test.env")
loader.project_working_directory = os.path.join(
os.getcwd(), "tests",
)
env_variables_mapping = loader.load_dot_env_file()
self.assertIn("PROJECT_KEY", env_variables_mapping)
self.assertEqual(env_variables_mapping["UserName"], "debugtalk")
def test_load_env_path_not_exist(self):
with self.assertRaises(exceptions.FileNotFound):
loader.load_dot_env_file("not_exist.env")
loader.project_working_directory = os.path.join(
os.getcwd(), "tests", "data",
)
loader.load_dot_env_file()
def test_locate_file(self):
with self.assertRaises(exceptions.FileNotFound):
@@ -166,7 +171,7 @@ class TestFileLoader(unittest.TestCase):
"tests/debugtalk.py"
)
self.assertEqual(
loader.locate_file("tests/data/test.env", "debugtalk.py"),
loader.locate_file("tests/data/demo_testcase.yml", "debugtalk.py"),
"tests/debugtalk.py"
)
@@ -180,15 +185,13 @@ class TestModuleLoader(unittest.TestCase):
self.assertNotIn("is_py3", functions_dict)
def test_load_debugtalk_module(self):
project_dir = os.path.join(os.getcwd(), "tests")
loader.load_project_tests(project_dir)
loader.load_debugtalk_module()
loader.load_project_tests(os.path.join(os.getcwd(), "httprunner"))
imported_module_items = loader.project_mapping["debugtalk"]
self.assertIn("equals", imported_module_items["functions"])
self.assertNotIn("SECRET_KEY", imported_module_items["variables"])
self.assertNotIn("alter_response", imported_module_items["functions"])
loader.load_debugtalk_module("tests")
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
imported_module_items = loader.project_mapping["debugtalk"]
self.assertEqual(
imported_module_items["variables"]["SECRET_KEY"],
@@ -223,18 +226,26 @@ class TestModuleLoader(unittest.TestCase):
with self.assertRaises(exceptions.VariableNotFound):
loader.get_module_item(module_mapping, "variables", "SECRET_KEY2")
def test_locate_pwd(self):
loader.locate_pwd("tests/data/demo_testcase.yml")
self.assertEqual(loader.project_working_directory, "tests")
loader.locate_pwd("tests/base.py")
self.assertEqual(loader.project_working_directory, "tests")
loader.locate_pwd("httprunner/__init__.py")
self.assertEqual(loader.project_working_directory, os.getcwd())
class TestSuiteLoader(unittest.TestCase):
@classmethod
def setUpClass(cls):
project_dir = os.path.join(os.getcwd(), "tests")
loader.load_project_tests(project_dir)
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
def test_load_test_file_testcase(self):
testcase = loader._load_test_file("tests/testcases/smoketest.yml")
self.assertEqual(testcase["config"]["name"], "smoketest")
self.assertEqual(testcase["config"]["path"], "tests/testcases/smoketest.yml")
self.assertIn("device_sn", testcase["config"]["variables"][0])
self.assertEqual(len(testcase["teststeps"]), 8)
self.assertEqual(testcase["teststeps"][0]["name"], "get token")
@@ -353,8 +364,6 @@ class TestSuiteLoader(unittest.TestCase):
os.getcwd(), 'tests/data/demo_testset_hardcode.json')
testset_list = loader.load_testcases(path)
self.assertEqual(len(testset_list), 1)
self.assertIn("path", testset_list[0]["config"])
self.assertEqual(testset_list[0]["config"]["path"], path)
self.assertEqual(len(testset_list[0]["teststeps"]), 3)
testsets_list.extend(testset_list)
@@ -362,8 +371,6 @@ class TestSuiteLoader(unittest.TestCase):
path = 'tests/data/demo_testset_hardcode.yml'
testset_list = loader.load_testcases(path)
self.assertEqual(len(testset_list), 1)
self.assertIn("path", testset_list[0]["config"])
self.assertIn(path, testset_list[0]["config"]["path"])
self.assertEqual(len(testset_list[0]["teststeps"]), 3)
testsets_list.extend(testset_list)
@@ -478,10 +485,9 @@ class TestSuiteLoader(unittest.TestCase):
)
def test_load_project_tests(self):
project_dir = os.path.join(os.getcwd(), "tests")
loader.load_project_tests(project_dir)
loader.load_debugtalk_module(project_dir)
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
project_mapping = loader.project_mapping
self.assertEqual(project_mapping["debugtalk"]["variables"]["SECRET_KEY"], "DebugTalk")
self.assertIn("get_token", project_mapping["def-api"])
self.assertIn("setup_and_reset", project_mapping["def-testcase"])
self.assertEqual(project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")

View File

@@ -412,8 +412,7 @@ class TestParser(unittest.TestCase):
)
def test_parse_parameters_mix(self):
project_dir = os.path.join(os.getcwd(), "tests")
loader.load_debugtalk_module(project_dir)
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
project_mapping = loader.project_mapping
parameters = [

View File

@@ -10,9 +10,7 @@ from tests.base import ApiServerUnittest
class TestRunner(ApiServerUnittest):
def setUp(self):
project_dir = os.path.join(os.getcwd(), "tests")
loader.load_project_tests(project_dir)
loader.load_debugtalk_module(project_dir)
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
self.debugtalk_module = loader.project_mapping["debugtalk"]
config_dict = {
"variables": self.debugtalk_module["variables"],
@@ -82,7 +80,6 @@ class TestRunner(ApiServerUnittest):
start_time = time.time()
config_dict = {
"path": os.path.join(os.getcwd(), __file__),
"name": "basic test with httpbin",
"variables": self.debugtalk_module["variables"],
"functions": self.debugtalk_module["functions"],
@@ -132,7 +129,6 @@ class TestRunner(ApiServerUnittest):
def test_run_testset_with_hooks_modify_request(self):
config_dict = {
"path": os.path.join(os.getcwd(), __file__),
"name": "basic test with httpbin",
"variables": self.debugtalk_module["variables"],
"functions": self.debugtalk_module["functions"],
@@ -187,9 +183,7 @@ class TestRunner(ApiServerUnittest):
],
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
}
config_dict = {
"path": os.path.join(os.getcwd(), __file__)
}
config_dict = {}
self.test_runner.init_config(config_dict, "testcase")
start_time = time.time()
@@ -220,9 +214,7 @@ class TestRunner(ApiServerUnittest):
],
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
}
config_dict = {
"path": os.path.join(os.getcwd(), __file__)
}
config_dict = {}
self.test_runner.init_config(config_dict, "testcase")
start_time = time.time()
@@ -248,9 +240,7 @@ class TestRunner(ApiServerUnittest):
testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/test_bugfix.yml')
testcases = loader.load_file(testcase_file_path)
config_dict = {
"path": testcase_file_path
}
config_dict = {}
self.test_runner.init_config(config_dict, "testcase")
test = testcases[2]["test"]

View File

@@ -254,11 +254,12 @@ class TestUtils(ApiServerUnittest):
def test_create_scaffold(self):
project_path = os.path.join(os.getcwd(), "projectABC")
utils.create_scaffold(project_path)
self.assertTrue(os.path.isdir(os.path.join(project_path, "tests")))
self.assertTrue(os.path.isdir(os.path.join(project_path, "tests", "api")))
self.assertTrue(os.path.isdir(os.path.join(project_path, "tests", "suite")))
self.assertTrue(os.path.isdir(os.path.join(project_path, "tests", "testcases")))
self.assertTrue(os.path.isfile(os.path.join(project_path, "tests", "debugtalk.py")))
self.assertTrue(os.path.isdir(os.path.join(project_path, "api")))
self.assertTrue(os.path.isdir(os.path.join(project_path, "testcases")))
self.assertTrue(os.path.isdir(os.path.join(project_path, "testsuites")))
self.assertTrue(os.path.isdir(os.path.join(project_path, "reports")))
self.assertTrue(os.path.isfile(os.path.join(project_path, "debugtalk.py")))
self.assertTrue(os.path.isfile(os.path.join(project_path, ".env")))
shutil.rmtree(project_path)
def test_cartesian_product_one(self):