キュウリとシナリオの概要

きゅうりとシナリオの概要

1. 前書き

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

入力/出力の順列が異なるフレームワークto write repetitive scenariosを使用すると、非常に時間がかかり、保守が難しく、もちろんイライラする可能性があります。

Cucumberには、Scenario Outline coupled with Examplesの概念を使用して、この労力を削減するためのソリューションが付属しています。 以下のセクションでは、例を取り上げて、この労力を最小限に抑える方法を確認します。

アプローチとGherkin言語について詳しく知りたい場合は、this articleをご覧ください。

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

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


    info.cukes
    cucumber-junit
    1.2.5
    test


    info.cukes
    cucumber-java
    1.2.5
    test


    org.hamcrest
    hamcrest-library
    1.3
    test

Maven Centralからの依存関係への便利なリンク:cucumber-junitcucumber-javahamcrest-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.example.cucumber.calculator" })
public class CalculatorTest {}

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

セクション4.1で見ました。 機能ファイルの定義が時間のかかるタスクであり、エラーが発生しやすいことがあります。 Scenario Outline:を使用すると、同じ機能ファイルを数行に減らすことができます。

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  & 
    Given I have a calculator
    When I add  and 
    Then the result should be 

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

通常のScenario DefinitionScenario Outlineを比較する場合、ステップ定義で値をハードコーディングする必要がなくなりました。 値は、ステップ定義自体で<parameter_name>としてパラメーターに置き換えられます。

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

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

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

6. 結論

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

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