问题
I'm using cucumber-watir-webdriver with for automation purposes. I have the following directory structure:
|features
-|feature1
--|1.feature
--|step_definitions
---|1.rb
-feature2
--|2.feature
--|step_definitions
---|2.rb
and so on. I need to know what is best practice to reduce redundancy in 1.rb
and 2.rb
.
feature1
and feature2
are completely different so I cannot combine both in one directory.
Also there is some part where feature line is same but execution in steps is different so it will create ambiguity if they are together.
I need to know if there is some part common in 1.rb
and 2.rb
where should I put it is there best practice to keep common step definitions.
回答1:
It's fine to separate out your features into individual directories, but it's best to keep your *_step.rb
files together in one step_definitions
directory under the features
directory. You can put steps common to both features into a common_steps.rb
file (even better would be something like login_steps.rb
and test_data_creation_steps.rb
instead of common_steps.rb
).
To translate that into the same style directory structure diagram as in your question, I'd suggest the following:
|features
-|feature1
--|1.feature
-feature2
--|2.feature
-|step_definitions
--|1.rb
--|2.rb
--|common_steps.rb <-- put your common steps in here
回答2:
Cucumber only searches for step definitions in current or following directories. So we can not have common step definitions directory or file below two feature directories, I found one solution to this as
|features
-|feature1
--|1.feature
--|step_definitions
---1.rb
-feature2
--|2.feature
--|step_definitions
---2.rb
-|common_steps.rb <-- keep common steps in here
now to load this step definition add
require "#{File.dirname(__FILE__)}/../../common_steps.rb"
in your 1.rbs. also you can have longer directory structure then you can keep common_steps.rb file in every such directory containing common steps of following features and you can require previous common_steps.rb file with--
require "#{File.dirname(__FILE__)}/../common_steps.rb"
this code. This will keep your directory structure and step_definition files clean
来源:https://stackoverflow.com/questions/12910763/best-practice-to-keep-common-steps-of-cucumber