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。
可用键
KEY | Description | Type | Scopes |
---|---|---|---|
api | It is reserver word that marks the root of the specification and must not appear in any other place | dict | root |
body | The HTTP body of the request | dict | request |
base_url | The API’s base URL | string | api |
endpoints | It represents a list of API endpoints | list | api, endpoint |
headers | The HTTP headers | dict | api, endpoint, request |
method | The HTTP method of the request (GET, POST, PUT or DELETE) | string | request |
name | An identifier | string | endpoint, request |
path | A part of the URL path that will be concatenated with the base URL and possible other paths | string | endpoint, request |
requests | It represents a list of HTTP requests | list | api, endpoint |
vars | Key used to define your custom variables to be used along the specification | dict | request |
${custom var} | A syntax to get the value of the custom variables defined at key ^{ | string | request - after ^{ |
${ENV_VAR} | A syntax to get the value of the environment variables defined at ^{ | string | api, endpoint, request |
${{python_code}} | A syntax to get the value of a Python code expression | string | requests |