2014-10-20 81 views
5

是否可以使用NUnit的[TestCaseSource]屬性和多個參數?這裏是我的代碼(正在從MbUnit的遷移):NUnit的[TestCaseSource]帶有多個參數,如MbUnit的[Factory] ​​

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) 
    { 
     ... 
    } 

你看,參數爲對象[]傳遞,以在TestSwitchMultiItems多個參數。應該這樣做還是必須在TestSwitchMultiItems(object []參數)中只使用一個參數。謝謝。

回答

7

是的,您可以使用具有多個參數的TestCaseSource屬性。在你給的例子中,你會看到測試TestSwitchMultiItems運行兩次。我在以下人造測試代碼上使用了NUnit。 TestSwitchMultiItems運行兩次,並且在每次測試中調用微不足道的Assert

[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" } 
       } 
      }; 
    }