如何在使用黄瓜的情况之间共享状态 [英] How to share state between scenarios using cucumber

查看:117
本文介绍了如何在使用黄瓜的情况之间共享状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能从外部网站导入文章。
在我的第一个场景中,我测试从外部网站导入链接列表。

 功能:网站
场景:在example.com搜索文章并返回链接
给定一个进口商
其URL是http://example.com
当我们搜索fordemo
然后导入器应该返回25个链接
其中一个链接应该是http://example.com/demo.html



在我的步骤中,我有一个 @result 数组中的25个链接。



在我的第二个场景中,我想使用其中一个链接,并测试我正确解析文章的事实。

我不想每次都去外部网站,特别是现在第一种情况通过。



如何继续这里,所以我可以继续测试,而不使HTTP请求第一个方案?或者我应该运行一次,并在其余的方案中保留 @result 数组,以便我可以继续使用实际的结果集?

解决方案

这是故意很难做的!测试之间的共享状态通常是非常糟糕的事情,特别是因为它强制您的测试顺序运行(您的第一个方案必须在后续的方案之前运行,而不是Cucumber明确支持的方法)。



我的建议是重新考虑你的测试策略。在测试中打外部服务是使它们运行缓慢和不可靠的一个好方法(当外部服务关闭时会发生什么?)。在这种情况下,我建议您使用类似 webmock vcr 创建一个假的版本的外部网站,它返回相同的响应,你从真正的网站,但你可以打多次,而不必担心性能或不可用性。


I have a feature "Importing articles from external website". In my first scenario I test importing a list of links from the external website.

Feature: Importing articles from external website
  Scenario: Searching articles on example.com and return the links
    Given there is an Importer
    And its URL is "http://example.com"
    When we search for "demo"
    Then the Importer should return 25 links
    And one of the links should be "http://example.com/demo.html"

In my steps I have the 25 links in a @result array.

In my second scenario I want to take one of the links and test the fact that I parse the article correctly.

Now obviously I do not want to go to the external website every time, especially now that the first scenario passes.

How do I proceed here so I can keep testing without making the HTTP requests for the first scenario? Or should I run it once and persist the @result array across the rest of the scenarios so I can keep working with the actual result set?

解决方案

This is intentionally very difficult to do! Sharing state between tests is generally a Very Bad Thing, not least because it forces your tests to run in sequence (your first scenario MUST run before the subsequent ones, not something Cucumber supports explicitly).

My suggestion would be to re-think your testing strategy. Hitting external services in tests is a great way to make them run slowly and be unreliable (what happens when the external service goes down?). In this case I'd suggest using something like webmock or vcr to create a fake version of the external site, that returns the same response as you'd expect from the real site, but you can hit as many times as you like without the worry of performance or unavailability.

这篇关于如何在使用黄瓜的情况之间共享状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