黄瓜 vs 蚂蚁宝宝

49

有人能解释一下这两个平台的区别吗?

它们都属于BDD,但为什么我应该使用一个还是另一个,或者两个都使用?

4个回答

105

Capybara 是一个可以模拟人类访问网站的工具(如访问 URL、点击链接、在表单中输入文本并提交)。它用于模拟用户在网站上的操作流程。使用 Capybara,您可以编写以下代码:

describe "the signup process", :type => :feature do
  before :each do
    User.make(:email => 'user@example.com', :password => 'caplin')
  end

  it "signs me in" do
    visit '/sessions/new'
    within("#session") do
      fill_in 'Login', :with => 'user@example.com'
      fill_in 'Password', :with => 'password'
    end
    click_link 'Sign in'
    page.should have_content 'Success'
  end
end

黄瓜是一种工具,可用于编写映射到代码的可读性强的测试。使用它,您可以像这样重写上面的示例:

Scenario: Signup process

Given a user exists with email "user@example.com" and password "caplin"
When I try to login with "user@example.com" and "caplin"
Then I should be logged in successfully

这个近乎纯文本的解释有助于传达给非开发人员,但也需要一些代码与之配合才能实际工作(步骤定义)。

通常情况下,如果你正在测试一个网站,你会使用Capybara;如果你需要与非开发人员共享这些测试,则会使用Cucumber。这两个条件是独立的,因此你可以单独使用其中一个或两个或没有。

PS:在代码片段中还有一些RSpec。这是必需的,因为Cucumber或Capybara本身无法测试某些东西。它们依赖于RSpec、Test::Unit或minitest来完成实际的“通过或失败”工作。


7

黄瓜是一种BDD工具,用业务可读的领域特定语言表达测试场景。

Capybara是一个自动化测试工具(经常用于)ROR应用程序。

在Capybara的Github页面上,有一个关于如何使用Capybara和黄瓜的例子。


8
Capybara不仅适用于ROR,它可用于测试使用任何编程语言编写的Web应用程序。 - Andy Waite

5

Cucumber是一种通用的BDD工具,它对Web应用程序一无所知。因此,Cucumber步骤定义调用Capybara来测试Web应用程序。


0
|-------------------------------|-------------------------------|
|      Cucumber                 |     Capybara                  |
|-------------------------------|-------------------------------|
| Test cases are more readable  | Test cases are not readable;  |
| and written in plain english  | Capybara also wraps RSpec and |
| text as a DSL                 | you follow the same pattern to|
|                               | write test cases              |
|-------------------------------|-------------------------------|
| Easy to iterate data using    | Not easy to iterate data      |
| tables                        |                               |
|-------------------------------|-------------------------------|
| Cucumber has its own runner   | Uses RSpec runner to execute  |
|                               | tests                         |
|-------------------------------|-------------------------------|
| Default HTML reporter         | No default HTML reporter      |
|-------------------------------|-------------------------------|
| Need to learn cucumber regex  | No such concept               |
| pattern to write step-        |                               |
| definition which is the middle|                               |
| man for test case and script. |                               |
| Btw, the regex pattern is not |                               |
| essential for all cases.      |                               |
|-------------------------------|-------------------------------|
| You can use the native        | (Wrapper)/Built on top of     |
| selenium-webdriver methods    | selenium-webdriver ruby       |
| while using Cucumber; Cucumber| library when used with        |
| is just an additional         | selenium; it can also be used |
| framework to your generic     | with two other drivers.       |
| automation framework          |                               |
|-------------------------------|-------------------------------|
| Pure BDD framework (In theory | Traditional functional test   |
| BDD sounds great. In practice,| model for end-to-end testing  |
| product owners and developers |                               |
| rarely continue to use BDD)   |                               |
|-------------------------------|-------------------------------|

这是一个漂亮的回答,但很遗憾大部分都是错误的。例如,cucumber不使用本地的selenium组件,可以在没有selenium的情况下使用capybara,capybara与测试用例或页面对象无关,你不需要学习正则表达式来编写步骤定义等等。恕我直言,我建议您撤回这个答案。 - diabolist
@diabolist 感谢您宝贵的反馈;我同意其中的一些观点并进行了调整;同时,我还添加了一些之前被忽略的额外观点。 - Prashanth Sams
Capybara与RSpec(测试用例)有关; 这是我不同意的两个观点。 - Prashanth Sams
Capybara可以使用Selenium,但它并不一定需要这样做。例如,它可以使用Rack驱动程序。 - diabolist
你可以有效地使用Cuke而不使用正则表达式。我已经使用Cucumber进行测试比Cucumber的整个生命周期还要长(从Rspec Plain Text stories开始,这是Cucumber的前身)。在过去的5年左右中,我没有在我的步骤定义中使用正则表达式。 - diabolist
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接