`

用Cucumber测试Emails(等待翻译)

阅读更多

 

 

http://drnicwilliams.com/2009/03/26/testing-outbound-emails-with-cucumber/

 

  My testimonial for Cucumber still stands even in 2009. In fact I promise to let you know when I don’t think Cucumber is the bees-knees of integration testing. I love the step-by-step English instructions of user usage scenarios backed by a simple Ruby DSL for describing real actions on your application for each step.

For testing Rails apps, Cucumber defaults to using Webrat on top of Rails’ own integration sessions. With Webrat you actually test that your views match to your controller actions. If I click the “Submit” button, then it checks that it invokes an available action correctly. Before Cucumber I could appreciate the merit of Webrat alone, but the two tools combined are instant superheroes of my Rails development crime fighting team. That is, like any superhero TV show at least once every 30 minutes you find yourself saying “Thank you Mr Cucumber and your trusty sidekick Webrat, you’re my heroes”. (BTW, I’m not alone in this analogy)

Testing emails

But I wanted to test emails. More interestingly, emails containing links back into my application. Like an activation email on sign up.

Specifically, I wanted a cucumber scenario like this:

Scenario: Signup for new account
  Given I am on the signup form
  When I fill in "Email" with "drnic@mocra.com"
  And I press "Join"
  Then I should see "An activation email has been sent"
  And I should receive an activation email
  When I click the activation email link
  Then I should see "Your account is active"

Within this scenario there are 7 steps. Lines 2, 3, 4 and 7 match to steps from the generated webrat_steps.rb file when you install cucumber.

Line 1 also matches to a webrat step definition. But it requires that you define what “the signup form” maps to in your routes. So you need to update features/support/paths.rb to specify what “the signup form” url is:

module NavigationHelpers
  def path_to(page_name)
    case page_name
    when /the signup form/
      signup_path
    end
  end
end

So, that leaves us with lines 5 and 6.

email-spec plugin

I’m happy using the email-spec gem/plugin from Ben Mabey , who is also the lead maintainer of the awesome Cucumber TextMate bundle and did a wonderful presentation on Outside-In Development with Cucumber at the recent Mountain West RubyConf (lots of great videos available )

To install as a plugin:

script/plugin install git://github.com/bmabey/email-spec.git

Then add the following line to your *feature/support/env.rb* file:

require 'email_spec/cucumber'

Finally, the plugin comes with some bonus cucumber step definitions which wrap around lots of nice helpers:

script/generate email_spec

Using email-spec step definitions

Let’s ignore my desired lines 5 and 6 above and use the step definitions that we get with email-spec. We can replace the two lines with the following:

And I should receive an email
When I open the email
Then I should see "Please activate your new account" in the subject
When I click the first link in the email

Update: The last line didn’t used to be in email-spec but it now is. I’ve removed the example from this article.

I did all this for an existing application and every line of the scenario tested positive/green. Yay!

Refactoring four steps into two

For the sake of demonstration, you now might want to refactor these four steps into two steps to keep your scenarios nice and readable.

That is, how can we convert these 4 steps into our original lines 5 and 6?

The quickest way is to copy and paste the lines and slap some quotes around the text:

Then /^I should receive an activation email$/ do |email|
  Then 'I should receive an email'
  When 'I open the email'
  Then 'I should see "Please activate your new account" in the subject'
end

When /^I click the activation email link$/ do
  When 'I click the first link in the email'
end

That’s right, you can use Given /When /Then as invocation methods as well as step definition methods. If you don’t pass these methods a do…end block then they will match/find/invoke a step rather than define a new one. Very cool.

Alternately, you could implement the two steps using the underlying email helper methods provided by the email-spec plugin:

Then /^I should receive an activation email$/ do
  unread_emails_for(current_email_address).size.should == 1
  open_email(current_email_address)
  current_email.should have_subject(/Please activate your new account/))
end

When /^I click the activation email link$/ do
  click_first_link_in_email
end

Your choice.

Configuring current_email_address

All the email-spec helper methods assume that the “I” in the scenario has an email address. It uses a method current_email_address for this.

You must change the current_email_address method in the email_steps.rb file to pull out email addresses from wherever they might be located within any given scenario.

For example, here is one definition of the method from one of my projects:

def current_email_address
  @email || (@current_user && @current_user.email) || "drnic@mocra.com"
end

Summary

分享到:
评论

相关推荐

    cucumber_配置指南

    接着安装 Cucumber 本身 (`gem install cucumber`),以及 RSpec,因为 Cucumber 常与 RSpec 结合使用来提供断言功能 (`gem install rspec`)。此外,还需要安装 Watir 和 Watir-Webdriver 以进行浏览器自动化测试 (`...

    cucumber.jar

    Cucumber的核心理念是将测试描述为人类可读的故事,这些故事称为特性文件,通常使用Gherkin语法编写。Gherkin是一种业务领域特定的语言,它的设计目的是使非技术人员也能理解测试用例。在提供的压缩包中,gherkin-...

    cucumber-debugging:Cucumber 测试的调试

    Cucumber 是一种流行的自动化测试工具,特别是在BDD(行为驱动开发)中,它允许开发者、测试人员和非技术人员使用自然语言编写可执行的规范。在Java项目中,Cucumber经常被用来验证软件功能是否按预期工作。然而,当...

    自动化测试平台cucumber框架兼容APP,WEB,API

    Appium是一个跨平台的测试框架,允许使用Cucumber编写测试脚本来控制iOS和Android设备。通过这种方式,开发者可以模拟用户交互,检查应用程序的功能和性能。 **在WEB测试中的应用**: 在Web测试中,Cucumber可以与...

    Cucumber-rust:Rust的Cucumber测试框架。 完全本机,没有外部测试运行程序或依赖项

    使用Cucumber-rust进行测试时,你可以通过Cargo命令运行测试集,框架会自动匹配并执行所有关联的Gherkin步骤。`cucumber-rust`还支持断言库、日志记录、调试选项以及测试报告生成,从而提供了一个全面的测试环境。 ...

    软件测试_cucumber交流.pdf

    此外,Cucumber还支持使用辅助工具(如Cucumber-JVM、Cuke4Duke等)将自然语言编写的测试用例自动转化为测试脚本,从而与实际的软件应用程序进行交互。这种测试脚本的自动执行,帮助团队在软件开发过程中快速发现不...

    cucumber jar包

    总之,cucumber jar 包是 Cucumber 测试框架的核心组成部分,使得开发者能够使用结构化自然语言进行自动化测试,提高了测试的可读性和可维护性,同时降低了非技术人员参与测试的门槛。通过合理使用 cucumber.jar 和...

    cucumber自动化测试官方教程

    在Java平台上使用Cucumber进行自动化测试,首先需要创建一个Maven工程,并在`pom.xml`文件中引入Cucumber的相关依赖。创建Cucumber项目的一个便捷方式是使用`cucumber-prototype` Maven插件。在终端中,导航至想要...

    cucumber-jvm源码 BDD测试框架

    Cucumber是一个自动化测试工具,它属于行为驱动开发(BDD)的范畴。这个工具的特点是可以用自然语言来描述测试用例,因此...开发人员可以使用Cucumber进行公共方法的封装,而功能测试人员则可以专注于编写测试用例。

    基于cucumber的BDD实现【java、pythion】

    Cucumber是一个开源的BDD工具,它允许开发者使用一种名为Gherkin的简单语言编写可执行的测试用例。Gherkin语言的关键词包括Feature(特性)、Scenario(场景)、Given(给定)、When(当)、Then(那么)等。通过...

    Cucumber Recipes

    根据给出的部分内容,这本书不仅覆盖了基本的Cucumber使用方法,还包括了一些更为高级和专业的技巧和窍门,比如如何处理难以驾驭的测试套件,如何在多个服务器间进行扩展测试,甚至包括了针对嵌入式Arduino硬件项目...

    cucumber-automation:这是一个Cucumber自动化测试框架演示

    Cucumber以其易读性著称,因为它允许用自然语言编写测试场景,使得非技术人员也能理解测试目标。 首先,Cucumber是基于Gherkin语法的,这是一种结构化的自然语言,用于描述软件的功能。它的基本结构包括Feature文件...

    cucumber-stddtool:将Cucumber测试结果报告给 STDDTool 的Cucumber格式化程序

    项目移至 LEARNINGWELL 内部 GITLAB 服务器Cucumber STDDTool 格式化程序将测试结果发送到 STDDTool 的 -formatter开始: 在您的 Gemfile 中: gem 'stddtool' 并在您的/support/env.rb文件或支持目录下的任何其他...

    springboot-cucumber-test:Cucumber弹簧靴的测试项目

    6. **数据层测试**: SpringBoot通常与Spring Data一起使用,我们可以用Cucumber测试数据库操作,比如验证保存、查询和删除数据的逻辑。 7. **断言库**: Cucumber测试中会使用JUnit或其他断言库(如AssertJ)来验证...

    Springboot Cucumber测试配置介绍详解

    主要介绍了Springboot Cucumber测试配置介绍详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    The Cucumber book (English Version)

    - **验收测试**:客户或产品所有者可以使用Cucumber编写验收标准,确保软件满足业务需求。 - **文档化需求**:Cucumber的测试案例本身就是一种文档,可以作为项目需求的一部分,方便团队成员理解和追踪需求变化。 #...

    Maven 搭建cucumber 简单示例

    Maven 搭建cucumber 简单示例 1.新建Maven 项目 2.Eclipse安装 cucumber 插件:Help->Install New Software->Add name:cucumber location:http://cucumber.github.com/cucumber-eclipse/update-site 3.导入示例项目,...

    jest-cucumber-fusion:以自然的方式编写Cucumber测试,但仍然使它们成为玩笑的一部分(包括覆盖率)

    在,Jest-Cucumber-Fusion使用整齐的Cucumber样式来处理相应的Jest测试步骤。 您不必为每个场景编写一个Jest测试,而无需使用describe和it块,然后在Jest测试中定义Given , When和Then步骤定义。 然后,Jest-...

    Cucumber:用于SBT的Cucumber测试框架和插件(Scala中的BDD测试)

    在Scala开发环境中,结合使用SBT(Simple Build Tool)构建工具,Cucumber可以帮助开发者编写易于理解的自然语言测试用例,从而实现更高效的沟通和协作。 ### 1. Cucumber的核心概念 - **Gherkin语言**:Cucumber...

    使用Cucumber+Rspec玩转BDD全集.rar

    在他们下载的chm始终打不开或有问题。 ... 使用Cucumber+Rspec玩转BDD(1)——用户注册 使用Cucumber+Rspec玩转BDD(2)——邮件激活 ...使用Cucumber+Rspec玩转BDD(3)——用户...使用Cucumber+Rspec玩转BDD(7)——测试重构

Global site tag (gtag.js) - Google Analytics