TOP →
Java →
テスト → This Page
TestNGの利用方法(応用編)【ユニットテスト - テスト】
前提
本ページは3ページ目です。
応用編なので利用準備、基本編を理解している前提ですすめます。
例外処理テスト
TestNGの概要~利用準備【ユニットテスト - テスト】でも紹介したとおり、
TestNGでは例外が発生するテストが簡単にできます。
テスト対象クラスの作成
まずはテスト対象となるクラスを作成します。
今回は以下のような検証用クラスを作ってみました。
Apply.java
package com.mitchy_world.testng.sample;
/**
* 応用編サンプル用クラス
*/
public class Apply {
/**
* クォータ名を取得する
*
* @param month 月
* @return クォータ名
*/
static public String getQuota(int month) {
if (1 <= month && month <= 3 ) {
return "4Q";
} else if (4 <= month && month <= 6 ) {
return "1Q";
} else if (7 <= month && month <= 9 ) {
return "2Q";
} else if (10 <= month && month <= 12 ) {
return "3Q";
}
throw new RuntimeException("例外発生");
}
}
テスト・クラスの作成
ではテスト・クラスを作ってみましょう。
TestNG では @Test アノテーションに expectedExceptions 属性を追加することで簡単に例外発生のテストができます。
以下のサンプルでは getQuota_05 メソッドが例外発生のテストになります。
ApplyTest.java
package com.mitchy_world.testng.sample;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
public class ApplyTest {
@Test
public void getQuota_01() {
System.out.println("getQuota_01");
assertEquals(Apply.getQuota(1), "4Q");
}
@Test
public void getQuota_02() {
System.out.println("getQuota_02");
assertEquals(Apply.getQuota(4), "1Q");
}
@Test
public void getQuota_03() {
System.out.println("getQuota_03");
assertEquals(Apply.getQuota(7), "2Q");
}
@Test
public void getQuota_04() {
System.out.println("getQuota_04");
assertEquals(Apply.getQuota(10), "3Q");
}
@Test(expectedExceptions = { java.lang.RuntimeException.class })
public void getQuota_05() {
System.out.println("getQuota_05");
Apply.getQuota(0);
}
}
では早速実行してみましょう。
全て成功すると思います。
getQuota_05 メソッドでは指定した例外が発生したので成功となっています。
簡単にできましたね。
タイムアウトテスト
特定の時間内に処理が終わるかどうかをテストできます。
テスト対象クラスの作成
先ほどのクラスに以下のメソッドを追加しましょう。
Apply.java
/**
* 負荷テスト用メソッド
*
* @param count
*/
static public void executeStress(int count) {
String s = "";
for (int i = 0; i < count; i++) {
s += "x";
}
}
テスト・クラスの作成
ではテスト・クラスを作ってみましょう。
TestNG では @Test アノテーションに timeOut 属性を追加することで簡単にタイムアウトのテストができます。
先ほどのテスト・クラスに以下のメソッドを追加してみましょう。
ApplyTest.java
@Test(timeOut = 100)
public void executeStress() {
Apply.executeStress(10000);
}
では早速実行してみましょう。
お使いの環境によるので失敗するかもしれませんが、その場合は数値を色々と変更して試してみて下さい。
timeOut 属性に指定したミリ秒以内に処理が終わった場合は成功、
終わらなかった場合は
org.testng.internal.thread.ThreadTimeoutException
例外が発生して失敗となります。
複数スレッドでのテスト実行
複数スレッドを使ってテストを並列実行させてみましょう。
テスト・スイートの作成
test フォルダの直下に以下のような testngsample.xml を作成します。
testngsample.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" thread-count="2" parallel="tests">
<test name="Test1">
<classes>
<class name="com.mitchy_world.testng.sample.ApplyTest"/>
</classes>
</test>
<test name="Test2">
<classes>
<class name="com.mitchy_world.testng.sample.ConvertorUtilityTest"/>
<class name="com.mitchy_world.testng.sample.ValidatorUtilityTest"/>
</classes>
</test>
</suite>
suite 要素にある thread-count 属性はスレッドの数を指定します。
suite 要素にある parallel 属性は、「どの単位」で同じスレッドを使うかを指定します。
parallel 属性には methods、tests、classes が指定できます。
今回は test 要素ごとにスレッドを分けたいので tests を指定します。
test 要素ごとにスレッドを分けるので、
スレッド1・・・ApplyTest
スレッド2・・・ConvertorUtilityTest、ValidatorUtilityTest
となります。
テスト・クラスの修正
実際にはテスト・クラスを修正する必要はないですが、
今回はスレッドが指定した通りになっているかを確認するため、
テスト・クラスの各テスト・メソッドの
System.out.println("テスト・メソッド名");
となっている箇所を全て
System.out.println("テスト・メソッド名:" + Thread.currentThread().getId());
に変更してみます。
テストの実行と確認
ではテスト・スイート(testngsample.xml)を実行してみましょう。
コンソール画面に以下のように出力されると思います。
concatHyphen_01:12
executeStress:13
concatHyphen_02:12
concatHyphen_03:12
nullToBlank_01:12
nullToBlank_02:12
isBlank_01:12
isInt_01:12
getQuota_01:11
getQuota_02:11
getQuota_03:11
(一部省略しています)
ApplyTest クラスはスレッドID:11で、
ConvertorUtilityTest クラス、ValidatorUtilityTest クラスはスレッドID:12で実行されていることが分かります。
が、ApplyTest クラスの executeStress メソッドだけなぜかスレッドID:13になっていると思います。
これは例外的に、タイムアウトのテストだけはさらに別スレッドになるからです。
ダウンロード
解説で使ったクラスなどを含んだ eclipse 用プロジェクト一式
更新履歴
2011/04/10 新規作成
TOP →
Java →
テスト → This Page