サーチ…


前書き

Gherkinは、テストの自動化とテストのためのビジネスで読める言語です。これはキュウリによって理解され、一緒に行動主導の開発ツールとして存在します。

構文

  • 機能:このキーワードは、テストまたは文書化されている機能の基本的な説明または名前です。
  • 背景:このキーワードは、機能内のすべてのシナリオの前に実行されるステップを示します。
  • シナリオ:このキーワードは、機能をテストする特定のシナリオの名前または基本的な説明を表します。
  • シナリオ概要:このキーワードは、角括弧で囲まれた列名で明示的に渡された例にリストされているすべての引数についてシナリオがN回実行されることを示します。
  • 例:このキーワードは、シナリオの概要に渡される静的引数のリストを示します。
  • 与えられた:このキーワードは、与えられたステップ、または継続する前に想定される前提条件を表します。アレンジ、アクト、アサートのパラダイムでは、与えられた "アレンジ"を表します。
  • When:このキーワードは、whenステップ、またはアサートされるべき動作を表します。 Arrange、Act、Assertのパラダイムでは、与えられた "Act"を表します。
  • 次に:このキーワードは、ステップ、つまり行動の結果が検証されるステップを表します。 Arrange、Act、Assertパラダイムでは、与えられた "Assert"を表します。
  • And:このキーワードは、上記のいずれかのキーワードと組み合わせて使用​​されます。 2つの指定されたステートメントがある場合、Givenを明示的に2回呼び出すのではなく、 "AとBを指定する"と言うことができます。

基礎

この例では、Gherkinのキュウリの機能ファイルの基本構造について説明します。機能ファイルは、基本的な構文でいくつかのキーワードを使用します。

基本的なキーワードを見てみましょう:

  • 機能:このキーワードは、テストまたは文書化されている機能の基本的な説明または名前です。
  • シナリオ:このキーワードは、機能をテストする特定のシナリオの名前または基本的な説明を表します。
  • このキーワードを考えると、与えられたステップ、または継続する前に想定している前提条件を表しています。アレンジ、アクト、アサートのパラダイムでは、与えられた "アレンジ"を表します。
  • このキーワードはときステップ、または対抗する行動を表す場合 。 Arrange、Act、Assertのパラダイムでは、与えられた "Act"を表します。
  • 次に、このキーワードは、ステップ、すなわち行動の結果が検証されるステップを表します。 Arrange、Act、Assertパラダイムでは、与えられた "Assert"を表します。
  • そして、このキーワードは、上記のいずれかのキーワードと組み合わせて使用されます。 2つの指定されたステートメントがある場合、Givenを明示的に2回呼び出すのではなく、 "AとBを指定する"と言うことができます。
  • しかし、このキーワードは、 GivenWhenThenと一緒に使用され、何かが起こらないことを示します。その後、AではなくB.

すべてのキーワードは、新しい行になければならず、ガーキンパーザによって認識されるためには、新しい行の最初の単語でなければなりません。フィーチャーとシナリオのキーワードは、以下の例に示すように、直後にコロンを付ける必要があります。 「When、Then、And」はコロンを必要としません。

キーワードに加えて、説明やコメントを書くことができます。説明はキーワードの後に​​ありますが、同じ行にあり、キーワードの下の行にコメントが表示されます。フィーチャーコメントを書くときは、動作の異なる結果につながるエッジと条件の概要を明示的に規定するのが通例です。

Feature: Product Login
    As a user, I would like to be able to use my credentials to successfully 
    login. 
    
    Rules:
    - The user must have a valid username
    - The user must have a valid password
    - The user must have an active subscription 
    - User is locked out after 3 invalid attempts
    - User will get a generic error message following 
      login attempt with invalid credentials 

    Scenario: The user successfully logs in with valid credentials 
        This scenario tests that a user is able to successfully login
        provided they enter a valid username, valid password, and 
        currently have an active subscription on their account. 

        Given the user is on the login page
        When the user signs in with valid credentials
        Then the user should be logged in

