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

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

问题描述

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

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"

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

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.

如何在此处继续进行,以便在不为第一个场景发出 HTTP 请求的情况下继续测试?或者我应该运行一次并在其余场景中保留 @result 数组,以便我可以继续使用实际结果集?

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?

推荐答案

这是故意很难做到的!在测试之间共享状态通常是一件非常糟糕的事情,尤其是因为它会强制您的测试按顺序运行(您的第一个场景必须在后续场景之前运行,而不是 Cucumber 明确支持的).

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).

我的建议是重新考虑您的测试策略.在测试中使用外部服务是使它们运行缓慢且不可靠的好方法(当外部服务出现故障时会发生什么?).在这种情况下,我建议使用 webmockvcr 创建外部站点的假版本,它返回的响应与您对真实站点的期望相同,但您可以随意点击多次,而不必担心性能或不可用性.

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天全站免登陆