私は Gherkin で受け入れテストを書いています。ここでは、最初のアクションに基づいて Web アプリの UI の複数の変更をテストしたいと考えています。次に例を示します。
Scenario: Cancel editing a new text asset
Given the user "[email protected]" 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 ステップのグループが複数あることに注意してください。ステップの実装のほとんどはプレフィックス キーワードを無視し、このテストを実行できると確信していますが、さまざまな結果をテストするより良い方法はありますか? 同じ設定で異なる「Then」ステートメントを使用して複数のシナリオを記述する方がよい方法でしょうか?
ベストアンサー1
一度にテストする動作や機能は1つだけにしてください。原則として、たった1つのWhenステップ:
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 パターンなどについて読んでください例による仕様