简体   繁体   中英

Cucumber step with numerous parameters

Is there a way to group the parameters into a single one, eg pass a data structure that yields all of them?

eg I want to avoid having methods with too many arguments:

Scenario Outline: My scenario has too many parameters
    When I perform my request with these inputs: <param1>, <param2>, <param3>, <param4>, <param5>, <param6>, <param7>, <param8>, <param9>, <param10>, <param11>
 Examples:
  | param1 | param2 | param3 | param4 | param5 | param6 | param7 | param8 | param9 | param10 | param11 |
  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy   | dummy   |
  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy   | dummy   |

and then the method:

@Given("^When I perform my request with these inputs: (.+), (.+), (.+), (.+), (.+), (.+), (.+), (.+), (.+), (.+), (.+)$")
public void tooMany(String param1, String param2, String param3, String param4, String param5, String param6, String param7, String param8, String param9, String param10, String param11)  {
...

Are there better approaches to transfer that many inputs?

Thank you

Using a data table as part of your step can help organize this information:

Scenario Outline: My scenario has too many parameters
  When I perform my request with the following inputs:
    | Field   | Value     |
    | param1  | <param1>  |
    | param2  | <param2>  |
    | param3  | <param3>  |
    | param4  | <param4>  |
    | param5  | <param5>  |
    | param6  | <param6>  |
    | param7  | <param7>  |
    | param8  | <param8>  |
    | param9  | <param9>  |
    | param10 | <param10> |
    | param11 | <param11> |

Examples:
  | param1 | param2 | param3 | param4 | param5 | param6 | param7 | param8 | param9 | param10 | param11 |
  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy   | dummy   |
  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy  | dummy   | dummy   |

And your step definition becomes:

@When("When I perform my request with the following inputs:")
public void notTooManyAnymore(DataTable table) {
    // Use table to get the params
}

Now you can extract the params from the table (see Data Tables in Cucumber and Cucumber Data Tables ). You should be able to map the table to a POJO for some compile time safety.

A much better approach is to take all these examples out of the feature and push them down into helper methods called by the step definitions.

Each set of examples in an example table should mean something (otherwise why are they there in the first place!). You can use this mean to determine a name which captures the particular combination of parameters. You can then use that name in your scenario to replace the params.

Hopefully you can cope with me using foo, bar and request as names here, your example does not have anything significant enough to extract better names.

Following this we would have

Scenario: Make a foo request
  When I perform a foo request

and a ruby implementation of this (you'll have to translate to java yourself) would be

module RequestStepHelper
  def foo_example_params
    {
      param1: dummy,
      param2: dummy,
      ...
    }
  end

  def perform_request(params : {})
    # process hash to make correct call
    ...
  end
end
World RequestStepHelper

When 'I perform a foo request' do
  perform_request(foo_example_params)
end

So now have removed all that junk from your feature file which can now focus on describing WHAT is going on and WHY its important. In addition all the processing of params is now being done in code where you have much more power available and can do things like take an existing set of params and modify them eg foo_example_params.except(param1) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM