0

I have a feature file as blow. How could I import .csv file to replace Table in Examples?

Scenario Outline: Blenders
   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        |

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

2 Answers2

0

Table importing is not supported yet, as described here.

But according to the jenisys response in the same post, it will be available soon.

Lucas Mello
  • 342
  • 3
  • 8
0

Table importing is not supported yet, but you can create helper method.

The x.feature file:

Feature: csv file

@dynamic
Scenario Outline: Load data from csv
  Then just print <username> <email> <password>
    Examples: Dynamic
      | username | email |  password |
      |    .     |   .   |      .    |

Step file (steps/step_file.py):

from behave import step


@step('just print {username} {email} {password}')
def step_impl(context, username, email, password):
    print(username, email, password)

The environment.py file:

from behave.model import ScenarioOutline
import copy
import csv


def before_feature(context, feature):
    features = (scenario for scenario in feature.scenarios if type(scenario) == ScenarioOutline and 'dynamic' in scenario.tags)
    for scenario in features:
        for example in scenario.examples: 
            orig = copy.deepcopy(example.table.rows[0])
            example.table.rows = []
            with open('test.json') as csv_file:
                csv_reader = csv.reader(csv_file, delimiter=',')
                for row in csv_reader:
                    n = copy.deepcopy(orig)
                    n.cells = ['{}'.format(row[0]), '{}'.format(row[1]), '{}'.format(row[2])]
                    example.table.rows.append(n)

the test.json file:

andrew,testemail1@example.com,1235aaa
mike,testemail1@example.com,1234bbb

The result is:

behave -i tutorial.feature
Feature: csv file # features/tutorial.feature:1

  @dynamic
  Scenario Outline: Load data from csv -- @1.1 Dynamic     # features/tutorial.feature:8
    Then just print andrew  testemail1@example.com 1235aaa # features/steps/step_test.py:4 0.000s

  @dynamic
  Scenario Outline: Load data from csv -- @1.2 Dynamic    # features/tutorial.feature:8
    Then just print marina testemail1@example.com 1234bbb # features/steps/step_test.py:4 0.000s

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
2 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s
andrew
  • 51
  • 7