ccy*_*ccy 19 ruby rest cucumber web-api-testing
尝试编写REST API测试的黄瓜功能步骤.
我不确定哪种方法更好:
Given I log in with username and password
When I add one "tv" into my cart
And I check my cart
Then I should see the item "tv" is in my cart
Run Code Online (Sandbox Code Playgroud)
要么
Given the client authenticate with username and password
When the client send POST to "/cart/add" with body "{item: body}"
Then the response code should be "200"
And the response body should expect "{success: true}"
When the client send GET to "/cart"
Then the response code should be "200"
And the response body should expect "{"items": ["tv"]}"
Run Code Online (Sandbox Code Playgroud)
当人们试图为REST API编写黄瓜步骤时,是否有任何约定?
bit*_*her 13
我偶然发现了这篇有用的文章:http://gregbee.ch/blog/effective-api-testing-with-cucumber
总结一下......
Scenario: List fruit
Given the system knows about the following fruit:
| name | color |
| banana | yellow |
| strawberry | red |
When the client requests a list of fruit
Then the response is a list containing 2 fruits
And one fruit has the following attributes:
| attribute | type | value |
| name | String | banana |
| color | String | yellow |
And one fruit has the following attributes:
| attribute | type | value |
| name | String | strawberry |
| color | String | red |
Run Code Online (Sandbox Code Playgroud)
针对JSON验证结果是一项棘手的工作,因为如果结果是数组,则元素可能与您在测试中验证的顺序不同.
这是一个(足够接近)例子,实用程序员的"黄瓜书"说通过Cuke测试REST API,它似乎与你的第二个例子更紧密相关:
Feature: Addresses
In order to complete the information on the place
I need an address
Scenario: Addresses
Given the system knows about the following addresses:
[INSERT TABLE HERE or GRAB FROM DATABASE]
When client requests GET /addresses
Then the response should be JSON:
"""
[
{"venue": "foo", "address": "bar"},
{ more stuff }
]
"""
STEP DEFINITION:
Given(/^the system knows about the following addresses:$/) do |addresses|
# table is a Cucumber::Ast::Table
File.open('addresses.json', 'w') do |io|
io.write(addresses.hashes.to_json)
end
end
When(/^client requests GET (.*)$/) do |path|
@last_response = HTTParty.get('local host url goes here' + path)
end
Then /^the response should be JSON:$/ do |json|
JSON.parse(@last_response.body).should == JSON.parse(json)
end
ENV File:
require File.join(File.dirname(__FILE__), '..', '..', 'address_app')
require 'rack/test'
require 'json'
require 'sinatra'
require 'cucumber'
require 'httparty'
require 'childprocess'
require 'timeout'
server = ChildProcess.build("rackup", "--port", "9000")
server.start
Timeout.timeout(3) do
loop do
begin
HTTParty.get('local host here')
break
rescue Errno::ECONNREFUSED => try_again
sleep 0.1
end
end
end
at_exit do
server.stop
end
Run Code Online (Sandbox Code Playgroud)
我一直在使用黄瓜进行测试,更重要的是记录我rails-api在当前项目中使用的API .我四处寻找可以使用的工具,最后我使用了cucumber-api-steps和json_spec的组合.它对我很有用.
关于如何写黄瓜步骤没有惯例.您编写步骤的方式取决于您希望如何使用黄瓜套件.我使用黄瓜输出作为我们的Angular JS客户端开发人员实现API客户端的参考.所以我的黄瓜步骤包含实际的JSON请求和响应以及每个场景的状态代码.这使得在发生变化时(特别是当客户端团队没有亲自到我的工作场所时)与客户端团队进行沟通非常容易.
每次我创建或更新API时,CI服务器都会将黄瓜作为构建的一部分运行,并将HTML格式的输出移动到可以在浏览器中打开的"build_artifacts"位置.客户端开发人员总是会以这种方式获得最新的引用.
我在博客文章中写了关于创建经过测试,记录和版本化的JSON API的所有内容,希望它能以某种方式帮助您.
Cucumber最初的意图之一是为其设计做出贡献,它将弥合技术实施与了解业务需求的人之间的差距,以便非开发人员可以编写和/或理解测试描述.因此,它不适合详细的技术规格或逐个吹扫单元测试.
因此,这将指向我的第一个测试描述,如果这也是您使用Cucumber的原因.
实施第二个版本的测试没有什么大问题,Cucumber可以支持它.可能不需要解析大量的语句类型.但是你最终可能会稍微与测试框架作斗争,或者首先违背你使用Cucumber的理由.
至于一个约定,我不知道在实践中有足够的REST API测试来评论,我所看到的测试中没有一个使用Cucumber作为框架.
更新:浏览主题的SO,我确实找到了这个链接:https://github.com/jayzes/cucumber-api-steps,它更类似于你的第二种格式.
| 归档时间: |
|
| 查看次数: |
26986 次 |
| 最近记录: |