ここでは、いくつかのアンチパターンについて説明します。また、それらを回避する方法のヒントも提供します。

機能結合ステップ定義

機能結合ステップ定義は、機能やシナリオ間で再利用できないステップ定義です。

これにより、ステップ定義の爆発的な増加、コードの重複、および高い保守コストにつながる可能性があります。

架空の履歴書アプリケーションには、次のフィーチャーファイルとステップ定義ファイルが存在する可能性があります

features/
+--edit_work_experience.feature
+--edit_languages.feature
+--edit_education.feature
+--steps/
   +--edit_work_experience_steps.java
   +--edit_languages_steps.java
   +--edit_education_steps.java
features/
+--edit_work_experience.feature
+--edit_languages.feature
+--edit_education.feature
+--steps/
   +--edit_work_experience_steps.kt
   +--edit_languages_steps.kt
   +--edit_education_steps.kt
features/
+--edit_work_experience.feature
+--edit_languages.feature
+--edit_education.feature
+--steps/
   +--edit_work_experience_steps.js
   +--edit_languages_steps.js
   +--edit_education_steps.js
features/
+--edit_work_experience.feature
+--edit_languages.feature
+--edit_education.feature
+--steps/
   +--edit_work_experience_steps.rb
   +--edit_languages_steps.rb
   +--edit_education_steps.rb

edit_work_experience.featureには、次のシナリオが存在する可能性があります

Scenario: add description
  Given I have a CV and I'm on the edit description page
  And I fill in "Description" with "Cucumber BDD tool"
  When I press "Save"
  Then I should see "Cucumber BDD tool" under "Descriptions"

は、次のように実装できます

    @Given("I have a CV and I'm on the edit description page")
    public void I_have_a_CV_and_Im_on_the_edit_description_page() {
        Employee employee = new Employee("Sally");
        employee.createCV();
    }
@Given("I have a CV and I'm on the edit description page")
fun I_have_a_CV_and_Im_on_the_edit_description_page() {
    val employee = Employee("Sally")
    employee.createCV()
}
var { Given } = require('cucumber');

Given(/^I have a CV and I'm on the edit description page$/, function () {
  this.employee = new Employee('Sally');
  this.employee.createCV();
});
Given /I have a CV and I'm on the edit description page/ do
  @employee = Employee.create!(name: 'Sally')
  @employee.create_cv
  visits("/employees/#{@employee.id}/descriptions/new")
end

ステップとステップ定義を分離する方法

  • ステップをドメイン概念ごとに整理します。

  • ステップファイルとステップ定義ファイルには、機能またはシナリオ関連の名前ではなく、ドメイン関連の名前を使用します。

接続詞ステップ

オンラインの Merriam-Webster 辞書より

con·junc·tion: 文、節、句、または語を結合する非屈折言語形式。

複数の異なるものを組み合わせたステップを使用しないでください。これにより、ステップが過度に特殊化され、再利用が困難になります。Cucumber には、接続詞(AndBut)が組み込まれている理由があります!

Given I have shades and a brand new Mustang

接続詞ステップを分割する方法

Given I have shades
And I have a brand new Mustang

接続詞ステップのサポート

シナリオを読みやすくするために、複数のステップを1つに結合したい場合があります。たとえば、Givenステートで複数の前提条件を設定する必要がある場合です。

構成と再利用を実現する最良の方法は、プログラミング言語の機能を使用することです。複数のアクションを1つのステップに結合する場合は、個別の(ヘルパー)メソッドを抽出し、ステップ定義からこれらのメソッドを呼び出します。

ステップは、できる限りアトミックに保つように努める必要があります。

詳細情報

アンチパターンの詳細については、Cucumber アンチパターン (ブログ)を参照してください。

このドキュメントの改善にご協力ください。このページを編集してください。