【问题标题】:Best practice to keep common steps of cucumber保持黄瓜常用步骤的最佳做法
【发布时间】:2012-10-06 07:41:39
【问题描述】:

我将 cucumber-watir-webdriver 用于自动化目的。我有以下目录结构:

|features
 -|feature1
 --|1.feature
 --|step_definitions
 ---|1.rb
 -feature2
 --|2.feature
 --|step_definitions
 ---|2.rb

等等。我需要知道在1.rb2.rb 中减少冗余的最佳做法是什么。 feature1feature2 完全不同,所以我不能将两者合并到一个目录中。 还有一些部分特征线相同,但执行步骤不同,所以如果它们在一起会产生歧义。

我需要知道1.rb2.rb 中是否有一些共同的部分我应该把它放在哪里是保持共同步骤定义的最佳做法。

【问题讨论】:

  • 你需要什么??就像“检查数据库条目”在这两个功能中一样,但他们检查的数据库是不同的,我不想混合它们并使事情复杂化。但是“鉴于我已登录”之类的步骤是相同的​​,因此我想将它们的步骤定义组合在一起。

标签: ruby cucumber automated-tests watir watir-webdriver


【解决方案1】:

可以将您的功能分开到单独的目录中,但最好将您的*_step.rb 文件放在step_definitions 目录下的features 目录下的一个step_definitions 目录中。您可以将这两个功能共有的步骤放入common_steps.rb 文件中(更好的是login_steps.rbtest_data_creation_steps.rb,而不是common_steps.rb)。

要将其转换为与您的问题相同的样式目录结构图,我建议如下:

|features
 -|feature1
 --|1.feature
 -feature2
 --|2.feature
 -|step_definitions
 --|1.rb
 --|2.rb
 --|common_steps.rb <-- put your common steps in here

【讨论】:

  • 谢谢,但我有一个疑问,如果我想要运行“cucumber /features/feature1/1.feature”,那么这些步骤定义是否会被加载运行特定功能,如“黄瓜 /features/feature1/feature1.1/1.1.feature”,然后将黄瓜自动加载 /feature/step_definition
  • 不正确,它们不会被加载。你必须告诉 Cucumber 去哪里看:cucumber -r features features/feature1/1.feature。默认情况下,在指定功能及其子目录的包含目录中查找步骤定义和支持代码。
  • @jbpros 我是这么认为的,但如果我正在运行 1.feature 我不想要的,你建议的方式也会加载 2.rb
  • @UdaySwami 在-r 路径中更具体,然后:) cucumber -r features/step_definitions/1.rb features/feature1
【解决方案2】:

Cucumber 仅在当前或后续目录中搜索步骤定义。 所以我们不能在两个特征目录下有共同的步骤定义目录或文件, 我找到了一个解决方案

|features
 -|feature1
 --|1.feature
 --|step_definitions
 ---1.rb
 -feature2
 --|2.feature
 --|step_definitions
 ---2.rb
 -|common_steps.rb <-- keep common steps in here

现在加载这个步骤定义添加

 require "#{File.dirname(__FILE__)}/../../common_steps.rb"

在您的 1.rbs 中。 你也可以有更长的目录结构,然后你可以在每个这样的目录中保留 common_steps.rb 文件,其中包含以下功能的常用步骤,你可以要求以前的 common_steps.rb 文件与--

 require "#{File.dirname(__FILE__)}/../common_steps.rb"

这段代码。 这将使您的目录结构和 step_definition 文件保持干净

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多