Na mojej drużynie, która została zablokowana za pomocą ramy MS testu, opracowaliśmy technikę, która opiera się tylko na Anonim Typy do przechowywania tablicy danych testowych i LINQ do przechodzenia i testowania każdego wiersza. Nie wymaga żadnych dodatkowych zajęć ani ram i jest raczej łatwy do odczytania i zrozumienia. Jest także znacznie łatwiejsze do wdrożenia niż testy oparte na danych z wykorzystaniem plików zewnętrznych lub połączonej bazy danych.
Załóżmy, że masz metodę rozszerzenia takiego:
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);
}
}
można użyć i tablicą typów anonimowych połączonych do LINQ napisać testy tak:
[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);
});
}
}
Podczas korzystania W tej technice pomocne jest sformatowanie komunikatu zawierającego dane wejściowe w Asercie, aby pomóc w zidentyfikowaniu, który wiersz powoduje niepowodzenie testu.
Blogowałem o tym rozwiązaniu z większą ilością tła i szczegółów pod numerem AgileCoder.net.
Niestety, DaTest wydaje się działać tylko z VS2008. –
http://code.google.com/p/datest/wiki/DaTest –
Możliwy duplikat [Jak uruchomić metodę testową z wieloma parametrami w MSTest?] (Http://stackoverflow.com/questions/9021881/how -to-run-a-test-method-with-multiple-parameters-in-mstest) – Rob