是否可以将一个功能作为另一个功能的“前提条件”进行重复使用?

13

能否将一个功能用作另一个功能的“Given”步骤?

或者说我正在尝试做不该做的事情吗?

基本上,我的功能看起来像:

Scenario: Creating a basic account with valid details (happy path)
  Given I am on the "signup" page
  And I enter all the right details #this is shortened of about 20 steps for your reading ease
  When I press the button labelled "Sign up"
  Then I should see the text "Thanks for signing up"
  And I should have an email from "confirmation@mysite.com" titled "Confirm your account Michael"
  And the database should contain a record for the user marked as unconfirmed

Scenario: Confirming account via email
  Given I have created a basic account
  When I open the confirmation email and visit the url to confirm the account
  Then I should be logged in
  And the database should contain a record for the user makred as confirmed

每次完成一个功能后,我都会清除我的数据库,因为它们应该都可以单独运行...

我这样做是错的吗?

谢谢


可能重复:https://dev59.com/BGsz5IYBdhLWcg3w0bOl - Jon M
@JonM 很棒的相关问题链接。然而,虽然这些答案最终相似,但我认为这两个问题有点不同。你提到的问题是关于共享块,而这一个是关于从功能调用其他功能--这是 Cucumber 之前明确支持的但现在不再支持的功能。 - Todd A. Jacobs
3个回答

13

问题

您正在尝试重用一个场景。在Cucumber中,这已经不再受支持

除了这种方法的其他问题外,您的测试将变得更慢且相互依赖,因为您将会:

  1. 通过浏览器进行账户创建,并且
  2. 使所有测试都依赖于账户创建测试的成功。

不要这样做。

Cucumber方式

通常情况下,您应该编写独立的测试,尽管您可以重用步骤定义。因此,在一般情况下,您可能需要添加共享步骤,例如:

  1. 假设用户帐户“Test User”不存在
  2. 假设用户帐户“Test User”存在

然后可以根据需要在您的场景中包含它们。这种方法的好处是,这些步骤可以以编程方式创建或删除用户。

或者,如果您的大多数测试将在现有帐户上进行,则使用正确的用户fixture设置默认数据集。对于您将测试帐户创建的有限子集,只需添加一个场景背景,以驱动用户删除。


1

如果您正在使用Javascript,我已经创建了一个名为reuse-cucumber-scenarios的包,通过以下方式调用场景:

Given the scenario "@scenario_tag"

.

Given the scenario "@scenario_tag" with parameters
"""
{
  "1": ["step1_param1", "step1_param2"],
  "2": ["step2_param1", "step2_param2", "step2_param3"],
  "3": ["step3_param1", "step3_param2", "step3_param3"],
}
"""

用于创建Gherkin变量...

Given the variable "$variable_name" is equal to
"""
#JSON object
"""

或者创建场景函数并通过执行它们来调用它们...
Given the scenario "@$scenario_function_tag" where variable "$variable_name" is "value_to_replace"

以及更多...


0

目前,您可以使用以下方法:

Background:
  Given Some common setup
  And Some more setup

Scenario: one
  When setup1
  Then something1

Scenario: two 
  When setup2
  Then something2

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