Gherkin Official Parser for Python

39.0.0 · active · verified Sat Apr 11

Gherkin parser (official, by Cucumber team). It is a parser and compiler for the Gherkin language, which is a business-readable, domain-specific language used in Behavior-Driven Development (BDD) to describe software's behavior. It is developed and maintained by the Cucumber team and is designed to produce easily consumable Abstract Syntax Tree (AST) and Pickle objects. As of version 39.0.0, it officially supports Python versions 3.9 through 3.13. Releases are frequent, aligning with the broader Cucumber ecosystem development.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse Gherkin feature text into a Gherkin Document Abstract Syntax Tree (AST) and then compile it into 'Pickle' objects, which are simplified representations suitable for execution. It also shows basic access to the parsed data.

from gherkin import Parser, Compiler

feature_text = """
Feature: Calculator
  As a math enthusiast
  I want to add numbers
  So I can avoid mental arithmetic

  Scenario: Add two numbers
    Given I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When I press add
    Then the result should be 120
"""

parser = Parser()
gherkin_document = parser.parse(feature_text)
gherkin_document["uri"] = "test.feature" # Add a URI for the document

compiler = Compiler()
pickles = compiler.compile(gherkin_document)

print(f"Parsed Gherkin Document: {gherkin_document}")
print(f"Compiled Pickles: {pickles}")

# Example of accessing data
if pickles:
    first_pickle = pickles[0]
    print(f"\nFirst Pickle Name: {first_pickle['name']}")
    print(f"First Pickle Steps: {[step['text'] for step in first_pickle['steps']]}")

view raw JSON →