Ruby RESTAPI测试步骤最佳实践
尝试编写RESTAPI测试的特性步骤 我不确定哪种方法更好:Ruby RESTAPI测试步骤最佳实践,ruby,rest,cucumber,web-api-testing,Ruby,Rest,Cucumber,Web Api Testing,尝试编写RESTAPI测试的特性步骤 我不确定哪种方法更好: 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 或 当人们试图为REST API编写cucumber步骤时,有什么约定可以遵循吗?cucumber的一个初衷是弥合技术实现和了解业务需求的人之间的差距,以
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
或
当人们试图为REST API编写cucumber步骤时,有什么约定可以遵循吗?cucumber的一个初衷是弥合技术实现和了解业务需求的人之间的差距,以便非开发人员可以编写和/或理解测试描述。因此,它不太适合详细的技术规范或逐个单元测试 因此,如果这也是您使用Cucumber的原因,那么这将指向您的第一个测试描述 实现像第二个版本这样的测试没有什么大问题,Cucumber可以支持它。您可能也不需要解析大量的语句类型。但是,您可能最终会与测试框架发生一些冲突,或者首先违背您使用Cucumber的基本原理 至于约定,我不知道实践中有足够多的RESTAPI测试可以评论,而且我所看到的测试中没有一个使用Cucumber作为框架 更新:浏览一下这个主题,我确实找到了一个链接:它更类似于您的第二种格式。这里有一个(足够接近)的例子,说明了实用主义程序员的《黄瓜书》中关于通过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
我一直在使用cucumber进行测试,更重要的是记录我在当前项目中使用
rails API
创建的API。我四处寻找一些工具来使用,最后我使用了和的组合。这对我很有效
关于如何编写黄瓜步骤,没有约定。编写步骤的方式取决于您希望如何使用cucumber套件。我使用cucumber输出作为Angular JS客户端开发人员实现API客户端的参考。因此,我的cucumber步骤包含实际的JSON请求和响应,以及每个场景的状态代码。这使得每当发生变化时(特别是当客户端团队不在我的工作场所时),与客户端团队沟通变得非常容易
每次我创建或更新API时,CI服务器都会将cucumber作为构建的一部分运行,并将HTML格式的输出移动到可以在浏览器中打开的“构建工件”位置。客户端开发人员总是以这种方式获取最新的引用
我将所有这些都写在了一篇文章中,希望它能对您有所帮助。我刚刚偶然发现了这篇有用的文章: 总而言之
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 |
根据JSON验证结果是一项棘手的工作,因为如果结果是一个数组,那么元素的顺序可能与测试中验证的顺序不同
编辑:使用finderAUT的注释更新链接。谢谢 我认为第一个更好。我会把技术放在ruby类和模块中。例如,像模块cart.add(项目)在when步骤和then步骤中放入expect(cart.item)。包括('items'=>a_字符串_匹配(项目)) 这样,ruby类和模块可以在其他功能步骤中重用。例如,您可能有另一个场景,将多个项目添加到购物车中,然后验证总金额
然而,第二个1我认为可以使它像技术功能。例如,在所有api中都会出现公共/全局标头或正文请求。请参见此处:。它提供了Cucumber DSL来测试RESTful API。现在有一些库用于在Ruby中使用Cucumber进行服务器端REST测试。这里有几个:
- 。(推荐)
- 。这是一个小教程
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add” with the following:
| item | body |
Then the response status should be “200”
And the JSON response should have "success" with the text "true"
When I send a GET request to “/cart”
Then the response status should be “200”
And the JSON response should be "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the JSON response should have "firstName" with the text "Katie"
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add”
And I set JSON request body to '{item: body}'
Then the response status should be “200”
And the response should have key “success” with value “true”
When I send a GET request to “/cart”
Then the response status should be “200”
And the response should follow "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the response should have key “firstName”
下面是我使用“cucumber api步骤”进行测试的情况:
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add” with the following:
| item | body |
Then the response status should be “200”
And the JSON response should have "success" with the text "true"
When I send a GET request to “/cart”
Then the response status should be “200”
And the JSON response should be "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the JSON response should have "firstName" with the text "Katie"
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add”
And I set JSON request body to '{item: body}'
Then the response status should be “200”
And the response should have key “success” with value “true”
When I send a GET request to “/cart”
Then the response status should be “200”
And the response should follow "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the response should have key “firstName”
以下是我如何使用“cucumber api”编写测试的方法:
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add” with the following:
| item | body |
Then the response status should be “200”
And the JSON response should have "success" with the text "true"
When I send a GET request to “/cart”
Then the response status should be “200”
And the JSON response should be "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the JSON response should have "firstName" with the text "Katie"
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add”
And I set JSON request body to '{item: body}'
Then the response status should be “200”
And the response should have key “success” with value “true”
When I send a GET request to “/cart”
Then the response status should be “200”
And the response should follow "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the response should have key “firstName”
下面是我使用“cucumber api”进行测试的情况:
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add” with the following:
| item | body |
Then the response status should be “200”
And the JSON response should have "success" with the text "true"
When I send a GET request to “/cart”
Then the response status should be “200”
And the JSON response should be "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the JSON response should have "firstName" with the text "Katie"
@success
Scenario: Successfully add to cart
Given I am logged in
When I send a POST request to “/cart/add”
And I set JSON request body to '{item: body}'
Then the response status should be “200”
And the response should have key “success” with value “true”
When I send a GET request to “/cart”
Then the response status should be “200”
And the response should follow "{'items': ['tv']}"
@success
Scenario: Successfully log in
Given I am logged out
When I send a POST request to “/login” with:
| username | katie@gmail.com |
| password | mypassword |
Then the response status should be “200”
And the response should have key “firstName”
- 关于Cucumber API的注意事项:目前没有办法执行
。“有价值”部分尚未完成应具有值为“Katie”的键“firstName”
- “Follow”还需要一个JSON文件
另一个资源是,但它是旧的(2011)。我推荐您的第一个场景 根据我自己的经验,我个人认为,将BDD作为软件交付方法所获得的最大价值是,当您将重点放在业务价值上时 换句话说,场景应该是业务想要的行为的示例,而不是技术实现。这确保了开发是由业务目标驱动的,可交付成果符合他们的期望 这就是所谓的由外而内的开发 系统行为的附加测试可以而且应该用于满足技术要求,但我认为用自然语言编写这些测试的价值较小,在大量场景中,这通常是费时费力的 我建议采取以下办法: 1) 与BAs和POs合作,使用非特定于实现的语言(如您的第一个示例)开发他们想要的行为示例 2) 工程师使用这些方法从测试优先的方法推动开发,将它们作为集成测试自动化——大多数在浏览器下方(例如,针对REST API),大多数核心场景也通过浏览器(如果您正在开发) 3) 工程师使用单元测试对功能代码进行TDD,直到单元测试和BDD示例都通过 虽然我在投票