1. 序章

Cucumberは、BDD(Behavioral Driven Development)テストフレームワークです。

フレームワークを使用して、入力/出力の順列が異なる反復シナリオを作成することは、非常に時間がかかり、保守が難しく、もちろんイライラする可能性があります。

Cucumberには、シナリオの概要と例の概念を組み合わせて使用することで、この労力を軽減するソリューションが付属しています。 以下のセクションでは、例を取り上げて、この労力を最小限に抑える方法を見ていきます。

アプローチとガーキン言語についてもっと知りたい場合は、この記事をご覧ください。

2. きゅうりサポートの追加

単純なMavenプロジェクトでCucumberのサポートを追加するには、次の依存関係を追加する必要があります。

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

Maven Centralからの依存関係への便利なリンク: cucumber-junit cucumber-java hamcrest-library

これらはテストライブラリであるため、実際にデプロイ可能なものと一緒に出荷する必要はありません。そのため、すべてtestスコープになっています。

3. 簡単な例

肥大化した方法と、注目のファイルを書くための簡潔な方法の両方を示しましょう。 まず、テストを作成するロジックを定義しましょう。

まず、テストを作成するロジックを定義しましょう。

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

4. キュウリテストの定義

4.1. 機能ファイルの定義

Feature: Calculator
  As a user
  I want to use a calculator to add numbers
  So that I don't need to add myself

  Scenario: Add two numbers -2 & 3
    Given I have a calculator
    When I add -2 and 3
    Then the result should be 1
   
  Scenario: Add two numbers 10 & 15
    Given I have a calculator
    When I add 10 and 15
    Then the result should be 25

ここに見られるように、ここで加算ロジックをテストするために、2つの異なる数値の組み合わせが配置されています。 数字を除けば、すべてのシナリオはまったく同じです。

4.2. 「グルー」コード

これらのシナリオをテストするには、ステートメントを機能的なコードに変換するために、対応するコードを使用して各ステップを定義することが不可欠です。

public class CalculatorRunSteps {

    private int total;

    private Calculator calculator;

    @Before
    private void init() {
        total = -999;
    }

    @Given("^I have a calculator$")
    public void initializeCalculator() throws Throwable {
        calculator = new Calculator();
    }

    @When("^I add (-?\\d+) and (-?\\d+)$")
    public void testAdd(int num1, int num2) throws Throwable {
        total = calculator.add(num1, num2);
    }

    @Then("^the result should be (-?\\d+)$")
    public void validateResult(int result) throws Throwable {
        Assert.assertThat(total, Matchers.equalTo(result));
    }
}

4.3. ランナークラス

機能とグルーコードを統合するために、JUnitランナーを使用できます。

@RunWith(Cucumber.class)
@CucumberOptions(
  features = { "classpath:features/calculator.feature" },
  glue = {"com.baeldung.cucumber.calculator" })
public class CalculatorTest {}

5. シナリオアウトラインを使用した機能の書き換え

セクション4.1で見ました。 機能ファイルの定義は、時間のかかる作業であり、エラーが発生しやすくなります。 シナリオの概要:を使用すると、同じ機能ファイルをほんの数行に減らすことができます。

Feature: Calculator
  As a user
  I want to use a calculator to add numbers
  So that I don't need to add myself

  Scenario Outline: Add two numbers <num1> & <num2>
    Given I have a calculator
    When I add <num1> and <num2>
    Then the result should be <total>

  Examples: 
    | num1 | num2 | total |
    | -2 | 3 | 1 |
    | 10 | 15 | 25 |
    | 99 | -99 | 0 |
    | -1 | -10 | -11 |

通常のシナリオ定義シナリオアウトラインを比較する場合、ステップ定義に値をハードコーディングする必要がなくなりました。 値は次のようにパラメータに置き換えられますステップ定義自体で。

シナリオの概要の最後に、Examplesを使用してパイプ区切りのテーブル形式で値が定義されています。

を定義するサンプルを以下に示します。

Examples:
  | Parameter_Name1 | Parameter_Name2 |
  | Value-1 | Value-2 |
  | Value-X | Value-Y |

6. 結論

この簡単な記事では、シナリオを一般的な性質にする方法を示しました。 また、これらのシナリオを作成および維持するための労力を削減します。

この記事の完全なソースコードは、GitHubにあります。