パラメータ化されたステップ

Gherkinを書くときには、テストプランを実装しているエンジニアが再利用のためのステップをパラメータ化することがあります。ステップは、正規表現キャプチャグループを介してパラメータを受け取ります。 ( エンジニアリングノート:正規表現のキャプチャグループごとに一致するパラメータがない場合、「CucumberException:Arity mismatch」がスローされることが予想されます)次の例では、引数を二重引用符で囲むことにしましたasは引数として整数を受け入れます。

 Feature: Product Login
    As a user, I would like to be able to use my credentials to successfully 
    login. 
    
    Rules:
    - The user must have a valid username
    - The user must have a valid password
    - The user must have an active subscription 
    - User is locked out after 3 invalid attempts
    - User will get a generic error message following 
      login attempt with invalid credentials 

    Scenario: The user successfully logs in with valid credentials 
        This scenario tests that a user is able to successfully login
        provided they enter a valid username, valid password, and 
        currently have an active subscription on their account. 

        Given the user is on the login page
        When the user signs in with "valid" credentials
        Then the user should be logged in

    Scenario: The user attempts to log in with invalid credentials 
        This scenario tests that a user is not able to log in when
        they enter invalid credentials

        Given the user is on the login page
        When the user signs in with "invalid" credentials
        Then the user should be logged in

    Scenario: The user is locked out after too many failed attempts
        This scenario validates that the user is locked out
        of their account after failing three consecutive 
        attempts to log in

        Given the user is on the login page
        When the user fails to log in 3 times
        Then the user should be locked out of their account

機能の背景

上記の例で気づいたように、同じ手順を何度も書き直しています:

Given the user is on the login page

例外的に退屈なことがあります。特に、再利用されるステップが複数ある場合は特にそうです。 Gherkinはこれを解決するために、 背景となる別のキーワードを提供します。

背景キーワードは、フィーチャー内のすべてのシナリオの前に、その下に宣言されたステップを実行するために使用されます。すべてのシナリオで必要であることが肯定的でない限り、バックグラウンドステップを追加しないでください。他のキーワードと同様に、背景には説明や名前が続き、その下にコメントが表示されます。フィーチャーとシナリオのように、バックグラウンドはコロンで処理する必要があります。

Feature: Product Login
    As a user, I would like to be able to use my credentials to successfully 
    login. 
    
    Rules:
    - The user must have a valid username
    - The user must have a valid password
    - The user must have an active subscription 
    - User is locked out after 3 invalid attempts
    - User will get a generic error message following 
      login attempt with invalid credentials 

    Background: The user starts out on the login page
        Given the user is on the login page

    Scenario: The user successfully logs in with valid credentials 
        This scenario tests that a user is able to successfully login
        provided they enter a valid username, valid password, and 
        currently have an active subscription on their account. 

        When the user signs in with "valid" credentials
        Then the user should be logged in

    Scenario: The user attempts to log in with invalid credentials 
        This scenario tests that a user is not able to log in when
        they enter invalid credentials

        When the user signs in with "invalid" credentials
        Then the user should be logged in

    Scenario: The user is locked out after too many failed attempts
        This scenario validates that the user is locked out
        of their account after failing three consecutive 
        attempts to log in

        When the user fails to log in 3 times
        Then the user should be locked out of their account

シナリオの概要

場合によっては、引数を代入して同じシナリオを何度も何度もやり直すことができます。この場合、Gherkinはこの状況に対応するためのいくつかの新しいキーワードを提供しています。 シナリオ概要:例: 。シナリオアウトライン(Scenario Outline)キーワードは、シナリオがリストから引き数を置き換えて複数回実行されることをCucumberに伝えます。リストが明示的に表記される前に、Examplesキーワードが呼び出されます。シナリオのアウトラインの引数は角カッコで囲む必要があります。以下の例では、角カッコで囲まれた引数名は、例に列挙されている列名に対応しています。各列は垂直バーで区切られ、最初の行に列名が表示されます。

