From 59c243f44c8490050a59dbe014df708297e90726 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 7 Mar 2020 09:57:59 +0800 Subject: [PATCH] feat: run testcase with debugtalk_py python script --- httprunner/app/routers/debug.py | 18 ++++++++-- httprunner/app/routers/debug_test.py | 51 ++++++++++++++++++++++++++++ httprunner/schema/common.py | 2 +- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 httprunner/app/routers/debug_test.py diff --git a/httprunner/app/routers/debug.py b/httprunner/app/routers/debug.py index 595fb100..4c2d7726 100644 --- a/httprunner/app/routers/debug.py +++ b/httprunner/app/routers/debug.py @@ -14,10 +14,24 @@ async def debug_single_testcase(project_meta: ProjectMeta, testcase: TestCase): "message": "success", "result": {} } + + project_meta_json = project_meta.dict(by_alias=True) + if project_meta.debugtalk_py: + origin_local_keys = list(locals().keys()).copy() + exec(project_meta.debugtalk_py, {}, locals()) + new_local_keys = list(locals().keys()).copy() + new_added_keys = set(new_local_keys) - set(origin_local_keys) + new_added_keys.remove("origin_local_keys") + project_meta_json["functions"] = {} + for func_name in new_added_keys: + project_meta_json["functions"][func_name] = locals()[func_name] + + testcase_json = testcase.dict(by_alias=True) tests_mapping = { - "project_mapping": project_meta, - "testcases": [testcase] + "project_mapping": project_meta_json, + "testcases": [testcase_json] } + summary = runner.run_tests(tests_mapping) if not summary["success"]: resp["code"] = 1 diff --git a/httprunner/app/routers/debug_test.py b/httprunner/app/routers/debug_test.py new file mode 100644 index 00000000..0be91592 --- /dev/null +++ b/httprunner/app/routers/debug_test.py @@ -0,0 +1,51 @@ +import unittest + +from starlette.testclient import TestClient + +from httprunner.app.main import app + +client = TestClient(app) + + +class TestDebug(unittest.TestCase): + + def test_debug_single_testcase(self): + json_data = { + "project_meta": { + "debugtalk_py": "\ndef hello(name):\n print(f'hello, {name}')\n", + "variables": {}, + "env": {} + }, + "testcase": { + "config": { + "name": "test demo for debug service", + "verify": False, + "base_url": "", + "variables": {}, + "setup_hooks": [], + "teardown_hooks": [], + "export": [] + }, + "teststeps": [ + { + "name": "get index page", + "request": { + "method": "GET", + "url": "https://httpbin.org/", + "params": {}, + "headers": {}, + "json": {}, + "cookies": {}, + "timeout": 30, + "allow_redirects": True, + "verify": False + }, + "extract": {}, + "validate": [] + } + ] + } + } + response = client.post("/hrun/debug/testcase", json=json_data) + assert response.status_code == 200 + assert response.json()["code"] == 1 diff --git a/httprunner/schema/common.py b/httprunner/schema/common.py index 7229488d..9acd2cf0 100644 --- a/httprunner/schema/common.py +++ b/httprunner/schema/common.py @@ -12,7 +12,7 @@ Verify = bool Hook = List[str] Export = List[str] Extract = Dict[str, str] -Validate = List[Dict[str, Tuple[str, Any]]] +Validate = List[Dict] Env = Dict[str, Any]