I am writing a data-intensive application. I have the following tests. They work, but they're pretty redundant.
[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);
}
The same properties are modified at the beginning, just as children of different container objects, and a couple of values in the assertion change at the end.
I need to write a few dozen of these, checking different properties. So I want to parameterize the test. The trick is passing the container object as a parameter to the test. The container object is instantiated in the test fixture SetUp.
I want to achieve something like this:
[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);
}
But that doesn't work and I'm not sure how to make it work, or if it's possible.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…