JGoTestingによるテスト

JGoTestingを使用したテスト

1. 概要

JGoTestingJUnit-compatible testing framework inspired by Go’s testing packageです。

この記事では、JGoTestingフレームワークの主要な機能を調査し、その機能を紹介するための例を実装します。

2. メーベン依存

まず、jgotestingの依存関係をpom.xmlに追加しましょう。


    org.jgotesting
    jgotesting
    0.12

このアーティファクトの最新バージョンはhereにあります。

3. 前書き

JGoTestingを使用すると、JUnitと互換性のあるテストを作成できます。 For every assertion method JGoTesting provides, there is one in JUnit with the same signatureなので、このライブラリの実装は非常に簡単です。

ただし、unlike JUnit, when an assertion fails, JGoTesting doesn’t stop the execution of the test。 代わりに、失敗はeventとして記録され、すべてのアサーションが実行されたときにのみ表示されます。

4. JGoTestingの実行

このセクションでは、JGoTestingのセットアップ方法の例を見て、その可能性を探ります。

4.1. 入門

テストを作成するために、最初にJGoTestingのアサーションメソッドをインポートしましょう。

import static org.jgotesting.Assert.*; // same methods as JUnit
import static org.jgotesting.Check.*; // aliases starting with "check"
import static org.jgotesting.Testing.*;

ライブラリには、@Ruleアノテーションでマークされた必須のJGoTestRuleインスタンスが必要です。 これは、クラス内のすべてのテストがJGoTestingによって管理されることを示しています。

そのようなルールを宣言するクラスを作成しましょう:

public class JGoTestingUnitTest {

    @Rule
    public final JGoTestRule test = new JGoTestRule();

    //...
}

4.2. テストを書く

JGoTestingは、テストを記述するための2つのアサーションメソッドのセットを提供します。 最初のセットのメソッドの名前はassertで始まり、JUnitと互換性のあるものであり、他のメソッドはcheckで始まります。

メソッドの両方のセットは同じように動作し、ライブラリはそれらの間に1対1の対応を提供します。

両方のバージョンを使用して、数値が別の数値と等しいかどうかをテストする例を次に示します。

@Test
public void whenComparingIntegers_thenEqual() {
    int anInt = 10;

    assertEquals(anInt, 10);
    checkEquals(anInt, 10);
}

APIの残りの部分は一目瞭然なので、これ以上詳しくは説明しません。 以下のすべての例では、メソッドのcheckバージョンのみに焦点を当てます。

4.3. 障害イベントとメッセージ

チェックが失敗すると、JGoTestingはテストケースの実行を継続するために失敗を記録します。 After the test ends, the failures are reported

これがどのように見えるかを示す例を次に示します。

@Test
public void whenComparingStrings_thenMultipleFailingAssertions() {
    String aString = "The test string";
    String anotherString = "The test String";

    checkEquals("Strings are not equal!", aString, equalTo(anotherString));
    checkTrue("String is longer than one character", aString.length() == 1);
    checkTrue("A failing message", aString.length() == 2);
}

テストを実行すると、次の出力が得られます。

org.junit.ComparisonFailure: Strings are not equal!
  expected:<[the test s]tring> but was:<[The Test S]tring>
// ...
java.lang.AssertionError: String is longer than one character
// ...
java.lang.AssertionError: Strings are not the same
  expected the same: was not:

各メソッドでエラーメッセージを渡すだけでなく、テストに少なくとも1つのエラーがある場合にのみ表示されるようにログを記録することもできます。

これを実践するテストメソッドを書いてみましょう。

@Test
public void whenComparingNumbers_thenLoggedMessage() {
    log("There was something wrong when comparing numbers");

    int anInt = 10;
    int anotherInt = 10;

    checkEquals(anInt, 10);
    checkTrue("First number should be bigger", 10 > anotherInt);
    checkSame(anInt, anotherInt);
}

テストの実行後、次の出力が得られます。

org.jgotesting.events.LogMessage: There was something wrong
  when comparing numbers
// ...
java.lang.AssertionError: First number should be bigger

メッセージをString.format()メソッドとしてフォーマットできるlogf()に加えて、条件式に基づいてメッセージをログに記録する_ we can also use the _logIf()およびlogUnless()メソッドに注意してください。

4.4. テストの中断

JGoTestingは、特定の前提条件に合格しない場合にテストケースを終了するいくつかの方法を提供します。

必要なファイルが存在しないため、test that ends prematurelyの例を次に示します。

@Test
public void givenFile_whenDoesnotExists_thenTerminated() throws Exception {
    File aFile = new File("a_dummy_file.txt");

    terminateIf(aFile.exists(), is(false));

    // this doesn't get executed
    checkEquals(aFile.getName(), "a_dummy_file.txt");
}

terminate()メソッドとterminateUnless()メソッドを使用して、テストの実行を中断することもできることに注意してください。

4.5. 連鎖

JGoTestRuleクラスにもfluent API that we can use to chain checks togetherがあります。

JGoTestRuleのインスタンスを使用して、Stringオブジェクトの複数のチェックをチェーン化する例を見てみましょう。

@Test
public void whenComparingStrings_thenMultipleAssertions() {
    String aString = "This is a string";
    String anotherString = "This Is a String";

    test.check(aString, equalToIgnoringCase(anotherString))
      .check(aString.length() == 16)
      .check(aString.startsWith("This"));
}

4.6. カスタムチェック

boolean式とMatcherインスタンスに加えて、JGoTestRule‘s methods can accept a custom Checker object to do the checking。 これは、ラムダ式を使用して実装できる単一の抽象メソッドインターフェイスです。

前述のインターフェースを使用して、Stringが特定の正規表現に一致するかどうかを確認する例を次に示します。

@Test
public void givenChecker_whenComparingStrings_thenEqual() throws Exception {
    Checker aChecker = s -> s.matches("\\d+");

    String aString = "1235";

    test.check(aString, aChecker);
}

5. 結論

このクイックチュートリアルでは、テストを記述するためにJGoTestingが提供する機能を調べました。

JUnit互換のassertメソッドとそれに対応するcheckメソッドを紹介しました。 また、ライブラリが失敗イベントを記録および報告する方法も確認し、ラムダ式を使用してカスタムCheckerを作成しました。

いつものように、この記事の完全なソースコードはover on Githubにあります。