change: remove fastapi and uvicorn for python httprunner

This commit is contained in:
debugtalk
2022-03-25 16:54:56 +08:00
parent c386302052
commit 097318ad2f
9 changed files with 1 additions and 363 deletions

View File

@@ -1,50 +0,0 @@
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"] == 0

View File

@@ -1,16 +0,0 @@
from fastapi import FastAPI
from httprunner import __version__
from .routers import deps, debugtalk, debug
app = FastAPI()
@app.get("/hrun/version")
async def get_hrun_version():
return {"code": 0, "message": "success", "result": {"HttpRunner": __version__}}
app.include_router(deps.router)
app.include_router(debugtalk.router)
app.include_router(debug.router)

View File

@@ -1,54 +0,0 @@
from fastapi import APIRouter
from httprunner.runner import HttpRunner
from httprunner.models import ProjectMeta, TestCase
router = APIRouter()
runner = HttpRunner()
@router.post("/hrun/debug/testcase", tags=["debug"])
async def debug_single_testcase(project_meta: ProjectMeta, testcase: TestCase):
resp = {"code": 0, "message": "success", "result": {}}
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")
for func_name in new_added_keys:
project_meta.functions[func_name] = locals()[func_name]
runner.with_project_meta(project_meta).run_testcase(testcase)
summary = runner.get_summary()
if not summary.success:
resp["code"] = 1
resp["message"] = "fail"
resp["result"] = summary.dict()
return resp
# @router.post("/hrun/debug/api", tags=["debug"])
# async def debug_single_api():
# resp = {
# "code": 0,
# "message": "success",
# "result": {}
# }
#
# # tests_mapping
#
# # summary = runner.run_tests(tests_mapping)
#
# return resp
#
#
# @router.post("/hrun/debug/testcases", tags=["debug"])
# async def debug_multiple_testcases(project_meta: ProjectMeta, testcases: TestCases):
# tests_mapping = {
# "project_meta": project_meta,
# "testcases": testcases
# }

View File

@@ -1,42 +0,0 @@
import contextlib
import sys
from io import StringIO
from fastapi import APIRouter
from loguru import logger
from starlette.requests import Request
router = APIRouter()
@contextlib.contextmanager
def stdout_io(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
@router.post("/hrun/debug/debugtalk_py", tags=["debugtalk"])
async def debug_python(request: Request):
body = await request.body()
if request.headers.get("content-transfer-encoding") == "base64":
# TODO: decode base64
pass
resp = {"code": 0, "message": "success", "result": ""}
try:
with stdout_io() as s:
exec(body, globals())
output = s.getvalue()
resp["result"] = output
except Exception as ex:
resp["code"] = 1
resp["message"] = "fail"
resp["result"] = str(ex)
logger.error(resp)
return resp

View File

@@ -1,34 +0,0 @@
import subprocess
from typing import List
import pkg_resources
from fastapi import APIRouter
from loguru import logger
router = APIRouter()
@router.get("/hrun/deps", tags=["deps"])
async def get_installed_dependenies():
resp = {"code": 0, "message": "success", "result": {}}
for p in pkg_resources.working_set:
resp["result"][p.project_name] = p.version
return resp
@router.post("/hrun/deps", tags=["deps"])
async def install_dependenies(deps: List[str]):
resp = {"code": 0, "message": "success", "result": {}}
for dep in deps:
try:
p = subprocess.run(["pip", "install", dep])
assert p.returncode == 0
resp["result"][dep] = True
except (AssertionError, subprocess.SubprocessError):
resp["result"][dep] = False
resp["code"] = 1
resp["message"] = "fail"
logger.error(f"failed to install dependency: {dep}")
return resp