unit-testing - NUnit的[TestCaseSource]具有多个参数,例如MbUnit的[Factory]

标签 unit-testing nunit mbunit

是否可以将NUnit的[TestCaseSource]属性与多个参数一起使用?这是我的代码(正在从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)
{
    ...
}

您会看到,参数作为object []传递,以便在TestSwitchMultiItems中具有多个参数。这应该可行还是必须在TestSwitchMultiItems(object [] parameters)中仅使用一个参数。谢谢你。

最佳答案

是的,您可以将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" }
            }
        };
}

关于unit-testing - NUnit的[TestCaseSource]具有多个参数,例如MbUnit的[Factory],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26472473/

相关文章:

C# 在内存数据库中共享索引?

unit-testing - 如何在 fetch-mock 中模拟多个获取?

.net - 集成测试

multithreading - MbUnit Icarus 在此测试中自毁

mbunit - 无法在 MbUnit + TestDriven 中输出日志消息

安卓测试 : Target app vs Test app?

android - 如何模拟 onLowMemory()?

c# - 在 TestCaseSource 中访问 NUnit 测试名称

visual-studio-2013 - NUnit 在 TFS 构建中加载失败

nhibernate - MSTest 和 NHibernate