Piszę aplikację intensywnie korzystającą z danych. Mam następujące testy. Działają, ale są dość zbędne.Jak przekazywać obiekty dynamiczne do funkcji NUnit TestCase?
[Test]
public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_InMerchantAggregateTotals_SetsWarning()
{
report.Merchants[5461324658456716].AggregateTotals.ItemCount = 0;
report.Merchants[5461324658456716].AggregateTotals._volume = 0;
report.Merchants[5461324658456716].AggregateTotals._houseGross = 1;
report.DoSanityCheck();
Assert.IsTrue(report.FishyFlag);
Assert.That(report.DataWarnings.Where(x=> x is Reports.WarningObjects.ImbalancedVariables && x.mid == 5461324658456716 && x.lineitem == "AggregateTotals").Count() > 0);
}
[Test]
public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_InAggregateTotals_SetsWarning()
{
report.AggregateTotals.ItemCount = 0;
report.AggregateTotals._volume = 0;
report.AggregateTotals._houseGross = 1;
report.DoSanityCheck();
Assert.IsTrue(report.FishyFlag);
Assert.That(report.DataWarnings.Where(x=> x is Reports.WarningObjects.ImbalancedVariables && x.mid == null && x.lineitem == "AggregateTotals").Count() > 0);
}
[Test]
public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_InAggregateTotalsLineItem_SetsWarning()
{
report.AggregateTotals.LineItem["WirelessPerItem"].ItemCount = 0;
report.AggregateTotals.LineItem["WirelessPerItem"]._volume = 0;
report.AggregateTotals.LineItem["WirelessPerItem"]._houseGross = 1;
report.DoSanityCheck();
Assert.IsTrue(report.FishyFlag);
Assert.That(report.DataWarnings.Where(x=> x is Reports.WarningObjects.ImbalancedVariables && x.mid == null && x.lineitem == "WirelessPerItem").Count() > 0);
}
Te same właściwości są modyfikowane na początku, podobnie jak elementy potomne różnych obiektów kontenera, a wartości para w zmianie asercji na końcu. Muszę napisać kilkadziesiąt z nich, sprawdzając różne właściwości. Więc chcę sparametryzować test. Sztuczka polega na przekazywaniu obiektu kontenera jako parametru do testu. Obiekt kontenera jest tworzony w urządzeniu testowym SetUp.
Co chcę osiągnąć będzie wyglądać mniej więcej tak:
[TestCase(report.AggregateTotals.LineItem["WirelessPerItem"], 0, "WirelessPerItem")]
[TestCase(report.AggregateTotals, 4268435971532164, "AggregateTotals")]
[TestCase(report.Merchants[5461324658456716].AggregateTotals, 5461324658456716, "WirelessPerItem")]
[TestCase(report.Merchants[4268435971532164].LineItem["EBTPerItem"], 4268435971532164, "EBTPerItem")]
public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_TestCase_SetsWarning(object container, long mid, string field)
{
container.ItemCount = 0;
container._volume = 0;
container._houseGross = 1;
report.DoSanityCheck();
Assert.IsTrue(report.FishyFlag);
Assert.That(report.DataWarnings.Where(x=> x is Reports.WarningObjects.ImbalancedVariables && x.mid == mid && x.lineitem == field).Count() > 0);
}
Ale to nie działa i nie jestem pewien, jak to działało, lub jeśli jest to możliwe.
Myślę, że właściwość CountEqualsZeroAndHouseGrossIsGreaterTestCases powinna być statyczna. – moyomeh
Jeśli używasz C# 6+, zamiast używać nazwy jako łańcucha, możesz użyć "nameof". [TestCaseSource (nameof (CountEqualsZeroAndHouseGrossIsGreaterTestCases))], co czyni go silnie wpisanym. –
Od NUnit 3, TestCaseSource jest również ograniczony do źródeł statycznych. – buckminst