rest api的自动化测试和文档

scanapi的Python项目详细描述


scanapi

为您的api提供的库:

  • 自动测试
  • 自动化文档

如何安装

$ pip install scanapi

如何使用

在项目的根目录中创建一个api规范文件api.yaml并列出api的端点。

api:base_url:https://jsonplaceholder.typicode.com/endpoints:-namespace:postspath:/postsrequests:-name:list_allmethod:get-name:detailsmethod:getpath:/1

要运行请求并创建文档,请运行:

$ scanapi --help
Usage: scanapi [OPTIONS]

  Automated Testing and Documentation for your REST API.

Options:
  -s, --spec-path PATH
  -d, --docs-path TEXT
  --log-level [DEBUG|INFO|WARNING|ERROR|CRITICAL]
  --help                          Show this message and exit.

默认情况下,文档将在docs.md文件中可用。

可用方法

您可以运行以下方法:

  • 获取
  • 发布
  • 放置
  • 删除

配置

如果要配置scanapi,可以在项目根目录中创建文件.scanapi.yaml

spec_path:api.yamldocs_path:docs.md

标题

api:base_url:https://api.thecatapi.com/v1headers:x-api-key:DEMO-API-KEYContent-Type:application/jsonendpoints:-namespace:votespath:/votesrequests:-name:list_allmethod:get

查询参数

api:base_url:http://api.openweathermap.org/data/2.5params:APPID:<INSERT_YOUR_API_KEY_HERE>endpoints:-namespace:weatherpath:/weatherrequests:-name:citymethod:getparams:q:Rio de Janeiro

身体

在POST请求中,您可以添加正文:

api:base_url:https://api.thecatapi.com/v1headers:x-api-key:DEMO-API-KEYContent-Type:application/jsonendpoints:-namespace:votespath:/votesrequests:-name:votemethod:postbody:image_id:asf2value:1sub_id:demo-d4332e

环境变量

您可以在api规范文件中使用环境变量:

exportBASE_URL=https://jsonplaceholder.typicode.com/
api:base_url:${BASE_URL}headers:Content-Type:application/jsonendpoints:-namespace:postspath:/postsrequests:-name:list_allmethod:get-name:detailsmethod:getpath:/1

heads up:变量名必须大写。

链接请求:自定义变量+python代码

api:base_url:https://jsonplaceholder.typicode.com/headers:Content-Type:application/jsonendpoints:-namespace:postspath:/postsrequests:-name:list_all# posts_list_allmethod:getvars:post_id:${{responses['posts_list_all'].json()[1]['id']}}# should return id 2-name:details# posts_detailsmethod:getpath:${post_id}

嵌套端点

api:base_url:https://jsonplaceholder.typicode.com/headers:Content-Type:application/jsonendpoints:-namespace:postspath:/postsrequests:-name:list_all# posts_list_allmethod:getendpoints:-namespace:commentspath:/1/commentsrequests:-name:comments# posts_details_commentsmethod:get# https://jsonplaceholder.typicode.com/posts/1/comments

隐藏敏感信息

如果要在生成的文档中使用ommit敏感信息,可以在.scanapi.yaml文件中对其进行配置。目前,它只适用于头文件。

docs:hide:headers:-Authorization

你可以找到更多的例子here

可用键

KEYDescriptionTypeScopes
apiIt is reserver word that marks the root of the specification and must not appear in any other placedictroot
bodyThe HTTP body of the requestdictrequest
base_urlThe API’s base URLstringapi
endpointsIt represents a list of API endpointslistapi, endpoint
headersThe HTTP headersdictapi, endpoint, request
methodThe HTTP method of the request (GET, POST, PUT or DELETE)stringrequest
nameAn identifierstringendpoint, request
pathA part of the URL path that will be concatenated with the base URL and possible other pathsstringendpoint, request
requestsIt represents a list of HTTP requestslistapi, endpoint
varsKey used to define your custom variables to be used along the specificationdictrequest
${custom var}A syntax to get the value of the custom variables defined at key ^{}stringrequest - after ^{} definition
${ENV_VAR}A syntax to get the value of the environment variables defined at ^{} filestringapi, endpoint, request
${{python_code}}A syntax to get the value of a Python code expressionstringrequests

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java从Aparabi线程返回   为什么这个正则表达式可以使用Java而不能使用Groovy   java密钥管理器连接早期刷新令牌   数组。lang.ArrayIndexOutofBounds异常:4   java嗅探访问的站点并保存到文件   java从一个特定子字符串后开始的字符串中获取两个双倍子字符串   JavaAndroid:SharedReferences。如何正确地实施它们   java通过Flatworm创建列表   java如何在TestNG框架中的@BeforeClass方法中使用数据驱动测试   java标题视图bug我不明白   Java中的Java解释器或编译器   java如何将一维索引转换为多维数组中的相应索引?   Java泛型警告。util。收藏   java JSP页面在不合适的地方创建JSESSIONID cookie   配置javaapachelog4jlog4j。xml配置多个错误   无法设置java EditText焦点   java将文本框的标签放在框的上方,而不是侧面   如何使用Java8处理可空列表?   java将3x3 2D数组插入9x9 2D数组