24

初期アクションに基づいて Web アプリの UI の複数の変更をテストしたい Gherkin で受け入れテストを作成しています。次に例を示します。

        Scenario: Cancel editing a new text asset
            Given the user "test_user@fakedomain.com" is logged in
            When the user navigates to "/build/"
            And the user clicks the "Sandbox" link
            And the user inputs "Test story for canceling editing of a new text asset" for the "title" field
            And the user inputs "Test User" for the "byline" field 
            And the user inputs "My summary, so exciting!" for the "summary" textarea
            And the user clicks on "Untitled Section" in the section list
            And the user clicks the "Text" icon in the "center" container 
            And the user inputs the following text in the rich text editor:
                    """
                    Test text for asset. This is cool. 
                    """
            And the user clicks the "cancel" button
            Then the following text is not present: 
                    """
                    Test text for asset. This is cool. 
                    """
            And the "Image" icon is present
            And the "Text" icon is present
            When the user refreshes the browser 
            And the user clicks on "Untitled Section" in the section list
            Then the following text is not present:
                    """
                    Test text for asset. This is cool. 
                    """
            When the user opens the asset drawer
            Then the following text is not present:
                    """
                    Test text for asset. This is cool.
                    """

最初のアクションの応答をテストするために、When/Then ステップの複数のグループがあることに注意してください。ステップのほとんどの実装は prefix キーワードを無視しますが、このテストを実行できると確信していますが、さまざまな結果をテストするより良い方法はありますか? 同じセットアップで異なる "Then" ステートメントを使用して複数のシナリオを作成することをお勧めしますか?

4

2 に答える 2

38

一度に 1 つの動作/機能のみをテストする必要があることに注意してください。経験則として、When ステップは 1 つだけ使用する必要があります。

Given some state before action
  And some other state before action
  ...
When  only one action
Then  assert correct output
  And assert correct output
  ...

ご覧のとおり、When は 1 行だけで、When の下に And はありません。代わりに多くの When ステップを使用する場合は、仕様ではなくテスト スクリプトを作成します。テストは理解しにくく、基礎となる実装が変更されると、ますます多くのステップを追加することに気付くでしょう。

また、無関係なものを変更するたびにロジックを変更したくないため、基礎となるロジックを隠しておく必要があります。例:

そして、ユーザーは「私のまとめ、とてもエキサイティングです!」と入力します。「概要」テキストエリア用

集計フィールドをテキストエリアから入力タイプに変更するとどうなるでしょうか? シナリオを変更するか (メンテナンスの悪夢)、シナリオを嘘のままにしておく必要があります (シナリオがないよりも悪いことです)。代わりに次のように書く必要があります。

When the user describes it as "so exciting!"

それにしても、シナリオ全体の構成が悪い。自問自答してください: 何をチェックしたいですか? 私が機能のビジネス ロジックを理解したい人であれば、次のようなものを見たいと思います。

Scenario: Cancel editing a new text asset
  Given I edit the Sandbox details with some data
  When  I cancel editing
  Then  Sandox details should be empty

それでおしまい!

それを達成する方法は?すべての無関係なロジックをより深く移動し、PageObject パターンなどを使用します。また、 Specification By Exampleについて読んでください。

于 2013-10-16T10:35:04.253 に答える