YokohamaUnit
で入力値と出力値だけが異なるようなテストケースをたくさん作りたい場合、
テストデータを表に抽出することができる。
# Test: String.endsWith
Assert `sut.endsWith(suffix)` is `expected`
for all sut, suffix and expected in Table [Test data for endsWith].
| sut | suffix | expected |
| ------------- | ------- | -------- |
| "hello world" | "" | true |
| "hello world" | "world" | true |
| "hello world" | "hello" | false |
[Test data for endsWith]
表のキャプションは表の前でもよい。
[Test data for endsWith]
| sut | suffix | expected |
| ------------- | ------- | -------- |
| "hello world" | "" | true |
| "hello world" | "world" | true |
| "hello world" | "hello" | false |
JUnitのTheoryとの大きな違いだが、表の1行が1つのテストメソッドとなる。
つまり、上記のような記述の場合、テストメソッドは3つできる。
したがって、表の一部のテストが失敗しても他のテストは実行されるし、
テストケース数も3件と数えられる。
テストデータをCSVファイルやExcelに書くこともできる。
# Test: String.startsWith
Assert `sut.startsWith(prefix)` is `expected`
for all sut, prefix and expected in Excel 'TestExcel.xlsx'.
# Test: String.startsWith
Assert `sut.startsWith(prefix)` is `expected`
for all sut, prefix and expected in CSV 'TestCSV.csv'.
この場合、CSVファイルやExcelファイル読み込みはテスト実行時ではなくて
テストコードのコンパイル時に行われ、やはり1行が1つのテストメソッドとなる。
コメント 0