以bdd方式测试restapi的工具链。

bddrest的Python项目详细描述


bddrest

用于在bdd中定义和验证rest api的工具链。

PyPI StatusBuild StatusCoverage Status

目录

快速启动

写作测试

使用Givenwhen函数,如下面的示例所示,您可以 确定并断言REST API的行为。

^ {< CD4>},^ {< CD5>},^ {< CD6>}对象是当前的代理 正在用给定的(…)在中编写故事():context)及其响应。 以及Given和/或when之后的最后一个响应。

importreimportsysimportjsonfrombddrest.authoringimportGiven,when,response,statusdefwsgi_application(environ,start_response):path=environ['PATH_INFO']ifpath.endswith('/None'):start_response('404 Not Found',[('Content-Type','text/plain;charset=utf-8')])yieldb''else:start_response('200 OK',[('Content-Type','application/json;charset=utf-8')])result=json.dumps(dict(foo='bar'))yieldresult.encode()withGiven(wsgi_application,title='Quickstart!',url='/books/id: 1',as_='visitor')asstory:assertstatus==200assertstatus=='200 OK'assert'foo'inresponse.jsonassertresponse.json['foo']=='bar'when('Trying invalid book id',url_parameters={'id':None})assertresponse.status==404

倾倒Story

story.dumps()

产生:

base_call:as_:visitordescription:As a member I have to POST a book to the library.form:name:BDD Bookquery:a:bresponse:headers:-'Content-Type:application/json;charset=utf-8'json:foo:barstatus:200 OKtitle:Posting a bookurl:/books/:idurl_parameters:id:'1'verb:GETcalls:-response:headers:-'Content-Type:text/plain;charset=utf-8'status:404 Not Foundtitle:Trying invalid book idurl_parameters:id:None

你可以用Story.loads(yaml)从这个yaml重新加载这个故事。

有两种附加方法可供转储和加载到 从文件中:story.load(file)story.dump(file)

自动倾倒

您可以通过Given函数的autodump参数来配置自动转储:

:param autodump: A string which indicates the filename to dump the story, or
                 a `callable(story) -> filename` to determine the filename.
                 A file-like object is also accepted.
                 Default is `None`, meana autodumping is disabled by default.

自动文档

您可以通过Given函数的autodoc参数来配置自动文档:

:param autodoc: A string which indicates the name of documentation file, or
                 a `callable(story) -> filename` to determine the filename.
                 A file-like object is also accepted.
                 Default is `None`, meana autodoc is disabled by default.
                 Currently only markdown is supprted.

降价

您可以使用story.document([formatter_factory=MarkdownFormatter])生成文档 以任意格式,例如:Markdown

还有一个cli可以执行此操作:

bddrest document < story.yml > story.md
## Posting a book

### GET /books/:id

As a member I have to POST a book to the library.

### Url Parameters

Name | Example
--- | ---
id | 1

### Query Strings

Name | Example
--- | ---
a | b

### Form

Name | Example
--- | ---
name | BDD Book

### Response: 200 OK

#### Headers

* Content-Type: application/json;charset=utf-8

#### Body

```json
{"foo": "bar"}
```

## WHEN: Trying invalid book id

### Url Parameters

Name | Example
--- | ---
id | None

### Response: 404 Not Found

#### Headers

* Content-Type: text/plain;charset=utf-8

命令行界面

安装项目后,名为bddrest的命令将可用。

bddrest -h

为bddrest

启用bash自动完成

将其添加到您的.bashrc和/或$VIRTUAL_ENV/bin/postactivate中。

eval"$(register-python-argcomplete bddrest)"

模型服务器

当您需要为服务器提供特殊的yaml文件时,请使用如下命令。

bddrest mockupserver yamlfilename

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

推荐PyPI第三方库


热门话题
java如何从同步请求中获取响应代码   具有深度复制的类中的Java oop getter/setter   java LDAP“简单”用户身份验证(JNDI Tomcat)不稳定?   java将Jenkins jnlp代理设置为通过API以编程方式使用WebSocket   java如何获取在servlet筛选器的静态块中启动的静态类的实例?   前两个数字的java charAt循环不能循环它   java在Spring引导执行器中是否有检查子服务运行状况的标准方法?   java我可以将jacksonmapped@JsonProperties推到“顶层”吗?   json JAVA:opencsv随机读取CSV单元格   无第三方应用程序的java捆绑包JRE   使用openidConnectClient功能的WAS Liberty中出现java无效cookie标头错误   java如何在Restful Web服务中从Http Post获取数组?   java如何读取安卓开发的JSON url?   如何在java IO中打开包含汉字的文件?