以bdd方式测试restapi的工具链。
bddrest的Python项目详细描述
bddrest
用于在bdd中定义和验证rest api的工具链。
目录
快速启动
写作测试
使用Given
,when
函数,如下面的示例所示,您可以
确定并断言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