0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · درس

صياغة Gherkin وميزاتها

تعلّموا تعريف الميزات والسيناريوهات وخطوات Given-When-Then باستخدام صياغة Gherkin سهلة القراءة

صياغة Gherkin وميزاتها درس مجاني في Testing Mastery: JUnit, Mockito & Integration Tests على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Testing Mastery: JUnit, Mockito & Integration Tests، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Testing Mastery: JUnit, Mockito & Integration Tests 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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.

الأسئلة الشائعة

هل درس «صياغة Gherkin وميزاتها» مجاني؟

نعم — نص درس «صياغة Gherkin وميزاتها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Testing Mastery: JUnit, Mockito & Integration Tests، انتقل إلى CoddyKit PRO. تتضمن دورة Testing Mastery: JUnit, Mockito & Integration Tests 4 دروس في المجموع.

ماذا ستتعلم في «صياغة Gherkin وميزاتها»؟

تعلّموا تعريف الميزات والسيناريوهات وخطوات Given-When-Then باستخدام صياغة Gherkin سهلة القراءة تتمرن على Testing Mastery: JUnit, Mockito & Integration Tests مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Testing Mastery: JUnit, Mockito & Integration Tests؟

لا تُشترط خبرة سابقة. Testing Mastery: JUnit, Mockito & Integration Tests على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «صياغة Gherkin وميزاتها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Testing Mastery: JUnit, Mockito & Integration Tests هذا؟

نعم. كل درس في Testing Mastery: JUnit, Mockito & Integration Tests يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى BDD
  2. صياغة Gherkin وميزاتها
  3. تنفيذ تعريفات الخطوات
  4. مخططات السيناريو وجداول البيانات
← العودة إلى Testing Mastery: JUnit, Mockito & Integration Tests