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

Sintaxe e funcionalidades do Gherkin

Aprenda a definir funcionalidades, cenários e etapas Dado-Quando-Então usando a sintaxe legível do Gherkin.

Sintaxe e funcionalidades do Gherkin é uma aula grátis de Testing Mastery: JUnit, Mockito & Integration Tests no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Testing Mastery: JUnit, Mockito & Integration Tests, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Testing Mastery: JUnit, Mockito & Integration Tests inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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.

Perguntas Frequentes

A aula “Sintaxe e funcionalidades do Gherkin” é grátis?

Sim — o texto completo de “Sintaxe e funcionalidades do Gherkin” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Testing Mastery: JUnit, Mockito & Integration Tests, atualize para CoddyKit PRO. O curso de Testing Mastery: JUnit, Mockito & Integration Tests inclui 4 aulas no total.

O que vou aprender em “Sintaxe e funcionalidades do Gherkin”?

Aprenda a definir funcionalidades, cenários e etapas Dado-Quando-Então usando a sintaxe legível do Gherkin. Você pratica Testing Mastery: JUnit, Mockito & Integration Tests com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Testing Mastery: JUnit, Mockito & Integration Tests?

Nenhuma experiência prévia é necessária. Testing Mastery: JUnit, Mockito & Integration Tests no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Sintaxe e funcionalidades do Gherkin”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Testing Mastery: JUnit, Mockito & Integration Tests?

Sim. Cada aula de Testing Mastery: JUnit, Mockito & Integration Tests inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Introdução ao BDD
  2. Sintaxe e funcionalidades do Gherkin
  3. Implementação de definições de etapas
  4. Esboços de cenários e tabelas de dados
← Voltar para Testing Mastery: JUnit, Mockito & Integration Tests