unit-testingnunitmbunit

NUnit's [TestCaseSource] with multiple arguments like with MbUnit's [Factory]


Is it possible to use NUnit's [TestCaseSource] attribute with more than one arguments? Here's my code (which is being migrated from MbUnit):

public IEnumerable<object[]> GetTestSwitchMultiItems()
{
    yield return new object[] { SwitchType.Sell, 0.94733, 
                       new SwitchSourceItem[] { new SwitchSourceItem(1176, 100, 50, SwitchSourceItem.QuantityType.TotalQuantity, SwitchType.Sell)}, 
                       new SwitchEquivalentItem[] { new SwitchEquivalentItem(415318955, 35, 25, SwitchType.Buy), new SwitchEquivalentItem(4348, 65, 45, SwitchType.Buy) } };

    yield return new object[] { SwitchType.Sell, 0.94733, 
                       new SwitchSourceItem[] { new SwitchSourceItem(1176, 100, 50, SwitchSourceItem.QuantityType.TotalQuantity, SwitchType.Sell)}, 
                       new SwitchEquivalentItem[] { new SwitchEquivalentItem(415318955, 15, 25, SwitchType.Buy), new SwitchEquivalentItem(4348, 25, 45, SwitchType.Buy), 
                                                    new SwitchEquivalentItem(430397879, 20, 15, SwitchType.Buy), new SwitchEquivalentItem(5330, 20, 85, SwitchType.Buy)} };
}

[Test, TestCaseSource("GetTestSwitchMultiItems")]
public void TestSwitchMultiItems(SwitchType switchType, double exchangeRate, SwitchSourceItem[] sources, SwitchEquivalentItem[] equivs)
{
    ...
}

You see, parameters are passed as an object[] in order to have more than one parameters in TestSwitchMultiItems. Should that work or must I instead use only one parameter in TestSwitchMultiItems(object[] parameters). Thank you.


Solution

  • Yes, you can use the TestCaseSource attribute with multiple arguments. In the example you give, you will see the test TestSwitchMultiItems run twice. I used NUnit on the following contrived test code. TestSwitchMultiItems runs twice, and the trivial Assert call in each test passes.

    [Test, TestCaseSource("GetTestSwitchMultiItems")]
    public void TestSwitchMultiItems(string switchType, double exchangeRate, object[] sources, object[] equivs)
    {
       Assert.AreEqual("Sell", switchType);
    }
    
    public IEnumerable<object[]> GetTestSwitchMultiItems()
    {
        yield return
           new object[]
           {
              "Sell", 0.94733, new object[] { new { a = 1176, b = 100, c = 50, d = 5, e = "Sell" } },
                new object[] { new { a = 415318955, b = 35, c = 25, d = "Buy", e = 4348, f = 65, g = 45, h = "Buy" } }
            };
    
        yield return
            new object[]
            {
               "Sell", 0.94733, new object[] { new { a = 1176, b = 100, c = 50, d = 5, e = "Sell" } },
                new object[]
                {
                    new { a = 415318955, b = 35, c = 25, d = "Buy", e = 4348, f = 65, g = 45, h = "Buy" },
                    new { a = 415318955, b = 35, c = 25, d = "Sell", e = 7348, f = 65, g = 45, h = "Sell" }
                }
            };
    }