37. データテーブル
• コードとデータを分離。
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a | b | c // データ変数
1 | 3 | 3 // データ行
7 | 4 | 7 // 〃
0 | 0 | 0 // 〃
}
// Math.max(1, 3) == 3
// Math.max(7, 4) == 7
// Math.max(0, 0) == 0
// の3イテレーション実施。
38. データテーブル
• 入力値と期待値を(多少)わかりやすく。
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a | b || c // データ変数
1 | 3 || 3 // データ行
7 | 4 || 7 // 〃
0 | 0 || 0 // 〃
}
39. どこが失敗?
• 何回目のイテレーションで失敗したか
わからない。
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a | b || c
1 | 3 || 3
7 | 4 || 4
0 | 0 || 0
}
maximum of two numbers FAILED
!
Condition not satisfied:
!
Math.max(a, b) == c
| | | | |
7 7 4 | 4
false
40. @Unroll
• @Unrollを付けたメソッドまたはクラス
は、イテレーション毎にレポートされる。
@Unroll
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a | b || c
1 | 3 || 3
7 | 4 || 4
0 | 0 || 0
}
maximum of two numbers[0] PASSED
maximum of two numbers[1] FAILED
!
Math.max(a, b) == c
| | | | |
7 7 4 | 4
false
!
maximum of two numbers[2] FAILED
41. もう少しわかりやすく
• メソッド名にプレースホルダを使うこと
で、レポート結果がわかりやすく。
@Unroll
def "maximum of #a and #b is #c"() {
expect:
Math.max(a, b) == c
where:
a | b || c
1 | 3 || 3
7 | 4 || 4
0 | 0 || 0
}
maximum of 1 and 3 is 3 PASSED
maximum of 7 and 4 is 4 FAILED
!
Math.max(a, b) == c
| | | | |
7 7 4 | 4
false
!
maximum of 0 and 0 is 0 FAILED
42. データパイプ
• データ変数ごとにデータプロバイダを用
意し、<<演算子で接続する。
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a << [ 1, 7, 0 ]
b << [ 3, 4, 0 ]
c << [ 3, 7, 0 ]
}
// Collectionだけでなく、テキストファイルやデータベースなど、
// 外部リソースから取得することも可能。
43. データパイプで複数の値
• データプロバイダが複数の値を返す場合、
複数のデータ変数に同時に接続可能。
@Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver")
!
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
[a, b, c] << sql.rows("select a, b, c from maxdata")
}
44. データ変数へ代入
• データ変数に直接値を代入することも可
能。
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a = 3
b = Math.random() * 100
c = a > b ? a : b
}