Behave: Writing a Scenario Outline with dynamic examples

Adam Matan picture Adam Matan · Jan 10, 2017 · Viewed 8.7k times · Source

Gherkin / Behave Examples

Gherkin syntax features test automation using examples:

Feature: Scenario Outline (tutorial04)

  Scenario Outline: Use Blender with <thing>
    Given I put "<thing>" in a blender
    When I switch the blender on
    Then it should transform into "<other thing>"

    Examples: Amphibians
        | thing         | other thing |
        | Red Tree Frog | mush        |
        | apples        | apple juice |

    Examples: Consumer Electronics
        | thing         | other thing |
        | iPhone        | toxic waste |
        | Galaxy Nexus  | toxic waste |

The test suite would run four times, once for each example, giving a result similar to:

enter image description here

My problem

How can I test using confidential data in the Examples section? For example, I would like to test an internal API with user ids or SSN numbers, without keeping the data hard coded in the feature file.

Is there a way to load the Examples dynamically from an external source?

Update: Opened a github issue on the behave project.

Answer

ekostadinov picture ekostadinov · Apr 10, 2017

Got here looking for something else, but since I've been in similar situation with Cucumber before, maybe someone will also end up at this question, looking for a possible solution. My approach to this problem is to use BDD variables that I can later handle at runtime in my step_definitions. In my python code I can check what is the value of the Gherkin variable and map it to what's needed.

For this example:

Scenario Outline: Use Blender with <thing>
     Given I put "<thing>" in a blender
     When I switch the blender on
     Then it should transform into "<other thing>"

    Examples: Amphibians
        | thing         | other thing            |
        | Red Tree Frog | mush                   |
        | iPhone        | data.iPhone.secret_key | # can use .yaml syntax here as well

Would translate to such step_def code:

@given('I put "{thing}" in a blender')
def step_then_should_transform_into(context, other_thing):   
  if other_thing == BddVariablesEnum.SECRET_KEY: 
    basic_actions.load_secrets(context, key)

So all you have to do is to have well defined DSL layer.