0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · Lezione

Sintassi e funzionalità di Gherkin

Impari a definire funzionalità, scenari e passaggi Given-When-Then usando la sintassi Gherkin, leggibile anche da chi non è tecnico.

Sintassi e funzionalità di Gherkin è una lezione Testing Mastery: JUnit, Mockito & Integration Tests gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Testing Mastery: JUnit, Mockito & Integration Tests, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Gherkin: Plain Language Tests

Gherkin is a plain-language parser used by Behavior-Driven Development (BDD) tools like Cucumber. It helps describe software behavior in a human-readable format.

This makes it easier for everyone—developers, testers, and business stakeholders—to understand and agree on what the software should do. Gherkin uses a specific set of keywords to structure these descriptions.

Defining a Feature

The Feature keyword is the starting point of any Gherkin document. It describes a high-level capability or a specific area of the application.

  • Think of it as the main goal or business requirement you are testing.
  • It should have a clear, concise title.
  • An optional description can follow, providing more context.
  • Each .feature file typically contains one Feature.

Feature Example in Action

Here’s how you define a Feature. Notice the optional description lines that add clarity, explaining the 'who', 'what', and 'why'.

Feature: User Login Functionality
  As a registered user
  I want to log in to my account
  So that I can access personalized content.

Describing Scenarios

Within a Feature, individual test cases are called Scenarios. A Scenario describes a single, concrete example of behavior.

For repetitive tests with different data, Scenario Outline is used. It runs the same steps multiple times, substituting values from an Examples table (which we'll cover in future lessons).

A Simple Scenario

A Scenario typically follows the "Given-When-Then" structure. It's a specific example of how the feature behaves under certain conditions.

Feature: User Login Functionality

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters "john@example.com" and "password123"
    And clicks the "Login" button
    Then the user should be redirected to the dashboard
    And a welcome message "Welcome, John!" should be displayed

Setting the Stage with `Given`

The Given keyword is used to describe the initial state or context of the system. It sets up the prerequisites for the scenario.

  • Think of it as the "precondition" for your test.
  • It should describe something that has already happened.
  • Example: Given the user is logged in or Given a product exists with price $10.

The Action with `When`

The When keyword describes the action or event that triggers the behavior under test. This is typically an interaction by the user or an external system.

  • It's the "trigger" for the behavior.
  • It should describe a single, observable action.
  • Example: When the user clicks the "Add to Cart" button or When the payment is processed.

Verifying Outcome with `Then`

The Then keyword describes the expected outcome or result after the When action. It's how you verify the system's behavior.

  • This is the "assertion" part of your test.
  • It should describe an observable change in the system.
  • Example: Then the item should be added to the cart or Then the order status should be "Completed".

Full Gherkin Scenario

Here's a complete Scenario showing Given, When, and Then working together. Notice how And can be used to extend any step for better readability.

Feature: Shopping Cart Management

  Scenario: Add item to empty cart
    Given the user is on the product page for "Laptop X"
    And the shopping cart is empty
    When the user clicks "Add to Cart"
    Then the shopping cart should contain "Laptop X"
    And the cart total should be $1200.00

Ordering Gherkin Steps

In Gherkin, steps follow a logical flow to describe a behavior. Arrange the keywords in the correct order for a typical scenario.

Gherkin Syntax Summary

You've learned the core elements of Gherkin syntax!

  • Feature defines a high-level capability.
  • Scenario describes a single example of behavior.
  • Given sets the initial context.
  • When describes the action or event.
  • Then verifies the expected outcome.
  • And and But are used to extend steps for readability.

This human-readable language bridges the gap between technical and non-technical team members, ensuring everyone understands the desired system behavior.

Domande Frequenti

La lezione «Sintassi e funzionalità di Gherkin» è gratuita?

Sì — il testo completo di «Sintassi e funzionalità di Gherkin» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Testing Mastery: JUnit, Mockito & Integration Tests, passa a CoddyKit PRO. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Cosa imparerò in «Sintassi e funzionalità di Gherkin»?

Impari a definire funzionalità, scenari e passaggi Given-When-Then usando la sintassi Gherkin, leggibile anche da chi non è tecnico. Eserciti Testing Mastery: JUnit, Mockito & Integration Tests con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Testing Mastery: JUnit, Mockito & Integration Tests?

Non è richiesta alcuna esperienza precedente. Testing Mastery: JUnit, Mockito & Integration Tests su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Sintassi e funzionalità di Gherkin»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Testing Mastery: JUnit, Mockito & Integration Tests?

Sì. Ogni lezione Testing Mastery: JUnit, Mockito & Integration Tests include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione al BDD
  2. Sintassi e funzionalità di Gherkin
  3. Implementazione delle definizioni dei passaggi
  4. Scenario Outline e tabelle di dati
← Torna a Testing Mastery: JUnit, Mockito & Integration Tests