0

I'm trying to load Examples data from an external file using pytest-bdd. My approach was to simply hook into the pytest_bdd_before_scenario event and rewrite the Examples data in the scenario object. Here's my code attempt to do that:

def pytest_bdd_before_scenario(request, feature, scenario):
    if ("data-driven" not in scenario.tags):
        return
    
    dataFile = ''

    print('\n\nscenario:' + str(vars(scenario)))

    for tag in scenario.tags:
        #print (str(tag))
        if (tag.startswith("datafile_")):
            dataFile = tag.split('_')[1]

    if (dataFile == ''):
        return

    dir, null = os.path.split(str(request.path))
    fileName = dir + "\\resources\\" + dataFile

    df = pd.read_excel(io=fileName, sheet_name="data")

    examples = scenario.feature.scenarios[scenario.name].examples.examples

    # clear existing examples
    examples = []
    for index, row in df.iterrows():
        examples.append({row[0], row[1]})

    scenario.feature.scenarios[scenario.name].examples.examples = examples

This produces what looks like the correct Scenario object, but it still executes only the Examples values that were in the file. It looks like the updated object is being discarded.

How can I make this work? Is there a simpler approach?

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71

0 Answers0