これも遅い回答であることは承知していますが、MS Test フレームワークの使用に固執している私のチームでは、匿名型のみに依存してテスト データの配列を保持し、LINQ をループして各行をテストする手法を開発しました。追加のクラスやフレームワークは必要なく、読みやすく理解しやすい傾向があります。また、外部ファイルや接続されたデータベースを使用したデータ駆動型テストよりも実装がはるかに簡単です。
たとえば、次のような拡張メソッドがあるとします。
public static class Extensions
{
/// <summary>
/// Get the Qtr with optional offset to add or subtract quarters
/// </summary>
public static int GetQuarterNumber(this DateTime parmDate, int offset = 0)
{
return (int)Math.Ceiling(parmDate.AddMonths(offset * 3).Month / 3m);
}
}
LINQ に結合された匿名型の配列を使用して、次のようなテストを作成できます。
[TestMethod]
public void MonthReturnsProperQuarterWithOffset()
{
// Arrange
var values = new[] {
new { inputDate = new DateTime(2013, 1, 1), offset = 1, expectedQuarter = 2},
new { inputDate = new DateTime(2013, 1, 1), offset = -1, expectedQuarter = 4},
new { inputDate = new DateTime(2013, 4, 1), offset = 1, expectedQuarter = 3},
new { inputDate = new DateTime(2013, 4, 1), offset = -1, expectedQuarter = 1},
new { inputDate = new DateTime(2013, 7, 1), offset = 1, expectedQuarter = 4},
new { inputDate = new DateTime(2013, 7, 1), offset = -1, expectedQuarter = 2},
new { inputDate = new DateTime(2013, 10, 1), offset = 1, expectedQuarter = 1},
new { inputDate = new DateTime(2013, 10, 1), offset = -1, expectedQuarter = 3}
// Could add as many rows as you want, or extract to a private method that
// builds the array of data
};
values.ToList().ForEach(val =>
{
// Act
int actualQuarter = val.inputDate.GetQuarterNumber(val.offset);
// Assert
Assert.AreEqual(val.expectedQuarter, actualQuarter,
"Failed for inputDate={0}, offset={1} and expectedQuarter={2}.", val.inputDate, val.offset, val.expectedQuarter);
});
}
}
この手法を使用する場合、Assert に入力データを含む書式設定されたメッセージを使用すると、テストが失敗する原因となった行を特定するのに役立ちます。
AgileCoder.netで、このソリューションの背景と詳細についてブログを書いています。