MCPcopy Create free account
hub / github.com/pytest-dev/pytest-bdd / FeatureParser

Class FeatureParser

src/pytest_bdd/parser.py:366–543  ·  view source on GitHub ↗

Converts a feature file into a Feature object. Args: basedir (str): The basedir for locating feature files. filename (str): The filename of the feature file. encoding (str): File encoding of the feature file to parse.

Source from the content-addressed store, hash-verified

364
365
366class FeatureParser:
367 """Converts a feature file into a Feature object.
368
369 Args:
370 basedir (str): The basedir for locating feature files.
371 filename (str): The filename of the feature file.
372 encoding (str): File encoding of the feature file to parse.
373 """
374
375 def __init__(self, basedir: str, filename: str, encoding: str = "utf-8"):
376 self.abs_filename = os.path.abspath(os.path.join(basedir, filename))
377 self.rel_filename = os.path.join(os.path.basename(basedir), filename)
378 self.encoding = encoding
379
380 def parse_steps(self, steps_data: list[GherkinStep]) -> list[Step]:
381 """Parse a list of step data into Step objects.
382
383 Args:
384 steps_data (List[dict]): The list of step data.
385
386 Returns:
387 List[Step]: A list of Step objects.
388 """
389
390 if not steps_data:
391 return []
392
393 first_step = steps_data[0]
394 if first_step.keyword_type not in STEP_TYPE_BY_PARSER_KEYWORD:
395 raise StepError(
396 message=f"First step in a scenario or background must start with 'Given', 'When' or 'Then', but got {first_step.keyword}.",
397 line=first_step.location.line,
398 line_content=first_step.text,
399 filename=self.abs_filename,
400 )
401
402 steps = []
403 current_type = STEP_TYPE_BY_PARSER_KEYWORD[first_step.keyword_type]
404 for step in steps_data:
405 current_type = STEP_TYPE_BY_PARSER_KEYWORD.get(step.keyword_type, current_type)
406 steps.append(
407 Step(
408 name=step.text,
409 type=current_type,
410 indent=step.location.column - 1,
411 line_number=step.location.line,
412 keyword=step.keyword.title(),
413 datatable=step.datatable,
414 docstring=step.docstring.content if step.docstring else None,
415 )
416 )
417 return steps
418
419 def parse_scenario(
420 self, scenario_data: GherkinScenario, feature: Feature, rule: Rule | None = None
421 ) -> ScenarioTemplate:
422 """Parse a scenario data dictionary into a ScenarioTemplate object.
423

Callers 1

get_featureFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…