feat: run testcase with debugtalk_py python script

This commit is contained in:
debugtalk
2020-03-07 09:57:59 +08:00
parent b5dae03cc4
commit 59c243f44c
3 changed files with 68 additions and 3 deletions

View File

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

View File

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

View File

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