2012-01-16 73 views
1

我目前正在尝试将黄瓜和水豚一起用于Web应用的一些集成测试。黄瓜动态加载数据表

有一个测试,我只想点击网页应用程序的所有(或大部分)页面,查看是否没有错误返回。我希望能够看到哪些页面无法正常工作。

我认为,方案轮廓将是最好的办法,所以我以这种方式开始:

Scenario Outline: Checking all pages pages 

    When I go on the page <page> 
    Then the page has no HTTP error response 

    Examples: 
     | page      | 
     | "/resource1"    | 
     | "/resource2"    | 
     ... 

我现在有82页,并且工作正常。

但是,我发现这种方法不可维护,因为可能会删除新的资源和资源。

更好的方法是从某处加载表中的数据(解析索引页的HTML,数据库等)。

但我没有弄清楚如何做到这一点。

我碰到一个article about table transformation但我无法弄清楚如何在场景大纲中使用这种转换。

有什么建议吗?

好,因为存在一些混淆。如果你看看上面的例子。所有我想要做的是改变它,这样的表几乎是空的:

Scenario Outline: Checking all pages pages 

    When I go on the page <page> 
    Then the page has no HTTP error response 

    Examples: 
    | page      | 
    | "will be generated"   | 

然后我想补充一个转型看起来是这样的:

Transform /^table:page$/ do 
    all_my_pages.each do |page| 
    table.hashes << {:page => page} 
    end 
    table.hashes 
end 

我指定的转换在同文件,但它没有被执行,所以我假设转换不适用于Scenario轮廓。

+0

是的,Transform只能在不包含大纲表的步骤参数表上运行。 – mschneider 2012-01-25 13:54:53

回答

3

黄瓜确实是该任务的错误的工具,你应该在功能方面描述的功能。如果你想以编程方式描述行为,你应该使用rspec或test-unit之类的东西。

另外,您的场景步骤应该是描述性的,并且像书面文本一样专用,而不是像编程语言中使用的抽象短语。他们不应该包含“附带的细节”,比如资源的确切url或它的id。

请仔细阅读http://blog.carbonfive.com/2011/11/07/modern-cucumber-and-rails-no-more-training-wheels/,看http://skillsmatter.com/podcast/home/refuctoring-your-cukes

关于你对“插入表格”的问题,是有可能的,如果你 平均增加额外的排它,事实上,你可以做任何你用它喜欢。 变换块的结果完全取代原始表格。

Transform /^table:Name,Posts$/ do 
    # transform the table into a list of hashes 
    results = table.hashes.map do |row| 
    user = User.create! :name => row["Name"] 
    posts = (1..row["Posts"]).map { |i| Post.create! :title => "Nr #{i}" } 
    { :user => user, :posts => posts } 
    end 
    # append another hash to the results (e.g. a User "Tim" with 2 Posts) 
    tim = User.create! :name => "Tim" 
    tims_posts = [Post.create! :title => "First", Post.create! :title => "Second"] 
    results << { :user => tim, :posts => tims_posts } 
    results 
end 

Given /^I have Posts of the following Users:$/ do |transformation_results| 
    transformation_results.each do |row| 
    # assing Posts to the corresponding User 
    row[:user].posts = row[:posts] 
    end 
end 

你可以与场景结合本大纲是这样的:

Scenario Outline: Paginate the post list of an user at 10 
    Given I have Posts of the following Users: 
    | Name | Posts | 
    | Max | 7  | 
    | Tom | 11 | 
    When I visit the post list of <name> 
    Then I should see <count> posts 
Examples: 
    | name | count | 
    | Max |  7 | 
    | Tom | 10 | 
    | Tim |  2 | 

这应该demonstarte为什么“加入”行的表,可能不是最好的做法。

请注意,这是不可能扩大示例代码中的表内:

Scenario Outline: Paginate the post list of an user at 10 
    Given I have Posts of the following Users: 
    | Name | Posts  | 
    | <name> | <existing> | # won't work 
    When I visit the post list of <name> 
    Then I should see <displayed> posts 
Examples: 
    | name | existing | displayed | 
    | Max |  7 |   7 | 
    | Tom | 11 |  10 | 
    | Tim |  2 |   2 | 
+0

在我提供的示例中,即使一条路径失败,测试也会继续。之后,我可以看到测试失败的参数。所以我的代码在任何方面都不是这样。我现在唯一需要的就是生成表格的转换。 – leifg 2012-01-23 09:04:37

+0

请详细说明“表的生成位置”。你想生成小黄瓜的源代码?或者你想在接受表格的步骤中使用Outline标签(如)?第一个不应该完成,第二个是不可能的。 – mschneider 2012-01-23 16:52:05

+0

我喜欢做的是类似于这个例子(在我的情况下添加数据的表,或插入不可能?):http://www.claytonlz.com/index.php/2010/01/cucumber-table - 转换/但我想在场景大纲中使用它。 – leifg 2012-01-24 08:59:20

0

我认为更好的方法是使用不同的工具,只是为了抓取您的网站,并检查是否没有错误返回。假设您正在使用Rails

您可能会考虑的工具是:Tarantula

https://github.com/relevance/tarantula

我希望帮助:)

+0

感谢您的建议,但是有几个原因导致我无法使用Tarantula(集成测试与应用程序分离,应用程序不支持Rails3应用程序)。除此之外,在我不想仅抓取网站的其他场景中,生成数据表格可能很有用。 – leifg 2012-01-16 10:50:13

0

快速黑客是改变的例子收集代码,并使用红宝石的EVAL运行自定义红宝石功能覆盖默认收集实例数据,这里是代码: generate-dynamic-examples-for-cucumber

缺点:需要改变scenario_outline.rb文件。

1

对于动态加载数据的具体情况,这里有一个建议:

  1. ,A级,比方说PageSets,有方法,例如all_pages_in_the_sitemap_errorcountdeveloping_countries_errorcount

  2. 一步,上面写着类似

    Given I am on the "Check Stuff" page 
    Then there are 0 errors in the "developing countries" pages 
    

Then there are 0 errors in "all pages in the sitemap" 

Then步骤串"developing countries"转换成一个方法名developing_countries_errorcount并试图调用它PageSets类。在这种情况下,步骤期望所有_errorcount方法返回一个整数。返回像地图这样的数据结构为编写简洁的动态步骤提供了许多可能性。

对于更多静态数据,我们发现YAML对于使我们的测试自我记录和自我验证非常有用,并且帮助我们去除难以维护的文字,例如“5382739”,我们都忘记了三周后。

YAML格式为便于阅读,如有必要可评论(它通常不是。)

不应该这样写:

Given I am logged in as "[email protected]" 
And I select the "History" tab 
Then I can see 5 or more "rows of history" 

我们可以改为写:

Given I am logged in as "a user with at least 5 items of history" 
When I select the "History" tab 
Then I can see 5 or more "rows of history" 

在文件logins.yaml ....

a member with at least 5 items of history: 
    username: [email protected] 
    password: WalRus 

我们使用YAML来保存与成员,供应商,政策等各种实体相关的数据集...该列表正在不断增加:列表正在不断增加:

在文件test_data.yaml ...

a member who has direct debit set up: 
    username: [email protected] 
    password: WalRus 
    policyId: 5382739 
    first name: Jack 
    last name: Robinson 
    partner's first name: Sally 
    partner's last name: Fredericks 

如果您需要验证文本,那么也值得看看YAML的multi-line text facilities。虽然这对于自动化测试并不常见,但它有时可能很有用。