针对相同方案的多个文件的步骤定义

时间:2014-01-28 16:54:54

标签: ruby cucumber

我有一个场景,其步骤定义位于多个文件中。例如,登录步骤位于login_steps.rb中,搜索相关步骤位于search_steps.rb

Cucumber为login_steps.rb中没有的任何步骤输出未定义的步骤。步骤定义仅在其出现在login_steps.rb中时运行。是否需要将场景的所有步骤定义放在同一个文件中?

我的文件夹结构

Project folder
└─ features
   ├─ pages
   ├─ scenarios 
   ├─ step_definitions
   └─ support

我使用的命令:

cucumber -r features features\scenarios\Test.feature

3 个答案:

答案 0 :(得分:1)

Cucumber和POM的重点在于您具有灵活性,无需为每个功能文件重新编写步骤。这就是我的目录结构:

Root
 - features
   - step_definitions
     - step_definition.rb
   - support
      - env.rb
 - lib
  - BasePage.rb
 - feature.feature

基本上,使用这种目录结构,您的步骤定义在哪里,因为您需要参考的特定页面(例如,您的BasePage.rb文件)无关紧要

require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'pages', 'BasePage')


And /^I do something$/ do
  @page = BasePage.new(@test_env)
  @page.verify_el(css)
end

答案 1 :(得分:0)

我不熟悉RoR和黄瓜的细节,但我确实使用了黄瓜-jvm。支持使用不同文件中的步骤。请注意文档https://github.com/cucumber/cucumber/wiki/Cucumber-Backgrounder#where-do-i-put-tests特别提到它。

很抱歉,我对这个具体问题无法提供更多帮助,但您尝试做的事情(使用不同文件中的步骤)是可行的。

答案 2 :(得分:0)

这可能是一个“违规”,但我会将Whitney Imura和Dave W的答案结合起来,以便更清楚地回答......

你问:

  

“是否需要放置所有步骤   同一文件中场景的定义?“

否。您可以根据需要将步骤定义放在各种文件夹中的逻辑上不同的文件中(例如下面的示例)。毕竟,这只是红宝石代码。

基本上您的命令对于运行在各种其他文件夹中具有步骤定义的单个功能是正确的...

cucumber -r features features\entities\entity.feature

如果你不像上面那样运行它,你会得到缺少的stepdef ...在这里我对当前项目执行测试作为演示的方法:

cucumber
60 scenarios (14 undefined, 46 passed)
409 steps (32 skipped, 26 undefined, 351 passed)

cucumber -r features
60 scenarios (60 passed)
409 steps (409 passed)

Cucumber documentation所述,您可以安排测试以适应功能的逻辑细分:

|__ features
|   |__ entities
|   |   |__ entity.feature
|   |   |__ step_definitions
|   |       |__ anything.rb
|   |       |__ entity_steps.rb
|   |__ locations
|   |   |__ location.feature
|   |   |__ step_definitions
|   |       |__location_steps.rb
|   |__ sites
|   |   |__ step_definitions
|   |__ step_definitions
|   |   |__ local_assert_steps.rb
|   |   |__ local_crud_response_steps.rb
|   |   |__ local_email_steps.rb
|   |   |__ local_file_steps.rb
|   |   |__ local_script_steps.rb
|   |   |__ local_steps.rb
|   |   |__ local_web_steps.rb
`   |   |__ local_xml_file_steps.rb   
    |__ support
        |__ env.rb
        |__ local_env.rb
        |__ local_transforms.rb