Skip to content

Commit

Permalink
feat: remove request_params, merge with case_params
Browse files Browse the repository at this point in the history
  • Loading branch information
wuranxu committed Jan 31, 2024
1 parent 7f06ed2 commit ac7934d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 25 deletions.
8 changes: 4 additions & 4 deletions app/core/constructor/case_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestcaseConstructor(ConstructorAbstract):

@staticmethod
async def run(executor, env, index, path, params, req_params, constructor: Constructor, **kwargs):
async def run(executor, env, index, path, params, constructor: Constructor, **kwargs):
try:
data = json.loads(constructor.constructor_json)
case_id = data.get("constructor_case_id")
Expand All @@ -21,13 +21,13 @@ async def run(executor, env, index, path, params, req_params, constructor: Const
new_param = data.get("params")
if new_param:
temp = json.loads(new_param)
req_params.update(temp)
result, err = await executor_class.run(env, case_id, params, req_params, f"{path}->{testcase.name}")
params.update(temp)
result, err = await executor_class.run(env, case_id, params, f"{path}->{testcase.name}")
if err:
raise Exception(err)
if not result["status"]:
raise Exception(f"断言失败, 断言数据: {result.get('asserts', 'unknown')}")
params[constructor.value] = result
return result
except Exception as e:
raise Exception(
f"{path}->{constructor.name}{index + 1}{ConstructorAbstract.get_name(constructor)}执行失败: {e}")
2 changes: 1 addition & 1 deletion app/core/constructor/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ConstructorAbstract(ABC):

@staticmethod
def run(executor, env, index, path, params, req_params, constructor: Constructor, **kwargs):
def run(executor, env, index, path, params, constructor: Constructor, **kwargs):
pass

@staticmethod
Expand Down
5 changes: 2 additions & 3 deletions app/core/constructor/http_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class HttpConstructor(ConstructorAbstract):

@staticmethod
async def run(executor, env, index, path, params, req_params, constructor: Constructor, **kwargs):
async def run(executor, env, index, path, params, constructor: Constructor, **kwargs):
try:
executor.append(f"当前路径: {path}, 第{index + 1}{HttpConstructor.get_name(constructor)}")
data = json.loads(constructor.constructor_json)
Expand All @@ -24,10 +24,9 @@ async def run(executor, env, index, path, params, req_params, constructor: Const
body=data.get("body"))
resp = await client.invoke(data.get("request_method"))
executor.append(f"当前{ConstructorAbstract.get_name(constructor)}类型为http, url: {url}")
if constructor.value:
params[constructor.value] = resp
executor.append(
f"当前{ConstructorAbstract.get_name(constructor)}返回变量: {constructor.value}\n返回值:\n {resp}\n")
return resp
except Exception as e:
raise Exception(
f"{path}->{constructor.name}{index + 1}{HttpConstructor.get_name(constructor)}执行失败: {e}")
6 changes: 2 additions & 4 deletions app/core/constructor/python_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PythonConstructor(ConstructorAbstract):

@staticmethod
@awaitable
def run(executor, env, index, path, params, req_params, constructor: Constructor, **kwargs):
def run(executor, env, index, path, params, constructor: Constructor, **kwargs):
try:
executor.append(f"当前路径: {path}, 第{index + 1}{ConstructorAbstract.get_name(constructor)}")
script = json.loads(constructor.constructor_json)
Expand All @@ -24,11 +24,9 @@ def run(executor, env, index, path, params, req_params, constructor: Constructor
executor.append(
f"当前{ConstructorAbstract.get_name(constructor)}未返回任何值")
return
# if not isinstance(py_data, str):
# py_data = json.dumps(py_data, ensure_ascii=False)
params[constructor.value] = py_data
executor.append(
f"当前{ConstructorAbstract.get_name(constructor)}返回变量: {constructor.value}\n返回值:\n {py_data}\n")
return py_data
except Exception as e:
raise Exception(
f"{path}->{constructor.name}{index + 1}{ConstructorAbstract.get_name(constructor)}执行失败: {e}")
4 changes: 2 additions & 2 deletions app/core/constructor/redis_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
class RedisConstructor(ConstructorAbstract):

@staticmethod
async def run(executor, env, index, path, params, req_params, constructor: Constructor, **kwargs):
async def run(executor, env, index, path, params, constructor: Constructor, **kwargs):
try:
executor.append(f"当前路径: {path}, 第{index + 1}{ConstructorAbstract.get_name(constructor)}")
data = json.loads(constructor.constructor_json)
redis = data.get("redis")
command = data.get("command")
executor.append(f"当前{ConstructorAbstract.get_name(constructor)}类型为redis, 名称: {redis}\n命令: {command}\n")
command_result = await PityRedisConfigDao.execute_command(command=command, name=redis, env=env)
params[constructor.value] = command_result
executor.append(
f"当前{ConstructorAbstract.get_name(constructor)}返回变量: {constructor.value}\n返回值:\n {command_result}\n")
return command_result
except Exception as e:
raise Exception(
f"{path}->{constructor.name}{index + 1}{ConstructorAbstract.get_name(constructor)}执行失败: {e}")
4 changes: 2 additions & 2 deletions app/core/constructor/sql_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
class SqlConstructor(ConstructorAbstract):

@staticmethod
async def run(executor, env, index, path, params, req_params, constructor: Constructor, **kwargs):
async def run(executor, env, index, path, params, constructor: Constructor, **kwargs):
try:
executor.append(f"当前路径: {path}, 第{index + 1}{ConstructorAbstract.get_name(constructor)}")
data = json.loads(constructor.constructor_json)
database = data.get("database")
sql = data.get("sql")
executor.append(f"当前{ConstructorAbstract.get_name(constructor)}类型为sql, 数据库名: {database}\nsql: {sql}\n")
sql_data = await DbConfigDao.execute_sql(env, database, sql)
params[constructor.value] = sql_data
executor.append(
f"当前{ConstructorAbstract.get_name(constructor)}返回变量: {constructor.value}\n返回值:\n {sql_data}\n")
return sql_data
except Exception as e:
raise Exception(
f"{path}->{constructor.name}{index + 1}{ConstructorAbstract.get_name(constructor)}执行失败: {e}")
19 changes: 10 additions & 9 deletions app/core/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,18 @@ async def get_constructor(self, case_id):
"""获取构造数据"""
return await TestCaseDao.async_select_constructor(case_id)

async def execute_constructors(self, env: int, path, params, req_params, constructors: List[Constructor],
suffix=False):
async def execute_constructors(self, env: int, path, params, constructors: List[Constructor], suffix=False):
"""开始构造数据"""
if len(constructors) == 0:
self.append("前后置条件为空, 跳出该环节")
return
current = 0
for i, c in enumerate(constructors):
if c.suffix == suffix:
await self.execute_constructor(env, current, path, params, req_params, c)
await self.execute_constructor(env, current, path, params, c)
current += 1

async def execute_constructor(self, env, index, path, params, req_params, constructor: Constructor):
async def execute_constructor(self, env, index, path, params, constructor: Constructor):
if not constructor.enable:
self.append(f"当前路径: {path}, 构造方法: {constructor.name} 已关闭, 不继续执行")
return False
Expand All @@ -234,7 +233,9 @@ async def execute_constructor(self, env, index, path, params, req_params, constr
return
# 加载变量
constructor.constructor_json = Render.render(params, constructor.constructor_json)
await construct.run(self, env, index, path, params, req_params, constructor, executor_class=Executor)
resp = await construct.run(self, env, index, path, params, constructor, executor_class=Executor)
if constructor.value and resp:
params[constructor.value] = resp

def add_header(self, case_info, headers):
"""
Expand Down Expand Up @@ -272,9 +273,9 @@ async def run(self, env: int, case_id: int, params_pool: dict = None, request_pa
# 加载全局变量
await self.query_gconfig(env)

# 挂载全局变量
# 挂载全局变量, 合并请求变量
case_params.update(self.glb)
req_params.update(self.glb)
case_params.update(req_params)

try:
case_info = await TestCaseDao.async_query_test_case(case_id)
Expand All @@ -293,7 +294,7 @@ async def run(self, env: int, case_id: int, params_pool: dict = None, request_pa
out_parameters = await PityTestCaseOutParametersDao.select_list(case_id=case_id)

# Step4: 执行前置条件
await self.execute_constructors(env, path, case_params, req_params, constructors)
await self.execute_constructors(env, path, case_params, constructors)

# Step5: 更新body url headers中的变量
await self.load_testcase_variables(case_info, GconfigType.case, case_params, *Executor.fields)
Expand Down Expand Up @@ -329,7 +330,7 @@ async def run(self, env: int, case_id: int, params_pool: dict = None, request_pa
case_params.update(out_dict)

# Step8: 执行后置条件
await self.execute_constructors(env, path, case_params, req_params, constructors, True)
await self.execute_constructors(env, path, case_params, constructors, True)

# Step9: 断言
asserts, ok = self.my_assert(case_params, asserts)
Expand Down

0 comments on commit ac7934d

Please sign in to comment.