How can I pass dynamic objects into an NUnit TestCase function?

后端 未结 4 676
忘了有多久
忘了有多久 2020-12-12 22:10

I am writing a data-intensive application. I have the following tests. They work, but they\'re pretty redundant.

[Test]
public void DoSanityCheck_WithCountEqu         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 22:32

    I tracked it down. I can't pass an instantiated object into a test via TestCase because attributes are strictly for static meta-data. But the NUnit team has a solution for that, TestCaseSource. The post on the NUnit list that answered the question is here.

    Here is what my solution now looks like:

    public static IEnumerable CountEqualsZeroAndHouseGrossIsGreaterTestCases
    {
        get
        {
            yield return new TestCaseData(report, report.Merchants[4268435971532164].LineItem["EBTPerItem"], 4268435971532164, "EBTPerItem").SetName("ReportMerchantsLineItem");
            yield return new TestCaseData(report, report.Merchants[5461324658456716].AggregateTotals, 5461324658456716, "WirelessPerItem").SetName("ReportMerchantsAggregateTotals");
            yield return new TestCaseData(report, report.AggregateTotals, null, "AggregateTotals").SetName("ReportAggregateTotals");
            yield return new TestCaseData(report, report.AggregateTotals.LineItem["WirelessPerItem"], null, "WirelessPerItem").SetName("ReportAggregateTotalsLineItem");
        }
    }
    
    
    [TestCaseSource("CountEqualsZeroAndHouseGrossIsGreaterTestCases")]
    public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_TestCase_SetsWarning(Reports.ResidualsReport report, Reports.LineItemObject 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);
    }
    

    It is not as pretty as I hoped and is not as easy to read. But it did succeed on cutting down code duplication, which should make things easier to maintain and fix.

提交回复
热议问题