Feature: Product Login
    As a user, I would like to be able to use my credentials to successfully 
    login. 
    
    Rules:
    - The user must have a valid username
    - The user must have a valid password
    - The user must have an active subscription 
    - User is locked out after 3 invalid attempts
    - User will get a generic error message following 
      login attempt with invalid credentials 

    Background: The user starts out on the login page
        Given the user is on the login page

    Scenario Outline: The user successfully logs in with their account
         This scenario outlines tests in which various users attempt
         to sign in successfully 

         When the user enters their <username>
         And the user enters their <password>
         Then the user should be successfully logged on

         Examples:
         | username | password |
         | frank    | 1234     |
         | jack     | 4321     |

タグ

文書化の目的で、テスト計画またはシナリオをカテゴリ別にフィルタリングすることができます。開発者は、同じカテゴリに基づいてテストを実行することができます。 Gherkinでは、タグを使用してフィーチャと個々のシナリオを分類できます。以下の例では、上記のFeatureキーワードがタグ "@Automation"であることに注意してください。 Gherkinはこれを "@"記号のユーザーがタグとして認識します。この例では、エンジニアはこれらのテストが自動化に使用されていることを明確にしたいと考えています。すべてのテストが自動化できるわけではなく、一部のテストは手動QAで行う必要があります。

同様に、@Productionというタグがシナリオテストのユーザーロックアウトに追加されていることにも注意してください。この例は、このシナリオがアプリケーションの運用環境でのみ有効なためです。開発者は開発中にサンドボックスアカウントをロックアウトしたくない。このタグを使用すると、このテストが本番環境に対してのみ実行されるよう強制することができます。

最後に、シナリオアウトラインには@Stagingタグが付いています。この例では、使用されているアカウントがステージングアカウントであり、他の環境では機能しないためです。 @Productionタグと同様に、これらのテストはステージング環境でのみ実行されます。

これらは、タグを使用する場所、方法、および理由のほんのいくつかの例です。最終的には、これらのタグはあなたと開発者にとって意味を持つことになります。

@Automation
Feature: Product Login
    As a user, I would like to be able to use my credentials to successfully 
    login. 
    
    Rules:
    - The user must have a valid username
    - The user must have a valid password
    - The user must have an active subscription 
    - User is locked out after 3 invalid attempts
    - User will get a generic error message following 
      login attempt with invalid credentials 

    Background: The user starts out on the login page
        Given the user is on the login page

    Scenario: The user successfully logs in with valid credentials 
        This scenario tests that a user is able to successfully login
        provided they enter a valid username, valid password, and 
        currently have an active subscription on their account. 

        When the user signs in with "valid" credentials
        Then the user should be logged in

    Scenario: The user attempts to log in with invalid credentials 
        This scenario tests that a user is not able to log in when
        they enter invalid credentials

        When the user signs in with "invalid" credentials
        Then the user should be logged in

    @Production
    Scenario: The user is locked out after too many failed attempts
        This scenario validates that the user is locked out
        of their account after failing three consecutive 
        attempts to log in

        When the fails to log in 3 times
        Then the user should be locked out of their account

    @Staging
    Scenario Outline: The user successfully logs in with their account
         This scenario outlines tests in which various users attempt
         to sign in successfully 

         When the user enters their <username>
         And the user enters their <password>
         Then the user should be successfully logged on

         Examples:
         | username | password |
         | frank    | 1234     |
         | jack     | 4321     |

ガーキンのヒント

  • 各シナリオは1つの動作をテストします
  • シナリオは宣言的な方法で記述されます
  • シナリオ内での偶発的な詳細の回避
  • 明らかにしない
  • 結合的なステップを避ける
  • シナリオを短くしておく
  • 同じ機能で多くのシナリオを使用する必要はありません
  • 説明的なシナリオ名を使用する
  • 1つしかないステップ
  • その後、「ステップ」で「必要」を使用します。


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow