LinuxSuRen / api-testing

YAML based API testing tool. 开源接口调试、测试工具。Mock Server

Home Page:https://linuxsuren.github.io/api-testing/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support to generator Python code from a HTTP testCase

LinuxSuRen opened this issue · comments

commented

Below is the Golang code generator.

Similarly, we could add the Python code generator. Please feel free to do some research to find out how to make a HTTP request in Python.

示例中的代码是在根据go template文件生成http请求的payload? 想要的python代码生成器也是想要做到这一点吗

感觉整个的代码生成器可能是想要根据模板生成一段http请求的代码,//go:embed data/main.go.tpl我对这行注释代码感到不解,是怎样把当前的生成器和data/main.go.tpl文件对应起来的呢

示例中的代码是在根据go template文件生成http请求的payload? 想要的python代码生成器也是想要做到这一点吗

  • 差不多,整个 generator 的功能就是为前端用户传来的 Request 生成不同语言的 HTTP 客户端代码。市面类似的产品,比如 Postman 或者 Apifox 都有类似的功能,你可以试一试。

//go:embed data/main.go.tpl我对这行注释代码感到不解,是怎样把当前的生成器和data/main.go.tpl文件对应起来的呢

commented

@hahahashen 其实,就是用来生成一段发送 HTTP 请求的代码(不同语言),主要的使用场景是:

  • 不熟悉某个语言的语法或 SDK 用法
  • 节省写代码的时间

我可以试试解决这个问题吗

commented

好呀,期待你的 PR @zhouzhou1017

似乎存在一些问题:

  1. 生成的代码头部的注释使用的是 C 风格,不能在 Python 中直接运行,会导致语法错误。
/*
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
	http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

应该修改为

"""
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
  1. #364 一致, body 会有错误

  2. 当请求不包含 headers 时,生成的代码仍会存在 headers,运行出现语法错误

NameError: name 'headers' is not defined
commented

问题 2 感觉可以统一解决下,避免每种语言(框架)都会遇到。

commented

另外,为了保证生成的代码可以运行,我们可以增加对应的 e2e 测试:

  • 编写 testcase
  • 通过 curl 调用 API 并拿到生成的代码,保存为文件
  • 调用对应的语言命令进行测试,例如:python test.pygo run test.go

另外,为了保证生成的代码可以运行,我们可以增加对应的 e2e 测试:

  • 编写 testcase
  • 通过 curl 调用 API 并拿到生成的代码,保存为文件
  • 调用对应的语言命令进行测试,例如:python test.pygo run test.go

我觉得是个好主意!我想试试看,但是我还想报名 ospp,不知道我现在开始写关于这个的代码会不会有影响?我看到 注意事项 里面有写到:

在中选结果公示之前进行的开发、提交的 PR/MR 链接不符合结项审核的时间要求,将不予认可。

commented

只要是和议题无关的 PR 还是鼓励的,议题相关的代码是需要按照规定的时间线来做。

只要是和议题无关的 PR 还是鼓励的,议题相关的代码是需要按照规定的时间线来做。

我跟进试试看